package node import ( "strings" "time" grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry" "github.com/prometheus/common/log" "google.golang.org/grpc" "google.golang.org/grpc/credentials" ) // splitAndTrimEmpty slices s into all subslices separated by sep and returns a // slice of the string s with all leading and trailing Unicode code points // contained in cutset removed. If sep is empty, SplitAndTrim splits after each // UTF-8 sequence. First part is equivalent to strings.SplitN with a count of // -1. also filter out empty strings, only return non-empty strings. func splitAndTrimEmpty(s, sep, cutset string) []string { if s == "" { return []string{} } spl := strings.Split(s, sep) nonEmptyStrings := make([]string, 0, len(spl)) for i := 0; i < len(spl); i++ { element := strings.Trim(spl[i], cutset) if element != "" { nonEmptyStrings = append(nonEmptyStrings, element) } } return nonEmptyStrings } // ConstructDialOptions constructs a list of grpc dial options func ConstructDialOptions( withCert string, extraOpts ...grpc.DialOption, ) []grpc.DialOption { var transportSecurity grpc.DialOption if withCert != "" { creds, err := credentials.NewClientTLSFromFile(withCert, "") if err != nil { log.Errorf("Could not get valid credentials: %v", err) return nil } transportSecurity = grpc.WithTransportCredentials(creds) } else { transportSecurity = grpc.WithInsecure() log.Warn("You are using an insecure gRPC connection! Please provide a certificate and key to use a secure connection.") } const ( retries = 50 // 50 * 100ms = 5s total timeout = 100 * time.Millisecond maxCallRecvMsgSize = 10 << 20 // Default 10Mb ) opts := []grpc_retry.CallOption{ grpc_retry.WithBackoff(grpc_retry.BackoffExponential(timeout)), } dialOpts := []grpc.DialOption{ transportSecurity, grpc.WithDefaultCallOptions( grpc.MaxCallRecvMsgSize(maxCallRecvMsgSize), grpc_retry.WithMax(retries), ), grpc.WithUnaryInterceptor( grpc_retry.UnaryClientInterceptor(opts...), ), } for _, opt := range extraOpts { dialOpts = append(dialOpts, opt) } return dialOpts }