github.com/annwntech/go-micro/v2@v2.9.5/tunnel/options.go (about)

     1  package tunnel
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/google/uuid"
     7  	"github.com/annwntech/go-micro/v2/logger"
     8  	"github.com/annwntech/go-micro/v2/transport"
     9  	"github.com/annwntech/go-micro/v2/transport/quic"
    10  )
    11  
    12  var (
    13  	// DefaultAddress is default tunnel bind address
    14  	DefaultAddress = ":0"
    15  	// The shared default token
    16  	DefaultToken = "go.micro.tunnel"
    17  	log          = logger.NewHelper(logger.DefaultLogger).WithFields(map[string]interface{}{"service": "tunnel"})
    18  )
    19  
    20  type Option func(*Options)
    21  
    22  // Options provides network configuration options
    23  type Options struct {
    24  	// Id is tunnel id
    25  	Id string
    26  	// Address is tunnel address
    27  	Address string
    28  	// Nodes are remote nodes
    29  	Nodes []string
    30  	// The shared auth token
    31  	Token string
    32  	// Transport listens to incoming connections
    33  	Transport transport.Transport
    34  }
    35  
    36  type DialOption func(*DialOptions)
    37  
    38  type DialOptions struct {
    39  	// Link specifies the link to use
    40  	Link string
    41  	// specify mode of the session
    42  	Mode Mode
    43  	// Wait for connection to be accepted
    44  	Wait bool
    45  	// the dial timeout
    46  	Timeout time.Duration
    47  }
    48  
    49  type ListenOption func(*ListenOptions)
    50  
    51  type ListenOptions struct {
    52  	// specify mode of the session
    53  	Mode Mode
    54  	// The read timeout
    55  	Timeout time.Duration
    56  }
    57  
    58  // The tunnel id
    59  func Id(id string) Option {
    60  	return func(o *Options) {
    61  		o.Id = id
    62  	}
    63  }
    64  
    65  // The tunnel address
    66  func Address(a string) Option {
    67  	return func(o *Options) {
    68  		o.Address = a
    69  	}
    70  }
    71  
    72  // Nodes specify remote network nodes
    73  func Nodes(n ...string) Option {
    74  	return func(o *Options) {
    75  		o.Nodes = n
    76  	}
    77  }
    78  
    79  // Token sets the shared token for auth
    80  func Token(t string) Option {
    81  	return func(o *Options) {
    82  		o.Token = t
    83  	}
    84  }
    85  
    86  // Transport listens for incoming connections
    87  func Transport(t transport.Transport) Option {
    88  	return func(o *Options) {
    89  		o.Transport = t
    90  	}
    91  }
    92  
    93  // Listen options
    94  func ListenMode(m Mode) ListenOption {
    95  	return func(o *ListenOptions) {
    96  		o.Mode = m
    97  	}
    98  }
    99  
   100  // Timeout for reads and writes on the listener session
   101  func ListenTimeout(t time.Duration) ListenOption {
   102  	return func(o *ListenOptions) {
   103  		o.Timeout = t
   104  	}
   105  }
   106  
   107  // Dial options
   108  
   109  // Dial multicast sets the multicast option to send only to those mapped
   110  func DialMode(m Mode) DialOption {
   111  	return func(o *DialOptions) {
   112  		o.Mode = m
   113  	}
   114  }
   115  
   116  // DialTimeout sets the dial timeout of the connection
   117  func DialTimeout(t time.Duration) DialOption {
   118  	return func(o *DialOptions) {
   119  		o.Timeout = t
   120  	}
   121  }
   122  
   123  // DialLink specifies the link to pin this connection to.
   124  // This is not applicable if the multicast option is set.
   125  func DialLink(id string) DialOption {
   126  	return func(o *DialOptions) {
   127  		o.Link = id
   128  	}
   129  }
   130  
   131  // DialWait specifies whether to wait for the connection
   132  // to be accepted before returning the session
   133  func DialWait(b bool) DialOption {
   134  	return func(o *DialOptions) {
   135  		o.Wait = b
   136  	}
   137  }
   138  
   139  // DefaultOptions returns router default options
   140  func DefaultOptions() Options {
   141  	return Options{
   142  		Id:        uuid.New().String(),
   143  		Address:   DefaultAddress,
   144  		Token:     DefaultToken,
   145  		Transport: quic.NewTransport(),
   146  	}
   147  }