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

     1  package transport
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"net"
     7  	"time"
     8  
     9  	"go-micro.dev/v5/codec"
    10  	"go-micro.dev/v5/logger"
    11  )
    12  
    13  var (
    14  	DefaultBufSizeH2 = 4 * 1024 * 1024
    15  )
    16  
    17  type Options struct {
    18  	// Codec is the codec interface to use where headers are not supported
    19  	// by the transport and the entire payload must be encoded
    20  	Codec codec.Marshaler
    21  	// Other options for implementations of the interface
    22  	// can be stored in a context
    23  	Context context.Context
    24  	// Logger is the underline logger
    25  	Logger logger.Logger
    26  	// TLSConfig to secure the connection. The assumption is that this
    27  	// is mTLS keypair
    28  	TLSConfig *tls.Config
    29  	// Addrs is the list of intermediary addresses to connect to
    30  	Addrs []string
    31  	// Timeout sets the timeout for Send/Recv
    32  	Timeout time.Duration
    33  	// BuffSizeH2 is the HTTP2 buffer size
    34  	BuffSizeH2 int
    35  	// Secure tells the transport to secure the connection.
    36  	// In the case TLSConfig is not specified best effort self-signed
    37  	// certs should be used
    38  	Secure bool
    39  }
    40  
    41  type DialOptions struct {
    42  
    43  	// TODO: add tls options when dialing
    44  	// Currently set in global options
    45  
    46  	// Other options for implementations of the interface
    47  	// can be stored in a context
    48  	Context context.Context
    49  	// Timeout for dialing
    50  	Timeout time.Duration
    51  	// Tells the transport this is a streaming connection with
    52  	// multiple calls to send/recv and that send may not even be called
    53  	Stream bool
    54  	// ConnClose sets the Connection header to close
    55  	ConnClose bool
    56  	// InsecureSkipVerify skip TLS verification.
    57  	InsecureSkipVerify bool
    58  }
    59  
    60  type ListenOptions struct {
    61  	// TODO: add tls options when listening
    62  	// Currently set in global options
    63  
    64  	// Other options for implementations of the interface
    65  	// can be stored in a context
    66  	Context context.Context
    67  }
    68  
    69  // Addrs to use for transport.
    70  func Addrs(addrs ...string) Option {
    71  	return func(o *Options) {
    72  		o.Addrs = addrs
    73  	}
    74  }
    75  
    76  // Codec sets the codec used for encoding where the transport
    77  // does not support message headers.
    78  func Codec(c codec.Marshaler) Option {
    79  	return func(o *Options) {
    80  		o.Codec = c
    81  	}
    82  }
    83  
    84  // Timeout sets the timeout for Send/Recv execution.
    85  func Timeout(t time.Duration) Option {
    86  	return func(o *Options) {
    87  		o.Timeout = t
    88  	}
    89  }
    90  
    91  // Use secure communication. If TLSConfig is not specified we
    92  // use InsecureSkipVerify and generate a self signed cert.
    93  func Secure(b bool) Option {
    94  	return func(o *Options) {
    95  		o.Secure = b
    96  	}
    97  }
    98  
    99  // TLSConfig to be used for the transport.
   100  func TLSConfig(t *tls.Config) Option {
   101  	return func(o *Options) {
   102  		o.TLSConfig = t
   103  	}
   104  }
   105  
   106  // Indicates whether this is a streaming connection.
   107  func WithStream() DialOption {
   108  	return func(o *DialOptions) {
   109  		o.Stream = true
   110  	}
   111  }
   112  
   113  func WithTimeout(d time.Duration) DialOption {
   114  	return func(o *DialOptions) {
   115  		o.Timeout = d
   116  	}
   117  }
   118  
   119  // WithConnClose sets the Connection header to close.
   120  func WithConnClose() DialOption {
   121  	return func(o *DialOptions) {
   122  		o.ConnClose = true
   123  	}
   124  }
   125  
   126  func WithInsecureSkipVerify(b bool) DialOption {
   127  	return func(o *DialOptions) {
   128  		o.InsecureSkipVerify = b
   129  	}
   130  }
   131  
   132  // Logger sets the underline logger.
   133  func Logger(l logger.Logger) Option {
   134  	return func(o *Options) {
   135  		o.Logger = l
   136  	}
   137  }
   138  
   139  // BuffSizeH2 sets the HTTP2 buffer size.
   140  // Default is 4 * 1024 * 1024.
   141  func BuffSizeH2(size int) Option {
   142  	return func(o *Options) {
   143  		o.BuffSizeH2 = size
   144  	}
   145  }
   146  
   147  // InsecureSkipVerify sets the TLS options to skip verification.
   148  // NetListener Set net.Listener for httpTransport.
   149  func NetListener(customListener net.Listener) ListenOption {
   150  	return func(o *ListenOptions) {
   151  		if customListener == nil {
   152  			return
   153  		}
   154  
   155  		if o.Context == nil {
   156  			o.Context = context.TODO()
   157  		}
   158  
   159  		o.Context = context.WithValue(o.Context, netListener{}, customListener)
   160  	}
   161  }