|
| 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 | +``` |
0 commit comments