github.com/godaddy-x/freego@v1.0.156/rpcx/pool/options.go (about) 1 // Copyright 2019 shimingyah. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // ee the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package pool 16 17 import ( 18 "context" 19 "time" 20 21 "google.golang.org/grpc" 22 "google.golang.org/grpc/keepalive" 23 ) 24 25 const ( 26 // DialTimeout the timeout of create connection 27 DialTimeout = 5 * time.Second 28 29 // BackoffMaxDelay provided maximum delay when backing off after failed connection attempts. 30 BackoffMaxDelay = 3 * time.Second 31 32 // KeepAliveTime is the duration of time after which if the client doesn't see 33 // any activity it pings the server to see if the transport is still alive. 34 KeepAliveTime = time.Duration(7200) * time.Second 35 36 // KeepAliveTimeout is the duration of time for which the client waits after having 37 // pinged for keepalive check and if no activity is seen even after that the connection 38 // is closed. 39 KeepAliveTimeout = time.Duration(20) * time.Second 40 41 // InitialWindowSize we set it 1GB is to provide system's throughput. 42 InitialWindowSize = 1 << 30 43 44 // InitialConnWindowSize we set it 1GB is to provide system's throughput. 45 InitialConnWindowSize = 1 << 30 46 47 // MaxSendMsgSize set max gRPC request message size sent to server. 48 // If any request message size is larger than current value, an error will be reported from gRPC. 49 MaxSendMsgSize = 4 << 30 50 51 // MaxRecvMsgSize set max gRPC receive message size received from server. 52 // If any message size is larger than current value, an error will be reported from gRPC. 53 MaxRecvMsgSize = 4 << 30 54 ) 55 56 // Options are params for creating grpc connect pool. 57 type Options struct { 58 // Dial is an application supplied function for creating and configuring a connection. 59 Dial func(address string) (*grpc.ClientConn, error) 60 61 // Maximum number of idle connections in the pool. 62 MaxIdle int 63 64 // Maximum number of connections allocated by the pool at a given time. 65 // When zero, there is no limit on the number of connections in the pool. 66 MaxActive int 67 68 // MaxConcurrentStreams limit on the number of concurrent streams to each single connection 69 MaxConcurrentStreams int 70 71 // If Reuse is true and the pool is at the MaxActive limit, then Get() reuse 72 // the connection to return, If Reuse is false and the pool is at the MaxActive limit, 73 // create a one-time connection to return. 74 Reuse bool 75 } 76 77 // DefaultOptions sets a list of recommended options for good performance. 78 // Feel free to modify these to suit your needs. 79 var DefaultOptions = Options{ 80 Dial: Dial, 81 MaxIdle: 8, 82 MaxActive: 64, 83 MaxConcurrentStreams: 64, 84 Reuse: true, 85 } 86 87 // Dial return a grpc connection with defined configurations. 88 func Dial(address string) (*grpc.ClientConn, error) { 89 ctx, cancel := context.WithTimeout(context.Background(), DialTimeout) 90 defer cancel() 91 return grpc.DialContext(ctx, address, grpc.WithInsecure(), 92 grpc.WithBackoffMaxDelay(BackoffMaxDelay), 93 grpc.WithInitialWindowSize(InitialWindowSize), 94 grpc.WithInitialConnWindowSize(InitialConnWindowSize), 95 grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(MaxSendMsgSize)), 96 grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(MaxRecvMsgSize)), 97 grpc.WithKeepaliveParams(keepalive.ClientParameters{ 98 Time: KeepAliveTime, 99 Timeout: KeepAliveTimeout, 100 PermitWithoutStream: true, 101 })) 102 } 103 104 // DialTest return a simple grpc connection with defined configurations. 105 func DialTest(address string) (*grpc.ClientConn, error) { 106 ctx, cancel := context.WithTimeout(context.Background(), DialTimeout) 107 defer cancel() 108 return grpc.DialContext(ctx, address, grpc.WithInsecure()) 109 }