github.com/TeaOSLab/EdgeNode@v1.3.8/internal/rpc/rpc_utils.go (about)

     1  package rpc
     2  
     3  import (
     4  	"github.com/TeaOSLab/EdgeNode/internal/configs"
     5  	"google.golang.org/grpc/codes"
     6  	"google.golang.org/grpc/status"
     7  	"strings"
     8  	"sync"
     9  )
    10  
    11  var sharedRPC *RPCClient = nil
    12  var locker = &sync.Mutex{}
    13  
    14  // SharedRPC RPC对象
    15  func SharedRPC() (*RPCClient, error) {
    16  	locker.Lock()
    17  	defer locker.Unlock()
    18  
    19  	if sharedRPC != nil {
    20  		return sharedRPC, nil
    21  	}
    22  
    23  	config, err := configs.LoadAPIConfig()
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  	client, err := NewRPCClient(config)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	sharedRPC = client
    33  	return sharedRPC, nil
    34  }
    35  
    36  // IsConnError 是否为连接错误
    37  func IsConnError(err error) bool {
    38  	if err == nil {
    39  		return false
    40  	}
    41  
    42  	// 检查是否为连接错误
    43  	statusErr, ok := status.FromError(err)
    44  	if ok {
    45  		var errorCode = statusErr.Code()
    46  		return errorCode == codes.Unavailable || errorCode == codes.Canceled
    47  	}
    48  
    49  	if strings.Contains(err.Error(), "code = Canceled") {
    50  		return true
    51  	}
    52  
    53  	return false
    54  }