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

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/network/transport/options.go
    14  
    15  package transport
    16  
    17  import (
    18  	"context"
    19  	"crypto/tls"
    20  	"net"
    21  	"time"
    22  
    23  	"github.com/tickoalcantara12/micro/v3/util/codec"
    24  )
    25  
    26  type Options struct {
    27  	// Addrs is the list of intermediary addresses to connect to
    28  	Addrs []string
    29  	// Codec is the codec interface to use where headers are not supported
    30  	// by the transport and the entire payload must be encoded
    31  	Codec codec.Marshaler
    32  	// Secure tells the transport to secure the connection.
    33  	// In the case TLSConfig is not specified best effort self-signed
    34  	// certs should be used
    35  	Secure bool
    36  	// TLSConfig to secure the connection. The assumption is that this
    37  	// is mTLS keypair
    38  	TLSConfig *tls.Config
    39  	// Timeout sets the timeout for Send/Recv
    40  	Timeout time.Duration
    41  	// Other options for implementations of the interface
    42  	// can be stored in a context
    43  	Context context.Context
    44  }
    45  
    46  type DialOptions struct {
    47  	// Tells the transport this is a streaming connection with
    48  	// multiple calls to send/recv and that send may not even be called
    49  	Stream bool
    50  	// Timeout for dialing
    51  	Timeout time.Duration
    52  	// Dial function
    53  	DialFunc func(addr string) (net.Conn, error)
    54  
    55  	// TODO: add tls options when dialling
    56  	// Currently set in global options
    57  
    58  	// Other options for implementations of the interface
    59  	// can be stored in a context
    60  	Context context.Context
    61  }
    62  
    63  type ListenOptions struct {
    64  	// TODO: add tls options when listening
    65  	// Currently set in global options
    66  
    67  	// Other options for implementations of the interface
    68  	// can be stored in a context
    69  	Context context.Context
    70  }
    71  
    72  // Addrs to use for transport
    73  func Addrs(addrs ...string) Option {
    74  	return func(o *Options) {
    75  		o.Addrs = addrs
    76  	}
    77  }
    78  
    79  // Codec sets the codec used for encoding where the transport
    80  // does not support message headers
    81  func Codec(c codec.Marshaler) Option {
    82  	return func(o *Options) {
    83  		o.Codec = c
    84  	}
    85  }
    86  
    87  // Timeout sets the timeout for Send/Recv execution
    88  func Timeout(t time.Duration) Option {
    89  	return func(o *Options) {
    90  		o.Timeout = t
    91  	}
    92  }
    93  
    94  // Use secure communication. If TLSConfig is not specified we
    95  // use InsecureSkipVerify and generate a self signed cert
    96  func Secure(b bool) Option {
    97  	return func(o *Options) {
    98  		o.Secure = b
    99  	}
   100  }
   101  
   102  // TLSConfig to be used for the transport.
   103  func TLSConfig(t *tls.Config) Option {
   104  	return func(o *Options) {
   105  		o.TLSConfig = t
   106  	}
   107  }
   108  
   109  // Indicates whether this is a streaming connection
   110  func WithStream() DialOption {
   111  	return func(o *DialOptions) {
   112  		o.Stream = true
   113  	}
   114  }
   115  
   116  // Timeout used when dialling the remote side
   117  func WithTimeout(d time.Duration) DialOption {
   118  	return func(o *DialOptions) {
   119  		o.Timeout = d
   120  	}
   121  }
   122  
   123  // WithDialFunc sets a dial function
   124  func WithDialFunc(fn func(addr string) (net.Conn, error)) DialOption {
   125  	return func(o *DialOptions) {
   126  		o.DialFunc = fn
   127  	}
   128  }