go-micro.dev/v5@v5.12.0/client/grpc/options.go (about)

     1  // Package grpc provides a gRPC options
     2  package grpc
     3  
     4  import (
     5  	"context"
     6  	"crypto/tls"
     7  
     8  	"go-micro.dev/v5/client"
     9  	"google.golang.org/grpc"
    10  	"google.golang.org/grpc/encoding"
    11  )
    12  
    13  var (
    14  	// DefaultPoolMaxStreams maximum streams on a connectioin
    15  	// (20).
    16  	DefaultPoolMaxStreams = 20
    17  
    18  	// DefaultPoolMaxIdle maximum idle conns of a pool
    19  	// (50).
    20  	DefaultPoolMaxIdle = 50
    21  
    22  	// DefaultMaxRecvMsgSize maximum message that client can receive
    23  	// (4 MB).
    24  	DefaultMaxRecvMsgSize = 1024 * 1024 * 4
    25  
    26  	// DefaultMaxSendMsgSize maximum message that client can send
    27  	// (4 MB).
    28  	DefaultMaxSendMsgSize = 1024 * 1024 * 4
    29  )
    30  
    31  type poolMaxStreams struct{}
    32  type poolMaxIdle struct{}
    33  type codecsKey struct{}
    34  type tlsAuth struct{}
    35  type maxRecvMsgSizeKey struct{}
    36  type maxSendMsgSizeKey struct{}
    37  type grpcDialOptions struct{}
    38  type grpcCallOptions struct{}
    39  
    40  // maximum streams on a connectioin.
    41  func PoolMaxStreams(n int) client.Option {
    42  	return func(o *client.Options) {
    43  		if o.Context == nil {
    44  			o.Context = context.Background()
    45  		}
    46  		o.Context = context.WithValue(o.Context, poolMaxStreams{}, n)
    47  	}
    48  }
    49  
    50  // maximum idle conns of a pool.
    51  func PoolMaxIdle(d int) client.Option {
    52  	return func(o *client.Options) {
    53  		if o.Context == nil {
    54  			o.Context = context.Background()
    55  		}
    56  		o.Context = context.WithValue(o.Context, poolMaxIdle{}, d)
    57  	}
    58  }
    59  
    60  // gRPC Codec to be used to encode/decode requests for a given content type.
    61  func Codec(contentType string, c encoding.Codec) client.Option {
    62  	return func(o *client.Options) {
    63  		codecs := make(map[string]encoding.Codec)
    64  		if o.Context == nil {
    65  			o.Context = context.Background()
    66  		}
    67  		if v := o.Context.Value(codecsKey{}); v != nil {
    68  			codecs = v.(map[string]encoding.Codec)
    69  		}
    70  		codecs[contentType] = c
    71  		o.Context = context.WithValue(o.Context, codecsKey{}, codecs)
    72  	}
    73  }
    74  
    75  // AuthTLS should be used to setup a secure authentication using TLS.
    76  func AuthTLS(t *tls.Config) client.Option {
    77  	return func(o *client.Options) {
    78  		if o.Context == nil {
    79  			o.Context = context.Background()
    80  		}
    81  		o.Context = context.WithValue(o.Context, tlsAuth{}, t)
    82  	}
    83  }
    84  
    85  // MaxRecvMsgSize set the maximum size of message that client can receive.
    86  func MaxRecvMsgSize(s int) client.Option {
    87  	return func(o *client.Options) {
    88  		if o.Context == nil {
    89  			o.Context = context.Background()
    90  		}
    91  		o.Context = context.WithValue(o.Context, maxRecvMsgSizeKey{}, s)
    92  	}
    93  }
    94  
    95  // MaxSendMsgSize set the maximum size of message that client can send.
    96  func MaxSendMsgSize(s int) client.Option {
    97  	return func(o *client.Options) {
    98  		if o.Context == nil {
    99  			o.Context = context.Background()
   100  		}
   101  		o.Context = context.WithValue(o.Context, maxSendMsgSizeKey{}, s)
   102  	}
   103  }
   104  
   105  // DialOptions to be used to configure gRPC dial options.
   106  func DialOptions(opts ...grpc.DialOption) client.CallOption {
   107  	return func(o *client.CallOptions) {
   108  		if o.Context == nil {
   109  			o.Context = context.Background()
   110  		}
   111  		o.Context = context.WithValue(o.Context, grpcDialOptions{}, opts)
   112  	}
   113  }
   114  
   115  // CallOptions to be used to configure gRPC call options.
   116  func CallOptions(opts ...grpc.CallOption) client.CallOption {
   117  	return func(o *client.CallOptions) {
   118  		if o.Context == nil {
   119  			o.Context = context.Background()
   120  		}
   121  		o.Context = context.WithValue(o.Context, grpcCallOptions{}, opts)
   122  	}
   123  }
   124  
   125  func callOpts(opts client.CallOptions) []grpc.CallOption {
   126  	if opts.Context == nil {
   127  		return nil
   128  	}
   129  
   130  	v := opts.Context.Value(grpcCallOptions{})
   131  
   132  	if v == nil {
   133  		return nil
   134  	}
   135  
   136  	options, ok := v.([]grpc.CallOption)
   137  
   138  	if !ok {
   139  		return nil
   140  	}
   141  
   142  	return options
   143  }