github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/client/grpc/options.go (about)

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