Skip to content

Commit 4eb6608

Browse files
author
willzhen
committed
Update readme
1 parent 492219c commit 4eb6608

File tree

6 files changed

+212
-3
lines changed

6 files changed

+212
-3
lines changed

misc/goroutine_help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (GoroutineHelp) Recover(recoverFunc RecoverFunc) error {
2929
// GoroutinFunc ...
3030
type GoroutinFunc func()
3131

32-
// SafeGo 安全启用携程,捕获 crash,并且重试
32+
// SafeGo 安全启用携程,捕获 crash,并且重试。recover 表示捕获到异常的情况下是否需要重新开启新的携程。
3333
func (help GoroutineHelp) SafeGo(goFunc GoroutinFunc, recover bool) {
3434
go func() {
3535
var recoverFunc RecoverFunc = nil

misc/id.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
type IDGenerator struct {
1414
}
1515

16-
// GenerateRandomString 生成随机字符串
16+
// GenerateRandomString 生成随机字符串, alphanum 字符集,nil 使用默认字符集。
1717
func (gen IDGenerator) GenerateRandomString(length int, alphanum []byte) string {
1818
if alphanum == nil {
1919
alphanum = []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")

misc/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func traverseMap(mp map[string]interface{}) (res map[string]interface{}) {
4545
return res
4646
}
4747

48-
// FoldLog 折叠log中的长数据,比如图片 base64 等等
48+
// FoldLog 折叠 json log 中的长数据,比如图片 base64 等等
4949
func FoldLog(content []byte) string {
5050
data := gjson.ParseBytes(content).Map()
5151
mp := map[string]interface{}{}

misc/misc_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,49 @@
11
package misc_test
22

33
import (
4+
"context"
45
"fmt"
6+
"os"
7+
"path"
58
"testing"
69

710
"github.com/memory-overflow/go-common-library/misc"
811
)
912

13+
func TestDownload(t *testing.T) {
14+
ctx := context.Background()
15+
uri := "http://www.baidu.com"
16+
dirs := "/root"
17+
filename := "baidu.html"
18+
defer os.Remove(path.Join(dirs, filename))
19+
// 目录不存在会自动创建
20+
if err := misc.DownloadFile(ctx, uri, dirs, filename); err != nil {
21+
t.Fatalf("download file: %v", err)
22+
}
23+
24+
// 目录不存在会自动创建
25+
if err := misc.DownloadFileWithLimit(ctx, uri, dirs, filename, 1000); err != nil {
26+
t.Fatalf("download file: %v", err)
27+
}
28+
}
29+
1030
func goFunc() {
1131
fmt.Println("start goFunc")
1232
panic("test panic")
1333
}
1434

1535
func TestGoroutineHelp(t *testing.T) {
1636
help := misc.GoroutineHelp{}
37+
// SafeGo, 如果异常,重启协程
1738
help.SafeGo(goFunc, true)
1839

40+
// 后面的处理,如果出现异常,捕获异常防止线程退出,recoverFunc 用来恢复携程的处理。
41+
defer help.Recover(func() error {
42+
// recover code
43+
return nil
44+
})
45+
46+
// other code
1947
for {
2048

2149
}

misc/readme.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
[TOC]
2+
3+
# misc 模块说明
4+
5+
## time_related 模块
6+
时间处理相关的函数
7+
- TimeTick —— 计时器,通过 TimeTick.Tick() 打点计时间,返回距离上一次 Tick 过去的时间,单位 ms。
8+
- Timed —— 函数调用计时。
9+
10+
example:
11+
```go
12+
import (
13+
"testing"
14+
"time"
15+
16+
timerelated "github.com/memory-overflow/go-common-library/misc/time_related"
17+
)
18+
19+
func f() error {
20+
time.Sleep(3 * time.Second)
21+
return nil
22+
}
23+
24+
func TestTimeRelated(t *testing.T) {
25+
// test TimeTick
26+
// build tick
27+
tick := timerelated.BuildTimeTick()
28+
time.Sleep(time.Second)
29+
// 距离上一次打点的时间间隔
30+
lastTickPast := tick.Tick()
31+
t.Logf("after last tick time pass: %dms", lastTickPast)
32+
// 距离上一次打点的时间间隔
33+
time.Sleep(2 * time.Second)
34+
lastTickPast = tick.Tick()
35+
t.Logf("after last tick time pass: %dms", lastTickPast)
36+
37+
// test Timed
38+
cost, _ := timerelated.Timed(f)
39+
t.Logf("call f time cost %dms", cost)
40+
}
41+
42+
```
43+
44+
## download 模块
45+
- DownloadFile 下载网络文件
46+
- DownloadFileWithLimit 下载网络文件(带文件大小限制)
47+
48+
example:
49+
```go
50+
import (
51+
"context"
52+
"fmt"
53+
"os"
54+
"path"
55+
"testing"
56+
57+
"github.com/memory-overflow/go-common-library/misc"
58+
)
59+
60+
func TestDownload(t *testing.T) {
61+
ctx := context.Background()
62+
uri := "http://www.baidu.com"
63+
dirs := "/root"
64+
filename := "baidu.html"
65+
defer os.Remove(path.Join(dirs, filename))
66+
// 目录不存在会自动创建
67+
if err := misc.DownloadFile(ctx, uri, dirs, filename); err != nil {
68+
t.Fatalf("download file: %v", err)
69+
}
70+
71+
// 目录不存在会自动创建
72+
if err := misc.DownloadFileWithLimit(ctx, uri, dirs, filename, 1000); err != nil {
73+
t.Fatalf("download file: %v", err)
74+
}
75+
}
76+
```
77+
78+
79+
## goroutine_help 模块
80+
协程相关的处理模块。
81+
- GoroutineHelp
82+
- GoroutineHelp.Recoverd:协程 recover 处理函数。
83+
- GoroutineHelp.SafeGo:安全的启用携程。
84+
85+
example:
86+
87+
```go
88+
import (
89+
"context"
90+
"fmt"
91+
"os"
92+
"path"
93+
"testing"
94+
95+
"github.com/memory-overflow/go-common-library/misc"
96+
)
97+
98+
func goFunc() {
99+
fmt.Println("start goFunc")
100+
panic("test panic")
101+
}
102+
103+
func TestGoroutineHelp(t *testing.T) {
104+
help := misc.GoroutineHelp{}
105+
// SafeGo, 如果异常,重启协程
106+
help.SafeGo(goFunc, true)
107+
108+
// 后面的处理,如果出现异常,捕获异常防止线程退出,recoverFunc 用来恢复携程的处理。
109+
defer help.Recover(func() error {
110+
// recover code
111+
return nil
112+
})
113+
114+
// other code
115+
for {
116+
117+
}
118+
}
119+
```
120+
## retry
121+
Retry——带重试调用。
122+
123+
124+
## id 生成器
125+
IDGenerator 对象封装了很多生成 id 的函数。
126+
- IDGenerator.GenerateRandomString:生成随机字符串。
127+
- IDGenerator.GenerateUUID:生成UUID。
128+
- IDGenerator.BighumpToUnderscore:大驼峰参数转换成下划线。
129+
- IDGenerator.UnderscoreToBighump:下划线参数转换成大驼峰。
130+
131+
## log
132+
- FoldLog: 折叠日志中数据过长的字段,输入仅支持 json 字符串。
133+
134+
example:
135+
```go
136+
import (
137+
"context"
138+
"fmt"
139+
"os"
140+
"path"
141+
"testing"
142+
143+
"github.com/memory-overflow/go-common-library/misc"
144+
)
145+
146+
func TestFlodLog(t *testing.T) {
147+
log := `{"RetrieveInputData":"其他","RetrieveInputType":0,"FilterSet":[],"PageNumber":1,"PageSize":6,"TIBusinessID":1,"TIProjectID":1}`
148+
fmt.Println(misc.FoldLog([]byte(log)))
149+
}
150+
```

misc/time_related/time_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package timerelated_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
timerelated "github.com/memory-overflow/go-common-library/misc/time_related"
8+
)
9+
10+
func f() error {
11+
time.Sleep(3 * time.Second)
12+
return nil
13+
}
14+
15+
func TestTimeRelated(t *testing.T) {
16+
// test TimeTick
17+
// build tick
18+
tick := timerelated.BuildTimeTick()
19+
time.Sleep(time.Second)
20+
// 距离上一次打点的时间间隔
21+
lastTickPast := tick.Tick()
22+
t.Logf("after last tick time pass: %dms", lastTickPast)
23+
// 距离上一次打点的时间间隔
24+
time.Sleep(2 * time.Second)
25+
lastTickPast = tick.Tick()
26+
t.Logf("after last tick time pass: %dms", lastTickPast)
27+
28+
// test Timed
29+
cost, _ := timerelated.Timed(f)
30+
t.Logf("call f time cost %dms", cost)
31+
}

0 commit comments

Comments
 (0)