Skip to content

Commit

Permalink
client: set keepalive and balancer options
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 authored and CMGS committed Aug 14, 2020
1 parent abfaba5 commit c1c82a6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
38 changes: 28 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
24 changes: 24 additions & 0 deletions utils/resolver.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit c1c82a6

Please sign in to comment.