github.com/micro/go-micro/v2@v2.9.1/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  	"github.com/micro/go-micro/v2/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  //
    86  // MaxRecvMsgSize set the maximum size of message that client can receive.
    87  //
    88  func MaxRecvMsgSize(s int) client.Option {
    89  	return func(o *client.Options) {
    90  		if o.Context == nil {
    91  			o.Context = context.Background()
    92  		}
    93  		o.Context = context.WithValue(o.Context, maxRecvMsgSizeKey{}, s)
    94  	}
    95  }
    96  
    97  //
    98  // MaxSendMsgSize set the maximum size of message that client can send.
    99  //
   100  func MaxSendMsgSize(s int) client.Option {
   101  	return func(o *client.Options) {
   102  		if o.Context == nil {
   103  			o.Context = context.Background()
   104  		}
   105  		o.Context = context.WithValue(o.Context, maxSendMsgSizeKey{}, s)
   106  	}
   107  }
   108  
   109  //
   110  // DialOptions to be used to configure gRPC dial options
   111  //
   112  func DialOptions(opts ...grpc.DialOption) client.CallOption {
   113  	return func(o *client.CallOptions) {
   114  		if o.Context == nil {
   115  			o.Context = context.Background()
   116  		}
   117  		o.Context = context.WithValue(o.Context, grpcDialOptions{}, opts)
   118  	}
   119  }
   120  
   121  //
   122  // CallOptions to be used to configure gRPC call options
   123  //
   124  func CallOptions(opts ...grpc.CallOption) client.CallOption {
   125  	return func(o *client.CallOptions) {
   126  		if o.Context == nil {
   127  			o.Context = context.Background()
   128  		}
   129  		o.Context = context.WithValue(o.Context, grpcCallOptions{}, opts)
   130  	}
   131  }