github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/net/options.go (about)

     1  package net
     2  
     3  import "time"
     4  
     5  // Option is a function that will set up option.
     6  type Option func(opts *Options)
     7  
     8  func loadOptions(options ...Option) *Options {
     9  	opts := new(Options)
    10  	for _, option := range options {
    11  		option(opts)
    12  	}
    13  	return opts
    14  }
    15  
    16  // Options are set when the client opens.
    17  type Options struct {
    18  	// Multicore indicates whether the server will be effectively created with multi-cores, if so,
    19  	// then you must take care with synchronizing memory between all event callbacks, otherwise,
    20  	// it will run the server with single thread. The number of threads in the server will be automatically
    21  	// assigned to the value of runtime.NumCPU().
    22  	Multicore bool
    23  
    24  	// LB represents the load-balancing algorithm used when assigning new connections.
    25  	LB LoadBalancing
    26  
    27  	// NumEventLoop is set up to start the given number of event-loop goroutine.
    28  	// Note: Setting up NumEventLoop will override Multicore.
    29  	NumEventLoop int
    30  
    31  	// ReusePort indicates whether to set up the SO_REUSEPORT socket option.
    32  	ReusePort bool
    33  
    34  	// Ticker indicates whether the ticker has been set up.
    35  	Ticker bool
    36  
    37  	// TCPKeepAlive sets up a duration for (SO_KEEPALIVE) socket option.
    38  	TCPKeepAlive time.Duration
    39  
    40  	// ICodec encodes and decodes TCP stream.
    41  	Codec ICodec
    42  
    43  	// Logger is the customized logger for logging info, if it is not set,
    44  	// default standard logger from log package is used.
    45  	Logger Logger
    46  }
    47  
    48  // WithOptions sets up all options.
    49  func WithOptions(options Options) Option {
    50  	return func(opts *Options) {
    51  		*opts = options
    52  	}
    53  }
    54  
    55  // WithMulticore sets up multi-cores in gnet server.
    56  func WithMulticore(multicore bool) Option {
    57  	return func(opts *Options) {
    58  		opts.Multicore = multicore
    59  	}
    60  }
    61  
    62  // WithLoadBalancing sets up the load-balancing algorithm in gnet server.
    63  func WithLoadBalancing(lb LoadBalancing) Option {
    64  	return func(opts *Options) {
    65  		opts.LB = lb
    66  	}
    67  }
    68  
    69  // WithNumEventLoop sets up NumEventLoop in gnet server.
    70  func WithNumEventLoop(numEventLoop int) Option {
    71  	return func(opts *Options) {
    72  		opts.NumEventLoop = numEventLoop
    73  	}
    74  }
    75  
    76  // WithReusePort sets up SO_REUSEPORT socket option.
    77  func WithReusePort(reusePort bool) Option {
    78  	return func(opts *Options) {
    79  		opts.ReusePort = reusePort
    80  	}
    81  }
    82  
    83  // WithTCPKeepAlive sets up SO_KEEPALIVE socket option.
    84  func WithTCPKeepAlive(tcpKeepAlive time.Duration) Option {
    85  	return func(opts *Options) {
    86  		opts.TCPKeepAlive = tcpKeepAlive
    87  	}
    88  }
    89  
    90  // WithTicker indicates that a ticker is set.
    91  func WithTicker(ticker bool) Option {
    92  	return func(opts *Options) {
    93  		opts.Ticker = ticker
    94  	}
    95  }
    96  
    97  // WithCodec sets up a codec to handle TCP stream.
    98  func WithCodec(codec ICodec) Option {
    99  	return func(opts *Options) {
   100  		opts.Codec = codec
   101  	}
   102  }
   103  
   104  // WithLogger sets up a customized logger.
   105  func WithLogger(logger Logger) Option {
   106  	return func(opts *Options) {
   107  		opts.Logger = logger
   108  	}
   109  }