Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit df51158

Browse files
committed
update retry
1 parent 0b6f854 commit df51158

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

examples/client/retry/client.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,45 @@ package main
33
import (
44
"context"
55

6+
"github.com/stack-labs/stack-rpc"
67
proto "github.com/stack-labs/stack-rpc-tutorials/examples/proto/service/rpc"
78
"github.com/stack-labs/stack-rpc/client"
8-
"github.com/stack-labs/stack-rpc/errors"
9+
"github.com/stack-labs/stack-rpc/client/mucp"
910
log "github.com/stack-labs/stack-rpc/logger"
11+
"github.com/stack-labs/stack-rpc/util/errors"
1012
)
1113

1214
func main() {
13-
cli := client.NewClient(
14-
// 根据需要指定重试次数
15-
client.Retries(4),
16-
client.Retry(func(ctx context.Context, req client.Request, retryCount int, err error) (b bool, e error) {
17-
// 遇错重试
18-
if err != nil {
19-
// 在这里进行业务代码控制逻辑
20-
if err2, ok := err.(*errors.Error); ok {
21-
// 假设大于1000的都是业务异常
22-
if err2.Code > 1000 {
23-
log.Infof("[ERR] 请求错误,业务异常,不重试, err: %s", err)
24-
return false, nil
15+
cli := stack.NewService(
16+
stack.Client(mucp.NewClient(
17+
// 根据需要指定重试次数
18+
client.Retries(4),
19+
client.Retry(func(ctx context.Context, req client.Request, retryCount int, err error) (b bool, e error) {
20+
// 遇错重试
21+
if err != nil {
22+
// 在这里进行业务代码控制逻辑
23+
if err2, ok := err.(*errors.Error); ok {
24+
// 假设大于1000的都是业务异常
25+
if err2.Code > 1000 {
26+
log.Infof("[ERR] 请求错误,业务异常,不重试, err: %s", err)
27+
return false, nil
28+
}
2529
}
30+
31+
log.Infof("[ERR] 请求错误,第%d次重试,即将重试, err: %s", retryCount+1, err)
32+
// 返回true,则客户端会进行重试
33+
return true, nil
2634
}
2735

28-
log.Infof("[ERR] 请求错误,第%d次重试,即将重试, err: %s", retryCount, err)
29-
// 返回true,则客户端会进行重试
30-
return true, nil
31-
}
36+
// 没有错误,则返回false,意味不需要重试
37+
return false, nil
38+
}),
39+
)))
3240

33-
// 没有错误,则返回false,意味不需要重试
34-
return false, nil
35-
}),
36-
)
41+
cli.Init()
3742

3843
// 创建客户端
39-
greeter := proto.NewGreeterService("stack.rpc.greeter.retry", cli)
44+
greeter := proto.NewGreeterService("stack.rpc.greeter.retry", cli.Client())
4045

4146
// 调用greeter服务
4247
for i := 0; i < 10; i++ {

examples/client/retry/failureServer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66

77
"github.com/stack-labs/stack-rpc"
88
proto "github.com/stack-labs/stack-rpc-tutorials/examples/proto/service/rpc"
9-
"github.com/stack-labs/stack-rpc/errors"
109
log "github.com/stack-labs/stack-rpc/logger"
10+
"github.com/stack-labs/stack-rpc/util/errors"
1111
)
1212

1313
type FailureExample struct{}

0 commit comments

Comments
 (0)