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