github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/pkg/client/grpc/options.go (about)

     1  /*
     2  Copyright [2014] - [2023] The Last.Backend authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package grpc
    18  
    19  import (
    20  	"github.com/lastbackend/toolkit/pkg/client"
    21  	"github.com/lastbackend/toolkit/pkg/client/grpc/selector"
    22  	"github.com/lastbackend/toolkit/pkg/util/converter"
    23  
    24  	"context"
    25  	"math"
    26  	"time"
    27  )
    28  
    29  func exponentialBackoff(ctx context.Context, req *client.GRPCRequest, attempts int) (time.Duration, error) {
    30  	if attempts > 10 {
    31  		return 2 * time.Minute, nil
    32  	}
    33  	return time.Duration(math.Pow(float64(attempts), math.E)) * time.Millisecond * 100, nil
    34  }
    35  
    36  type CallFunc func(ctx context.Context, addr string, req *client.GRPCRequest, rsp interface{}, opts client.GRPCCallOptions) error
    37  type CallMiddlewareFunc func(CallFunc) CallFunc
    38  type MiddlewareFunc func(client client.GRPCClient) client.GRPCClient
    39  type LookupFunc func(context.Context, *client.GRPCRequest, client.GRPCCallOptions) ([]string, error)
    40  
    41  type Options struct {
    42  	Context context.Context
    43  
    44  	ContentType string `env:"CONTENT_TYPE"  envDefault:"application/protobuf" comment:"Set GRPC client request content-type"`
    45  
    46  	// Grpc DialOptions
    47  	WriteBufferSize       *int    `env:"WRITE_BUFFER_SIZE" comment:"Sets the how much data can be batched before doing a write on the wire. The corresponding memory allocation for this buffer will be twice the size to keep syscalls low. Zero will disable the write buffer (default 32 KB)"`
    48  	ReadBufferSize        *int    `env:"READ_BUFFER_SIZE" comment:"Sets the size of the reading buffer, this determines how\nmuch data can be read at most for each read syscall. Zero will disable read buffer (default 32 KB)")"`
    49  	InitialWindowSize     *int32  `env:"INITIAL_WINDOW_SIZE" comment:"Sets the value for initial window size on a stream. The lower bound for window size is 64K and any value smaller than that will be ignored."`
    50  	InitialConnWindowSize *int32  `env:"INITIAL_CONN_WINDOW_SIZE" comment:"Sets the value for initial window size on a connection. The lower bound for window size is 64K and any value smaller than that will be ignored."`
    51  	MaxHeaderListSize     *int32  `env:"MAX_HEADER_LIST_SIZE" comment:"Sets the specifies the maximum (uncompressed) size of header list that the client is prepared to accept"`
    52  	MaxRecvMsgSize        *int    `env:"MAX_RECV_MSG_SIZE" comment:"Sets the maximum message size in bytes the client can receive (default 16 MB)"`
    53  	MaxSendMsgSize        *int    `env:"MAX_SEND_MSG_SIZE" comment:"Sets the maximum message size in bytes the client can send (default 16 MB)"`
    54  	UserAgent             *string `env:"USER_AGENT"  envDefault:"application/protobuf" comment:"Sets the specifies a user agent string for all the RPCs"`
    55  	Resolver              string  `env:"RESOLVER" envDefault:"local" comment:"Define resolver used as service registry [local, file, plugin]. "`
    56  
    57  	Selector    selector.Selector
    58  	Pool        PoolOptions
    59  	CallOptions client.GRPCCallOptions
    60  }
    61  
    62  func defaultOptions() Options {
    63  	slc, _ := selector.New(selector.RoundRobin)
    64  	return Options{
    65  		Context:     context.Background(),
    66  		ContentType: "application/protobuf",
    67  		Selector:    slc,
    68  		CallOptions: client.GRPCCallOptions{
    69  			Backoff:        exponentialBackoff,
    70  			Retries:        defaultRetries,
    71  			RequestTimeout: defaultRequestTimeout,
    72  		},
    73  		Pool: PoolOptions{
    74  			Size: converter.ToIntPointer(defaultPoolSize),
    75  			TTL:  converter.ToDurationPointer(defaultPoolTTL),
    76  		},
    77  	}
    78  }