feat(gw): added connection with retry
This commit is contained in:
@@ -15,7 +15,7 @@ type AuthClient struct {
|
||||
}
|
||||
|
||||
func NewAuthClient(ctx context.Context, address string, factory *ClientFactory) (*AuthClient, error) {
|
||||
conn, err := factory.GetConnection(ctx, address)
|
||||
conn, err := factory.GetConnectionWithRetry(ctx, address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create auth client: %w", err)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ import (
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type ClientFactory struct {
|
||||
@@ -19,6 +21,11 @@ func NewClientFactory() *ClientFactory {
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
maxRetries = 3
|
||||
retryDelay = 500 * time.Millisecond
|
||||
)
|
||||
|
||||
func (f *ClientFactory) GetConnection(ctx context.Context, address string) (*grpc.ClientConn, error) {
|
||||
if conn, ok := f.connections[address]; ok {
|
||||
return conn, nil
|
||||
@@ -39,6 +46,28 @@ func (f *ClientFactory) GetConnection(ctx context.Context, address string) (*grp
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (f *ClientFactory) GetConnectionWithRetry(ctx context.Context, address string) (*grpc.ClientConn, error) {
|
||||
var conn *grpc.ClientConn
|
||||
var err error
|
||||
|
||||
for i := 0; i < maxRetries; i++ {
|
||||
conn, err = f.GetConnection(ctx, address)
|
||||
if err == nil {
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
st, ok := status.FromError(err)
|
||||
if ok && (st.Code() == codes.Unavailable || st.Code() == codes.ResourceExhausted) {
|
||||
time.Sleep(retryDelay)
|
||||
continue
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("failed to connect to %s after %d retries: %w", address, maxRetries, err)
|
||||
}
|
||||
|
||||
func (f *ClientFactory) Close() error {
|
||||
for addr, conn := range f.connections {
|
||||
if err := conn.Close(); err != nil {
|
||||
|
||||
@@ -15,7 +15,7 @@ type CompetitionClient struct {
|
||||
}
|
||||
|
||||
func NewCompetitionClient(ctx context.Context, address string, factory *ClientFactory) (*CompetitionClient, error) {
|
||||
conn, err := factory.GetConnection(ctx, address)
|
||||
conn, err := factory.GetConnectionWithRetry(ctx, address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create competition client: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user