Skip to content

Commit

Permalink
Merge pull request #53 from RealFax/dev
Browse files Browse the repository at this point in the history
fixed client `too_many_pings` error
  • Loading branch information
PotatoCloud authored Aug 9, 2023
2 parents 68ce80f + 66fbdfe commit c26630c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
9 changes: 6 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package client

import "context"
import (
"context"
"google.golang.org/grpc"
)

type Client struct {
InternalClient
Expand All @@ -18,7 +21,7 @@ func (c *Client) Close() error {
return c.conn.Close()
}

func New(ctx context.Context, endpoints []string, syncConn bool) (*Client, error) {
func New(ctx context.Context, endpoints []string, opts ...grpc.DialOption) (*Client, error) {
var (
err error
client = &Client{
Expand All @@ -28,7 +31,7 @@ func New(ctx context.Context, endpoints []string, syncConn bool) (*Client, error

client.ctx, client.cancelFunc = context.WithCancel(ctx)

if client.conn, err = NewClientConn(ctx, endpoints, syncConn); err != nil {
if client.conn, err = NewClientConn(ctx, endpoints, opts...); err != nil {
return nil, err
}

Expand Down
25 changes: 4 additions & 21 deletions client/t_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ package client
import (
"context"
"crypto/rand"
"github.com/RealFax/RedQueen/api/serverpb"
"github.com/pkg/errors"
"google.golang.org/grpc"
"math/big"
"sync"
"sync/atomic"
"time"

"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"

"github.com/RealFax/RedQueen/api/serverpb"
)

type Conn interface {
Expand Down Expand Up @@ -143,7 +138,7 @@ func (c *clientConn) Close() error {
return nil
}

func NewClientConn(ctx context.Context, endpoints []string, syncConn bool) (Conn, error) {
func NewClientConn(ctx context.Context, endpoints []string, opts ...grpc.DialOption) (Conn, error) {
cc := &clientConn{
state: atomic.Bool{},
ctx: ctx,
Expand All @@ -156,20 +151,8 @@ func NewClientConn(ctx context.Context, endpoints []string, syncConn bool) (Conn
var (
err error
conn *grpc.ClientConn
opts = []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: time.Second * 3,
Timeout: time.Millisecond * 100,
PermitWithoutStream: true,
}),
}
)

if syncConn {
opts = append(opts, grpc.WithBlock())
}

// init
for _, endpoint := range endpoints {
if conn, err = grpc.DialContext(ctx, endpoint, opts...); err != nil {
Expand Down

0 comments on commit c26630c

Please sign in to comment.