初始化
()
| 230 | |
| 231 | // 初始化 |
| 232 | func (this *RPCClient) init() error { |
| 233 | // 重新连接 |
| 234 | var conns = []*grpc.ClientConn{} |
| 235 | for _, endpoint := range this.apiConfig.RPCEndpoints { |
| 236 | u, err := url.Parse(endpoint) |
| 237 | if err != nil { |
| 238 | return fmt.Errorf("parse endpoint failed: %w", err) |
| 239 | } |
| 240 | var conn *grpc.ClientConn |
| 241 | var callOptions = grpc.WithDefaultCallOptions( |
| 242 | grpc.MaxCallRecvMsgSize(512<<20), |
| 243 | grpc.MaxCallSendMsgSize(512<<20), |
| 244 | grpc.UseCompressor(gzip.Name), |
| 245 | ) |
| 246 | var keepaliveParams = grpc.WithKeepaliveParams(keepalive.ClientParameters{ |
| 247 | Time: 30 * time.Second, |
| 248 | }) |
| 249 | if u.Scheme == "http" { |
| 250 | conn, err = grpc.Dial(u.Host, grpc.WithTransportCredentials(insecure.NewCredentials()), callOptions, keepaliveParams) |
| 251 | } else if u.Scheme == "https" { |
| 252 | conn, err = grpc.Dial(u.Host, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{ |
| 253 | InsecureSkipVerify: true, |
| 254 | })), callOptions, keepaliveParams) |
| 255 | } else { |
| 256 | return errors.New("parse endpoint failed: invalid scheme '" + u.Scheme + "'") |
| 257 | } |
| 258 | if err != nil { |
| 259 | return err |
| 260 | } |
| 261 | conns = append(conns, conn) |
| 262 | } |
| 263 | if len(conns) == 0 { |
| 264 | return errors.New("[RPC]no available endpoints") |
| 265 | } |
| 266 | |
| 267 | // 这里不需要加锁,防止和pickConn()冲突 |
| 268 | this.conns = conns |
| 269 | return nil |
| 270 | } |
| 271 | |
| 272 | // 随机选择一个连接 |
| 273 | func (this *RPCClient) pickConn() *grpc.ClientConn { |
no test coverage detected