aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2021-05-30 00:07:43 -0700
committerChris Lu <chris.lu@gmail.com>2021-05-30 00:07:43 -0700
commit1456616a77c8fdb2e3a087645922da9bea388aa1 (patch)
tree6efc2406d59d1a8780529fb15357289fb4720764
parentfb8036385ab71c8277a554aff06f937f2e5ab6d6 (diff)
downloadseaweedfs-1456616a77c8fdb2e3a087645922da9bea388aa1.tar.xz
seaweedfs-1456616a77c8fdb2e3a087645922da9bea388aa1.zip
recreate grpc connections if too many errors
address https://github.com/chrislusf/seaweedfs/issues/2098
-rw-r--r--weed/pb/grpc_client_server.go23
1 files changed, 15 insertions, 8 deletions
diff --git a/weed/pb/grpc_client_server.go b/weed/pb/grpc_client_server.go
index edb60e4fa..5490339b3 100644
--- a/weed/pb/grpc_client_server.go
+++ b/weed/pb/grpc_client_server.go
@@ -31,7 +31,8 @@ var (
type versionedGrpcClient struct {
*grpc.ClientConn
- version int
+ version int
+ errCount int
}
func init() {
@@ -103,6 +104,7 @@ func getOrCreateConnection(address string, opts ...grpc.DialOption) (*versionedG
vgc := &versionedGrpcClient{
grpcConnection,
rand.Int(),
+ 0,
}
grpcClients[address] = vgc
@@ -116,15 +118,20 @@ func WithCachedGrpcClient(fn func(*grpc.ClientConn) error, address string, opts
return fmt.Errorf("getOrCreateConnection %s: %v", address, err)
}
executionErr := fn(vgc.ClientConn)
- if executionErr != nil && strings.Contains(executionErr.Error(), "transport") {
- grpcClientsLock.Lock()
- if t, ok := grpcClients[address]; ok {
- if t.version == vgc.version {
- vgc.Close()
- delete(grpcClients, address)
+ if executionErr != nil {
+ vgc.errCount++
+ if vgc.errCount > 3 ||
+ strings.Contains(executionErr.Error(), "transport") ||
+ strings.Contains(executionErr.Error(), "connection closed") {
+ grpcClientsLock.Lock()
+ if t, ok := grpcClients[address]; ok {
+ if t.version == vgc.version {
+ vgc.Close()
+ delete(grpcClients, address)
+ }
}
+ grpcClientsLock.Unlock()
}
- grpcClientsLock.Unlock()
}
return executionErr