File tree Expand file tree Collapse file tree 4 files changed +56
-0
lines changed
src/component/web/http_client/restyKit Expand file tree Collapse file tree 4 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -241,6 +241,7 @@ require (
241241 github.com/go-ole/go-ole v1.2.6 // indirect
242242 github.com/go-playground/locales v0.14.1 // indirect
243243 github.com/go-playground/universal-translator v0.18.1 // indirect
244+ github.com/go-resty/resty/v2 v2.17.2 // indirect
244245 github.com/go-sql-driver/mysql v1.8.1 // indirect
245246 github.com/go-stack/stack v1.8.1 // indirect
246247 github.com/go-text/render v0.2.0 // indirect
Original file line number Diff line number Diff line change @@ -601,6 +601,8 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC
601601github.com/go-redis/redis/v8 v8.11.5 /go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo =
602602github.com/go-redsync/redsync/v4 v4.16.0 h1:bNcOzeHH9d3s6pghU9NJFMPrQa41f5Nx3L4YKr3BdEU =
603603github.com/go-redsync/redsync/v4 v4.16.0 /go.mod h1:V4gagqgyASWBZuwx4xGzu72aZNb/6Mo05byUa3mVmKQ =
604+ github.com/go-resty/resty/v2 v2.17.2 h1:FQW5oHYcIlkCNrMD2lloGScxcHJ0gkjshV3qcQAyHQk =
605+ github.com/go-resty/resty/v2 v2.17.2 /go.mod h1:kCKZ3wWmwJaNc7S29BRtUhJwy7iqmn+2mLtQrOyQlVA =
604606github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y =
605607github.com/go-sql-driver/mysql v1.8.1 /go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg =
606608github.com/go-stack/stack v1.8.0 /go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY =
Original file line number Diff line number Diff line change 1+ ## 注意点
2+
3+ - 与标准库net/http(必须手动关闭)不同,Resty 内部已经帮你处理了连接复用和响应体的读取/关闭,不需要手动 resp.Body.Close()。
Original file line number Diff line number Diff line change 1+ package restyKit
2+
3+ import (
4+ "sync"
5+ "time"
6+
7+ "github.com/go-resty/resty/v2"
8+ )
9+
10+ var (
11+ defClient * resty.Client
12+ defClientOnce sync.Once
13+ )
14+
15+ func getDefClient () * resty.Client {
16+ defClientOnce .Do (func () {
17+ /*
18+ SetTimeout(10 * time.Second) — 整个请求的超时时间(含连接+读写),超过 10 秒直接报错
19+ SetRetryCount(3) — 失败时最多重试 3 次(即总共最多发 4 次请求)
20+ SetRetryWaitTime(500 * time.Millisecond) — 每次重试前的初始等待时间 500ms
21+ SetRetryMaxWaitTime(time.Second) — 重试等待时间的上限是 1 秒,防止退避时间过长
22+
23+ resty 的重试等待时间用的是指数退避策略:
24+ SetRetryWaitTime(500ms) — 第一次重试前等待的初始值
25+ SetRetryMaxWaitTime(1s) — 等待时间的上限,再怎么退避也不会超过这个值
26+
27+ 每次重试等待时间会翻倍增长,但不超过上限:
28+ 第1次重试:等 500ms
29+ 第2次重试:等 1000ms → 超过上限,截断为 1s
30+ 第3次重试:等 1s
31+ */
32+ defClient = resty .New ().SetTimeout (15 * time .Second ).
33+ SetRetryCount (3 ).
34+ SetRetryWaitTime (300 * time .Millisecond ).
35+ SetRetryMaxWaitTime (time .Second )
36+ })
37+ return defClient
38+ }
39+
40+ func Get (utl string , queryParams map [string ]string ) (int , string , error ) {
41+ client := getDefClient ()
42+
43+ resp , err := client .R ().
44+ SetQueryParams (queryParams ).
45+ Get (utl )
46+ if err != nil {
47+ return 0 , "" , err
48+ }
49+ return resp .StatusCode (), string (resp .Body ()), nil
50+ }
You can’t perform that action at this time.
0 commit comments