diff --git a/client/client.go b/client/client.go index fb9413708..979715b07 100644 --- a/client/client.go +++ b/client/client.go @@ -1,11 +1,20 @@ package client import ( + "context" + "time" + + log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/auth" + "github.com/projecteru2/core/client/interceptor" + _ "github.com/projecteru2/core/client/resolver/eru" + _ "github.com/projecteru2/core/client/resolver/static" pb "github.com/projecteru2/core/rpc/gen" "github.com/projecteru2/core/types" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/utils" "google.golang.org/grpc" + "google.golang.org/grpc/keepalive" ) // Client contain grpc conn @@ -15,9 +24,12 @@ type Client struct { } // NewClient new a client -func NewClient(addr string, authConfig types.AuthConfig) *Client { - conn := connect(addr, authConfig) - return &Client{addr: addr, conn: conn} +func NewClient(ctx context.Context, addr string, authConfig types.AuthConfig) *Client { + client := &Client{ + addr: addr, + conn: dial(ctx, addr, authConfig), + } + return client } // GetConn return connection @@ -30,16 +42,22 @@ func (c *Client) GetRPCClient() pb.CoreRPCClient { return pb.NewCoreRPCClient(c.conn) } -func connect(addr string, authConfig types.AuthConfig) *grpc.ClientConn { - opts := []grpc.DialOption{grpc.WithInsecure()} +func dial(ctx context.Context, addr string, authConfig types.AuthConfig) *grpc.ClientConn { + opts := []grpc.DialOption{ + grpc.WithInsecure(), + grpc.WithKeepaliveParams(keepalive.ClientParameters{Time: 6 * 60 * time.Second, Timeout: time.Second}), + grpc.WithBalancerName("round_robin"), + grpc.WithUnaryInterceptor(interceptor.NewUnaryRetry(interceptor.RetryOptions{Max: 1})), + grpc.WithStreamInterceptor(interceptor.NewStreamRetry(interceptor.RetryOptions{Max: 1})), + } if authConfig.Username != "" { opts = append(opts, grpc.WithPerRPCCredentials(auth.NewCredential(authConfig))) } - conn, err := grpc.Dial(addr, opts...) + target := utils.MakeTarget(addr) + cc, err := grpc.DialContext(ctx, target, opts...) if err != nil { - log.Fatalf("[ConnectEru] Can not connect %v", err) + log.Panicf("[NewClient] failed to dial grpc %s: %v", addr, err) } - log.Debugf("[ConnectEru] Init eru connection %s", addr) - return conn + return cc } diff --git a/go.mod b/go.mod index da5428687..eb421e8fb 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/Microsoft/go-winio v0.4.5 // indirect github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect github.com/alexcesaro/statsd v2.0.0+incompatible // indirect + github.com/cenkalti/backoff/v4 v4.0.2 github.com/containerd/continuity v0.0.0-20180612233548-246e49050efd // indirect github.com/docker/distribution v0.0.0-20171207180435-f4118485915a github.com/docker/docker v0.0.0-20181112142024-a5e2dd2bb141 diff --git a/go.sum b/go.sum index a541dcc8f..96085a7ad 100644 --- a/go.sum +++ b/go.sum @@ -34,6 +34,10 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/cenkalti/backoff v1.1.0 h1:QnvVp8ikKCDWOsFheytRCoYWYPO/ObCTBGxT19Hc+yE= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= +github.com/cenkalti/backoff/v4 v4.0.2 h1:JIufpQLbh4DkbQoii76ItQIUFzevQSqOLZca4eamEDs= +github.com/cenkalti/backoff/v4 v4.0.2/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= diff --git a/utils/resolver.go b/utils/resolver.go new file mode 100644 index 000000000..99db8e1f0 --- /dev/null +++ b/utils/resolver.go @@ -0,0 +1,24 @@ +package utils + +import ( + "fmt" + "strings" +) + +var legitSchemes map[string]string = map[string]string{ + "eru": "eru://", + "static": "static://", +} + +func MakeTarget(addr string) string { + scheme := "eru" + for s, prefix := range legitSchemes { + if strings.HasPrefix(addr, prefix) { + scheme = s + addr = strings.TrimPrefix(addr, prefix) + break + } + } + + return fmt.Sprintf("%s://_/%s", scheme, addr) +}