github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/server/options.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package server
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/m3db/m3/src/x/instrument"
    27  	xnet "github.com/m3db/m3/src/x/net"
    28  	"github.com/m3db/m3/src/x/retry"
    29  )
    30  
    31  const (
    32  	// By default keepAlives are enabled for TCP connections.
    33  	defaultTCPConnectionKeepAlive = true
    34  
    35  	// By default the keep alive period is fairly short for fast
    36  	// breaking of stale connections.
    37  	defaultTCPConnectionKeepAlivePeriod = 10 * time.Second
    38  )
    39  
    40  // Options provide a set of server options
    41  type Options interface {
    42  	// SetInstrumentOptions sets the instrument options
    43  	SetInstrumentOptions(value instrument.Options) Options
    44  
    45  	// InstrumentOptions returns the instrument options
    46  	InstrumentOptions() instrument.Options
    47  
    48  	// SetRetryOptions sets the retry options
    49  	SetRetryOptions(value retry.Options) Options
    50  
    51  	// RetryOptions returns the retry options
    52  	RetryOptions() retry.Options
    53  
    54  	// SetTCPConnectionKeepAlive sets the keep alive state for tcp connections.
    55  	SetTCPConnectionKeepAlive(value bool) Options
    56  
    57  	// TCPConnectionKeepAlive returns the keep alive state for tcp connections.
    58  	TCPConnectionKeepAlive() bool
    59  
    60  	// SetTCPConnectionKeepAlivePeriod sets the keep alive period for tcp connections.
    61  	// NB(xichen): on Linux this modifies both the idle time (i.e,. the time when the
    62  	// last packet is sent from the client and when the first keepAlive probe is sent)
    63  	// as well as the interval between keepAlive probes.
    64  	SetTCPConnectionKeepAlivePeriod(value time.Duration) Options
    65  
    66  	// TCPConnectionKeepAlivePeriod returns the keep alive period for tcp connections.
    67  	TCPConnectionKeepAlivePeriod() time.Duration
    68  
    69  	// SetListenerOptions sets the listener options for the server.
    70  	SetListenerOptions(value xnet.ListenerOptions) Options
    71  
    72  	// ListenerOptions sets the listener options for the server.
    73  	ListenerOptions() xnet.ListenerOptions
    74  }
    75  
    76  type options struct {
    77  	instrumentOpts               instrument.Options
    78  	retryOpts                    retry.Options
    79  	tcpConnectionKeepAlive       bool
    80  	tcpConnectionKeepAlivePeriod time.Duration
    81  	listenerOpts                 xnet.ListenerOptions
    82  }
    83  
    84  // NewOptions creates a new set of server options
    85  func NewOptions() Options {
    86  	return &options{
    87  		instrumentOpts:               instrument.NewOptions(),
    88  		retryOpts:                    retry.NewOptions(),
    89  		tcpConnectionKeepAlive:       defaultTCPConnectionKeepAlive,
    90  		tcpConnectionKeepAlivePeriod: defaultTCPConnectionKeepAlivePeriod,
    91  		listenerOpts:                 xnet.NewListenerOptions(),
    92  	}
    93  }
    94  
    95  func (o *options) SetInstrumentOptions(value instrument.Options) Options {
    96  	opts := *o
    97  	opts.instrumentOpts = value
    98  	return &opts
    99  }
   100  
   101  func (o *options) InstrumentOptions() instrument.Options {
   102  	return o.instrumentOpts
   103  }
   104  
   105  func (o *options) SetRetryOptions(value retry.Options) Options {
   106  	opts := *o
   107  	opts.retryOpts = value
   108  	return &opts
   109  }
   110  
   111  func (o *options) RetryOptions() retry.Options {
   112  	return o.retryOpts
   113  }
   114  
   115  func (o *options) SetTCPConnectionKeepAlive(value bool) Options {
   116  	opts := *o
   117  	opts.tcpConnectionKeepAlive = value
   118  	return &opts
   119  }
   120  
   121  func (o *options) TCPConnectionKeepAlive() bool {
   122  	return o.tcpConnectionKeepAlive
   123  }
   124  
   125  func (o *options) SetTCPConnectionKeepAlivePeriod(value time.Duration) Options {
   126  	opts := *o
   127  	opts.tcpConnectionKeepAlivePeriod = value
   128  	return &opts
   129  }
   130  
   131  func (o *options) TCPConnectionKeepAlivePeriod() time.Duration {
   132  	return o.tcpConnectionKeepAlivePeriod
   133  }
   134  
   135  func (o *options) SetListenerOptions(value xnet.ListenerOptions) Options {
   136  	opts := *o
   137  	opts.listenerOpts = value
   138  	return &opts
   139  }
   140  
   141  func (o *options) ListenerOptions() xnet.ListenerOptions {
   142  	return o.listenerOpts
   143  }