github.com/nsqio/nsq@v1.3.0/apps/nsqd/options.go (about)

     1  package main
     2  
     3  import (
     4  	"crypto/tls"
     5  	"flag"
     6  	"fmt"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/nsqio/nsq/internal/app"
    11  	"github.com/nsqio/nsq/internal/lg"
    12  	"github.com/nsqio/nsq/nsqd"
    13  )
    14  
    15  type tlsRequiredOption int
    16  
    17  func (t *tlsRequiredOption) Set(s string) error {
    18  	s = strings.ToLower(s)
    19  	if s == "tcp-https" {
    20  		*t = nsqd.TLSRequiredExceptHTTP
    21  		return nil
    22  	}
    23  	required, err := strconv.ParseBool(s)
    24  	if required {
    25  		*t = nsqd.TLSRequired
    26  	} else {
    27  		*t = nsqd.TLSNotRequired
    28  	}
    29  	return err
    30  }
    31  
    32  func (t *tlsRequiredOption) Get() interface{} { return int(*t) }
    33  
    34  func (t *tlsRequiredOption) String() string {
    35  	return strconv.FormatInt(int64(*t), 10)
    36  }
    37  
    38  func (t *tlsRequiredOption) IsBoolFlag() bool { return true }
    39  
    40  type tlsMinVersionOption uint16
    41  
    42  var tlsVersionTable = []struct {
    43  	val uint16
    44  	str string
    45  }{
    46  	{tls.VersionTLS10, "tls1.0"},
    47  	{tls.VersionTLS11, "tls1.1"},
    48  	{tls.VersionTLS12, "tls1.2"},
    49  	{tls.VersionTLS13, "tls1.3"},
    50  }
    51  
    52  func (t *tlsMinVersionOption) Set(s string) error {
    53  	s = strings.ToLower(s)
    54  	if s == "" {
    55  		return nil
    56  	}
    57  	for _, v := range tlsVersionTable {
    58  		if s == v.str {
    59  			*t = tlsMinVersionOption(v.val)
    60  			return nil
    61  		}
    62  	}
    63  	return fmt.Errorf("unknown tlsVersionOption %q", s)
    64  }
    65  
    66  func (t *tlsMinVersionOption) Get() interface{} { return uint16(*t) }
    67  
    68  func (t *tlsMinVersionOption) String() string {
    69  	for _, v := range tlsVersionTable {
    70  		if uint16(*t) == v.val {
    71  			return v.str
    72  		}
    73  	}
    74  	return strconv.FormatInt(int64(*t), 10)
    75  }
    76  
    77  type config map[string]interface{}
    78  
    79  // Validate settings in the config file, and fatal on errors
    80  func (cfg config) Validate() {
    81  	// special validation/translation
    82  	if v, exists := cfg["tls_required"]; exists {
    83  		var t tlsRequiredOption
    84  		err := t.Set(fmt.Sprintf("%v", v))
    85  		if err == nil {
    86  			cfg["tls_required"] = t.String()
    87  		} else {
    88  			logFatal("failed parsing tls_required %+v", v)
    89  		}
    90  	}
    91  	if v, exists := cfg["tls_min_version"]; exists {
    92  		var t tlsMinVersionOption
    93  		err := t.Set(fmt.Sprintf("%v", v))
    94  		if err == nil {
    95  			newVal := fmt.Sprintf("%v", t.Get())
    96  			if newVal != "0" {
    97  				cfg["tls_min_version"] = newVal
    98  			} else {
    99  				delete(cfg, "tls_min_version")
   100  			}
   101  		} else {
   102  			logFatal("failed parsing tls_min_version %+v", v)
   103  		}
   104  	}
   105  	if v, exists := cfg["log_level"]; exists {
   106  		var t lg.LogLevel
   107  		err := t.Set(fmt.Sprintf("%v", v))
   108  		if err == nil {
   109  			cfg["log_level"] = t
   110  		} else {
   111  			logFatal("failed parsing log_level %+v", v)
   112  		}
   113  	}
   114  }
   115  
   116  func nsqdFlagSet(opts *nsqd.Options) *flag.FlagSet {
   117  	flagSet := flag.NewFlagSet("nsqd", flag.ExitOnError)
   118  
   119  	// basic options
   120  	flagSet.Bool("version", false, "print version string")
   121  	flagSet.String("config", "", "path to config file")
   122  
   123  	logLevel := opts.LogLevel
   124  	flagSet.Var(&logLevel, "log-level", "set log verbosity: debug, info, warn, error, or fatal")
   125  	flagSet.String("log-prefix", "[nsqd] ", "log message prefix")
   126  	flagSet.Bool("verbose", false, "[deprecated] has no effect, use --log-level")
   127  
   128  	flagSet.Int64("node-id", opts.ID, "unique part for message IDs, (int) in range [0,1024) (default is hash of hostname)")
   129  	flagSet.Bool("worker-id", false, "[deprecated] use --node-id")
   130  
   131  	flagSet.String("https-address", opts.HTTPSAddress, "<addr>:<port> to listen on for HTTPS clients")
   132  	flagSet.String("http-address", opts.HTTPAddress, "address to listen on for HTTP clients (<addr>:<port> for TCP/IP or <path> for unix socket)")
   133  	flagSet.String("tcp-address", opts.TCPAddress, "address to listen on for TCP clients (<addr>:<port> for TCP/IP or <path> for unix socket)")
   134  
   135  	authHTTPAddresses := app.StringArray{}
   136  	flagSet.Var(&authHTTPAddresses, "auth-http-address", "<addr>:<port> or a full url to query auth server (may be given multiple times)")
   137  	flagSet.String("broadcast-address", opts.BroadcastAddress, "address that will be registered with lookupd (defaults to the OS hostname)")
   138  	flagSet.Int("broadcast-tcp-port", opts.BroadcastTCPPort, "TCP port that will be registered with lookupd (defaults to the TCP port that this nsqd is listening on)")
   139  	flagSet.Int("broadcast-http-port", opts.BroadcastHTTPPort, "HTTP port that will be registered with lookupd (defaults to the HTTP port that this nsqd is listening on)")
   140  	lookupdTCPAddrs := app.StringArray{}
   141  	flagSet.Var(&lookupdTCPAddrs, "lookupd-tcp-address", "lookupd TCP address (may be given multiple times)")
   142  	flagSet.Duration("http-client-connect-timeout", opts.HTTPClientConnectTimeout, "timeout for HTTP connect")
   143  	flagSet.Duration("http-client-request-timeout", opts.HTTPClientRequestTimeout, "timeout for HTTP request")
   144  
   145  	// diskqueue options
   146  	flagSet.String("data-path", opts.DataPath, "path to store disk-backed messages")
   147  	flagSet.Int64("mem-queue-size", opts.MemQueueSize, "number of messages to keep in memory (per topic/channel)")
   148  	flagSet.Int64("max-bytes-per-file", opts.MaxBytesPerFile, "number of bytes per diskqueue file before rolling")
   149  	flagSet.Int64("sync-every", opts.SyncEvery, "number of messages per diskqueue fsync")
   150  	flagSet.Duration("sync-timeout", opts.SyncTimeout, "duration of time per diskqueue fsync")
   151  
   152  	flagSet.Int("queue-scan-worker-pool-max", opts.QueueScanWorkerPoolMax, "max concurrency for checking in-flight and deferred message timeouts")
   153  	flagSet.Int("queue-scan-selection-count", opts.QueueScanSelectionCount, "number of channels to check per cycle (every 100ms) for in-flight and deferred timeouts")
   154  
   155  	// msg and command options
   156  	flagSet.Duration("msg-timeout", opts.MsgTimeout, "default duration to wait before auto-requeing a message")
   157  	flagSet.Duration("max-msg-timeout", opts.MaxMsgTimeout, "maximum duration before a message will timeout")
   158  	flagSet.Int64("max-msg-size", opts.MaxMsgSize, "maximum size of a single message in bytes")
   159  	flagSet.Duration("max-req-timeout", opts.MaxReqTimeout, "maximum requeuing timeout for a message")
   160  	flagSet.Int64("max-body-size", opts.MaxBodySize, "maximum size of a single command body")
   161  
   162  	// client overridable configuration options
   163  	flagSet.Duration("max-heartbeat-interval", opts.MaxHeartbeatInterval, "maximum client configurable duration of time between client heartbeats")
   164  	flagSet.Int64("max-rdy-count", opts.MaxRdyCount, "maximum RDY count for a client")
   165  	flagSet.Int64("max-output-buffer-size", opts.MaxOutputBufferSize, "maximum client configurable size (in bytes) for a client output buffer")
   166  	flagSet.Duration("max-output-buffer-timeout", opts.MaxOutputBufferTimeout, "maximum client configurable duration of time between flushing to a client")
   167  	flagSet.Duration("min-output-buffer-timeout", opts.MinOutputBufferTimeout, "minimum client configurable duration of time between flushing to a client")
   168  	flagSet.Duration("output-buffer-timeout", opts.OutputBufferTimeout, "default duration of time between flushing data to clients")
   169  	flagSet.Int("max-channel-consumers", opts.MaxChannelConsumers, "maximum channel consumer connection count per nsqd instance (default 0, i.e., unlimited)")
   170  
   171  	// statsd integration options
   172  	flagSet.String("statsd-address", opts.StatsdAddress, "UDP <addr>:<port> of a statsd daemon for pushing stats")
   173  	flagSet.Duration("statsd-interval", opts.StatsdInterval, "duration between pushing to statsd")
   174  	flagSet.Bool("statsd-mem-stats", opts.StatsdMemStats, "toggle sending memory and GC stats to statsd")
   175  	flagSet.String("statsd-prefix", opts.StatsdPrefix, "prefix used for keys sent to statsd (%s for host replacement)")
   176  	flagSet.Int("statsd-udp-packet-size", opts.StatsdUDPPacketSize, "the size in bytes of statsd UDP packets")
   177  	flagSet.Bool("statsd-exclude-ephemeral", opts.StatsdExcludeEphemeral, "Skip ephemeral topics and channels when sending stats to statsd")
   178  
   179  	// End to end percentile flags
   180  	e2eProcessingLatencyPercentiles := app.FloatArray{}
   181  	flagSet.Var(&e2eProcessingLatencyPercentiles, "e2e-processing-latency-percentile", "message processing time percentiles (as float (0, 1.0]) to track (can be specified multiple times or comma separated '1.0,0.99,0.95', default none)")
   182  	flagSet.Duration("e2e-processing-latency-window-time", opts.E2EProcessingLatencyWindowTime, "calculate end to end latency quantiles for this duration of time (ie: 60s would only show quantile calculations from the past 60 seconds)")
   183  
   184  	// TLS config
   185  	flagSet.String("tls-cert", opts.TLSCert, "path to certificate file")
   186  	flagSet.String("tls-key", opts.TLSKey, "path to key file")
   187  	flagSet.String("tls-client-auth-policy", opts.TLSClientAuthPolicy, "client certificate auth policy ('require' or 'require-verify')")
   188  	flagSet.String("tls-root-ca-file", opts.TLSRootCAFile, "path to certificate authority file")
   189  	tlsRequired := tlsRequiredOption(opts.TLSRequired)
   190  	tlsMinVersion := tlsMinVersionOption(opts.TLSMinVersion)
   191  	flagSet.Var(&tlsRequired, "tls-required", "require TLS for client connections (true, false, tcp-https)")
   192  	flagSet.Var(&tlsMinVersion, "tls-min-version", "minimum SSL/TLS version acceptable ('ssl3.0', 'tls1.0', 'tls1.1', 'tls1.2' or 'tls1.3')")
   193  
   194  	// compression
   195  	flagSet.Bool("deflate", opts.DeflateEnabled, "enable deflate feature negotiation (client compression)")
   196  	flagSet.Int("max-deflate-level", opts.MaxDeflateLevel, "max deflate compression level a client can negotiate (> values == > nsqd CPU usage)")
   197  	flagSet.Bool("snappy", opts.SnappyEnabled, "enable snappy feature negotiation (client compression)")
   198  
   199  	return flagSet
   200  }