Skip to content

Commit 8349df3

Browse files
authored
Merge pull request #33 from KyberNetwork/ft/redis-failovercluster
feat: redis client to route randomly for sentinel redis nodes by default
2 parents f7aff30 + 5860236 commit 8349df3

File tree

8 files changed

+31
-7
lines changed

8 files changed

+31
-7
lines changed

pkg/client/backoff.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/cenkalti/backoff/v4"
55
)
66

7+
// BackoffCfg is a hotcfg to create a backoff.ExponentialBackOff
78
type BackoffCfg struct {
89
backoff.ExponentialBackOff `mapstructure:",squash"`
910
MaxRetries uint64

pkg/client/eth.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818

1919
const EthCloseDelay = time.Minute
2020

21+
// EthCfg is hotcfg for eth client. It creates a client that automatically choose to use
22+
// the provided full node rpc or archive node.
2123
type EthCfg struct {
2224
Url string
2325
ArchiveUrl string

pkg/client/eth_batchable.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/ethereum/go-ethereum/rpc"
1313
)
1414

15+
// BatchableEthCfg is hotcfg for batchable eth client.
16+
// It batches eth_call's up to be sent together within 1 request to the rpc node.
1517
type BatchableEthCfg struct {
1618
EthCfg `mapstructure:",squash"`
1719
BatchRate time.Duration

pkg/client/grpc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import (
1212

1313
const GrpcCloseDelay = time.Minute
1414

15+
// GrpcCfg is hotcfg for grpc client. On update, it
16+
// creates a new grpc client with the provided factory generated by grpc using service proto.
17+
// The client has interceptors for adding client id header, validating requests, adding timeout, metrics and tracing.
1518
type GrpcCfg[T any] struct {
1619
grpcclient.Config `mapstructure:",squash"`
1720
C T // the inner grpc client

pkg/client/grpcclient/client.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Config struct {
2828
BaseURL string
2929
MinConnectTimeout time.Duration
3030
ConnectBackoff backoff.Config
31-
IsBlockConnect bool
31+
IsBlockConnect bool // deprecated: see grpc.WithBlock
3232
GRPCCredentials credentials.TransportCredentials
3333
Insecure bool
3434
Compression Compression
@@ -66,7 +66,7 @@ func New[T any](clientFactory func(grpc.ClientConnInterface) T, applyOptions ...
6666
}
6767

6868
dialOpts := cfg.dialOptions()
69-
grpcConn, err := grpc.Dial(cfg.BaseURL, dialOpts...)
69+
grpcConn, err := grpc.NewClient(cfg.BaseURL, dialOpts...)
7070
if err != nil {
7171
return nil, err
7272
}
@@ -123,10 +123,6 @@ func (c *Config) dialOptions() []grpc.DialOption {
123123
}
124124
dialOpts = append(dialOpts, grpc.WithConnectParams(connectParams))
125125

126-
if c.IsBlockConnect {
127-
dialOpts = append(dialOpts, grpc.WithBlock())
128-
}
129-
130126
requestHeaders := c.requestHeaders()
131127
unaryInterceptors := []grpc.UnaryClientInterceptor{
132128
validator.UnaryClientInterceptor(),

pkg/client/grpcclient/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ func WithCompression(compression Compression) ApplyOption {
6464
}
6565
}
6666

67+
// WithBlock is a no-op.
68+
// deprecated: see grpc.WithBlock
6769
func WithBlock() ApplyOption {
6870
return func(c *Config) {
6971
c.IsBlockConnect = true

pkg/client/http.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
88
)
99

10+
// HttpCfg is hotcfg for a resty http client. On update, it
11+
// creates a new resty client with the new config
12+
// as well as instruments the client for metrics and tracing.
1013
type HttpCfg struct {
1114
kutils.HttpCfg `mapstructure:",squash"`
1215
C *resty.Client

pkg/client/redis.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ import (
1313

1414
const RedisCloseDelay = time.Minute
1515

16+
// RedisCfg is hotcfg for redis client. On update, it
17+
// creates FailoverClusterClient with RouteRandomly for sentinel redis by default
18+
// as well as instrumenting the client for metrics and tracing.
1619
type RedisCfg struct {
1720
redis.UniversalOptions `mapstructure:",squash"`
21+
DisableRouteRandomly bool
1822
C redis.UniversalClient
1923
}
2024

@@ -28,7 +32,8 @@ func (*RedisCfg) OnUpdate(old, new *RedisCfg) {
2832
}
2933
})
3034
}
31-
new.C = redis.NewUniversalClient(&new.UniversalOptions)
35+
new.RouteRandomly = new.RouteRandomly || !new.DisableRouteRandomly
36+
new.C = newRedisClient(&new.UniversalOptions)
3237
if metric.Provider() != nil {
3338
if err := redisotel.InstrumentMetrics(new.C); err != nil {
3439
klog.Errorf(ctx, "RedisCfg.OnUpdate|redisotel.InstrumentMetrics failed|err=%v", err)
@@ -40,3 +45,13 @@ func (*RedisCfg) OnUpdate(old, new *RedisCfg) {
4045
}
4146
}
4247
}
48+
49+
func newRedisClient(opts *redis.UniversalOptions) redis.UniversalClient {
50+
if opts.MasterName == "" {
51+
return redis.NewUniversalClient(opts)
52+
}
53+
failoverOpts := opts.Failover()
54+
failoverOpts.RouteByLatency = opts.RouteByLatency
55+
failoverOpts.RouteRandomly = opts.RouteRandomly
56+
return redis.NewFailoverClusterClient(failoverOpts)
57+
}

0 commit comments

Comments
 (0)