| 1 |
// Copyright 2012-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package server |
| 15 |
|
| 16 |
import ( |
| 17 |
"regexp" |
| 18 |
"runtime/debug" |
| 19 |
"time" |
| 20 |
) |
| 21 |
|
| 22 |
// Command is a signal used to control a running nats-server process. |
| 23 |
type Command string |
| 24 |
|
| 25 |
// Valid Command values. |
| 26 |
const ( |
| 27 |
CommandStop = Command("stop") |
| 28 |
CommandQuit = Command("quit") |
| 29 |
CommandReopen = Command("reopen") |
| 30 |
CommandReload = Command("reload") |
| 31 |
|
| 32 |
// private for now |
| 33 |
commandLDMode = Command("ldm") |
| 34 |
commandTerm = Command("term") |
| 35 |
) |
| 36 |
|
| 37 |
var ( |
| 38 |
// gitCommit and serverVersion injected at build. |
| 39 |
gitCommit, serverVersion string |
| 40 |
// trustedKeys is a whitespace separated array of trusted operator's public nkeys. |
| 41 |
trustedKeys string |
| 42 |
// SemVer regexp to validate the VERSION. |
| 43 |
semVerRe = regexp.MustCompile(`^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`) |
| 44 |
) |
| 45 |
|
| 46 |
// formatRevision formats a VCS revision string for display. |
| 47 |
func formatRevision(revision string) string { |
| 48 |
if len(revision) >= 7 { |
| 49 |
return revision[:7] |
| 50 |
} |
| 51 |
return revision |
| 52 |
} |
| 53 |
|
| 54 |
func init() { |
| 55 |
// Use build info if present, it would be if building using 'go build .' |
| 56 |
// or when using a release. |
| 57 |
if info, ok := debug.ReadBuildInfo(); ok { |
| 58 |
for _, setting := range info.Settings { |
| 59 |
switch setting.Key { |
| 60 |
case "vcs.revision": |
| 61 |
gitCommit = formatRevision(setting.Value) |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
const ( |
| 68 |
// VERSION is the current version for the server. |
| 69 |
VERSION = "2.14.6" |
| 70 |
|
| 71 |
// PROTO is the currently supported protocol. |
| 72 |
// 0 was the original |
| 73 |
// 1 maintains proto 0, adds echo abilities for CONNECT from the client. Clients |
| 74 |
// should not send echo unless proto in INFO is >= 1. |
| 75 |
PROTO = 1 |
| 76 |
|
| 77 |
// DEFAULT_PORT is the default port for client connections. |
| 78 |
DEFAULT_PORT = 4222 |
| 79 |
|
| 80 |
// RANDOM_PORT is the value for port that, when supplied, will cause the |
| 81 |
// server to listen on a randomly-chosen available port. The resolved port |
| 82 |
// is available via the Addr() method. |
| 83 |
RANDOM_PORT = -1 |
| 84 |
|
| 85 |
// DEFAULT_HOST defaults to all interfaces. |
| 86 |
DEFAULT_HOST = "0.0.0.0" |
| 87 |
|
| 88 |
// MAX_CONTROL_LINE_SIZE is the maximum allowed protocol control line size. |
| 89 |
// 4k should be plenty since payloads sans connect/info string are separate. |
| 90 |
MAX_CONTROL_LINE_SIZE = 4096 |
| 91 |
|
| 92 |
// MAX_PAYLOAD_SIZE is the maximum allowed payload size. Should be using |
| 93 |
// something different if > 1MB payloads are needed. |
| 94 |
MAX_PAYLOAD_SIZE = (1024 * 1024) |
| 95 |
|
| 96 |
// MAX_PAYLOAD_MAX_SIZE is the size at which the server will warn about |
| 97 |
// max_payload being too high. In the future, the server may enforce/reject |
| 98 |
// max_payload above this value. |
| 99 |
MAX_PAYLOAD_MAX_SIZE = (8 * 1024 * 1024) |
| 100 |
|
| 101 |
// MAX_PENDING_SIZE is the maximum outbound pending bytes per client. |
| 102 |
MAX_PENDING_SIZE = (64 * 1024 * 1024) |
| 103 |
|
| 104 |
// DEFAULT_MAX_CONNECTIONS is the default maximum connections allowed. |
| 105 |
DEFAULT_MAX_CONNECTIONS = (64 * 1024) |
| 106 |
|
| 107 |
// TLS_TIMEOUT is the TLS wait time. |
| 108 |
TLS_TIMEOUT = 2 * time.Second |
| 109 |
|
| 110 |
// DEFAULT_TLS_HANDSHAKE_FIRST_FALLBACK_DELAY is the default amount of |
| 111 |
// time for the server to wait for the TLS handshake with a client to |
| 112 |
// be initiated before falling back to sending the INFO protocol first. |
| 113 |
// See TLSHandshakeFirst and TLSHandshakeFirstFallback options. |
| 114 |
DEFAULT_TLS_HANDSHAKE_FIRST_FALLBACK_DELAY = 50 * time.Millisecond |
| 115 |
|
| 116 |
// AUTH_TIMEOUT is the authorization wait time. |
| 117 |
AUTH_TIMEOUT = 2 * time.Second |
| 118 |
|
| 119 |
// DEFAULT_PING_INTERVAL is how often pings are sent to clients, etc... |
| 120 |
DEFAULT_PING_INTERVAL = 2 * time.Minute |
| 121 |
|
| 122 |
// DEFAULT_PING_MAX_OUT is maximum allowed pings outstanding before disconnect. |
| 123 |
DEFAULT_PING_MAX_OUT = 2 |
| 124 |
|
| 125 |
// CR_LF string |
| 126 |
CR_LF = "\r\n" |
| 127 |
|
| 128 |
// LEN_CR_LF hold onto the computed size. |
| 129 |
LEN_CR_LF = len(CR_LF) |
| 130 |
|
| 131 |
// DEFAULT_FLUSH_DEADLINE is the write/flush deadlines. |
| 132 |
DEFAULT_FLUSH_DEADLINE = 10 * time.Second |
| 133 |
|
| 134 |
// DEFAULT_HTTP_PORT is the default monitoring port. |
| 135 |
DEFAULT_HTTP_PORT = 8222 |
| 136 |
|
| 137 |
// DEFAULT_HTTP_BASE_PATH is the default base path for monitoring. |
| 138 |
DEFAULT_HTTP_BASE_PATH = "/" |
| 139 |
|
| 140 |
// ACCEPT_MIN_SLEEP is the minimum acceptable sleep times on temporary errors. |
| 141 |
ACCEPT_MIN_SLEEP = 10 * time.Millisecond |
| 142 |
|
| 143 |
// ACCEPT_MAX_SLEEP is the maximum acceptable sleep times on temporary errors |
| 144 |
ACCEPT_MAX_SLEEP = 1 * time.Second |
| 145 |
|
| 146 |
// DEFAULT_ROUTE_CONNECT Route solicitation intervals. |
| 147 |
DEFAULT_ROUTE_CONNECT = 1 * time.Second |
| 148 |
|
| 149 |
// DEFAULT_ROUTE_CONNECT_MAX Route solicitation intervals (max). |
| 150 |
DEFAULT_ROUTE_CONNECT_MAX = 30 * time.Second |
| 151 |
|
| 152 |
// DEFAULT_ROUTE_RECONNECT Route reconnect delay. |
| 153 |
DEFAULT_ROUTE_RECONNECT = 1 * time.Second |
| 154 |
|
| 155 |
// DEFAULT_ROUTE_DIAL Route dial timeout. |
| 156 |
DEFAULT_ROUTE_DIAL = 1 * time.Second |
| 157 |
|
| 158 |
// DEFAULT_ROUTE_POOL_SIZE Route default pool size |
| 159 |
DEFAULT_ROUTE_POOL_SIZE = 3 |
| 160 |
|
| 161 |
// DEFAULT_LEAF_NODE_RECONNECT LeafNode reconnect interval. |
| 162 |
DEFAULT_LEAF_NODE_RECONNECT = time.Second |
| 163 |
|
| 164 |
// DEFAULT_LEAF_TLS_TIMEOUT TLS timeout for LeafNodes |
| 165 |
DEFAULT_LEAF_TLS_TIMEOUT = 2 * time.Second |
| 166 |
|
| 167 |
// PROTO_SNIPPET_SIZE is the default size of proto to print on parse errors. |
| 168 |
PROTO_SNIPPET_SIZE = 32 |
| 169 |
|
| 170 |
// MAX_CONTROL_LINE_SNIPPET_SIZE is the default size of proto to print on max control line errors. |
| 171 |
MAX_CONTROL_LINE_SNIPPET_SIZE = 128 |
| 172 |
|
| 173 |
// MAX_MSG_ARGS Maximum possible number of arguments from MSG proto. |
| 174 |
MAX_MSG_ARGS = 4 |
| 175 |
|
| 176 |
// MAX_RMSG_ARGS Maximum possible number of arguments from RMSG proto. |
| 177 |
MAX_RMSG_ARGS = 6 |
| 178 |
|
| 179 |
// MAX_HMSG_ARGS Maximum possible number of arguments from HMSG proto. |
| 180 |
MAX_HMSG_ARGS = 7 |
| 181 |
|
| 182 |
// MAX_PUB_ARGS Maximum possible number of arguments from PUB proto. |
| 183 |
MAX_PUB_ARGS = 3 |
| 184 |
|
| 185 |
// MAX_HPUB_ARGS Maximum possible number of arguments from HPUB proto. |
| 186 |
MAX_HPUB_ARGS = 4 |
| 187 |
|
| 188 |
// MAX_RSUB_ARGS Maximum possible number of arguments from a RS+/LS+ proto. |
| 189 |
MAX_RSUB_ARGS = 6 |
| 190 |
|
| 191 |
// DEFAULT_MAX_CLOSED_CLIENTS is the maximum number of closed connections we hold onto. |
| 192 |
DEFAULT_MAX_CLOSED_CLIENTS = 10000 |
| 193 |
|
| 194 |
// DEFAULT_LAME_DUCK_DURATION is the time in which the server spreads |
| 195 |
// the closing of clients when signaled to go in lame duck mode. |
| 196 |
DEFAULT_LAME_DUCK_DURATION = 2 * time.Minute |
| 197 |
|
| 198 |
// DEFAULT_LAME_DUCK_GRACE_PERIOD is the duration the server waits, after entering |
| 199 |
// lame duck mode, before starting closing client connections. |
| 200 |
DEFAULT_LAME_DUCK_GRACE_PERIOD = 10 * time.Second |
| 201 |
|
| 202 |
// DEFAULT_LEAFNODE_INFO_WAIT Route dial timeout. |
| 203 |
DEFAULT_LEAFNODE_INFO_WAIT = 1 * time.Second |
| 204 |
|
| 205 |
// DEFAULT_LEAFNODE_PORT is the default port for remote leafnode connections. |
| 206 |
DEFAULT_LEAFNODE_PORT = 7422 |
| 207 |
|
| 208 |
// DEFAULT_CONNECT_ERROR_REPORTS is the number of attempts at which a |
| 209 |
// repeated failed route, gateway or leaf node connection is reported. |
| 210 |
// This is used for initial connection, that is, when the server has |
| 211 |
// never had a connection to the given endpoint. Once connected, and |
| 212 |
// if a disconnect occurs, DEFAULT_RECONNECT_ERROR_REPORTS is used |
| 213 |
// instead. |
| 214 |
// The default is to report every 3600 attempts (roughly every hour). |
| 215 |
DEFAULT_CONNECT_ERROR_REPORTS = 3600 |
| 216 |
|
| 217 |
// DEFAULT_RECONNECT_ERROR_REPORTS is the default number of failed |
| 218 |
// attempt to reconnect a route, gateway or leaf node connection. |
| 219 |
// The default is to report every attempt. |
| 220 |
DEFAULT_RECONNECT_ERROR_REPORTS = 1 |
| 221 |
|
| 222 |
// DEFAULT_RTT_MEASUREMENT_INTERVAL is how often we want to measure RTT from |
| 223 |
// this server to clients, routes, gateways or leafnode connections. |
| 224 |
DEFAULT_RTT_MEASUREMENT_INTERVAL = time.Hour |
| 225 |
|
| 226 |
// DEFAULT_ALLOW_RESPONSE_MAX_MSGS is the default number of responses allowed |
| 227 |
// for a reply subject. |
| 228 |
DEFAULT_ALLOW_RESPONSE_MAX_MSGS = 1 |
| 229 |
|
| 230 |
// DEFAULT_ALLOW_RESPONSE_EXPIRATION is the default time allowed for a given |
| 231 |
// dynamic response permission. |
| 232 |
DEFAULT_ALLOW_RESPONSE_EXPIRATION = 2 * time.Minute |
| 233 |
|
| 234 |
// DEFAULT_SERVICE_EXPORT_RESPONSE_THRESHOLD is the default time that the system will |
| 235 |
// expect a service export response to be delivered. This is used in corner cases for |
| 236 |
// time based cleanup of reverse mapping structures. |
| 237 |
DEFAULT_SERVICE_EXPORT_RESPONSE_THRESHOLD = 2 * time.Minute |
| 238 |
|
| 239 |
// DEFAULT_SERVICE_LATENCY_SAMPLING is the default sampling rate for service |
| 240 |
// latency metrics |
| 241 |
DEFAULT_SERVICE_LATENCY_SAMPLING = 100 |
| 242 |
|
| 243 |
// DEFAULT_SYSTEM_ACCOUNT |
| 244 |
DEFAULT_SYSTEM_ACCOUNT = "$SYS" |
| 245 |
|
| 246 |
// DEFAULT GLOBAL_ACCOUNT |
| 247 |
DEFAULT_GLOBAL_ACCOUNT = "$G" |
| 248 |
|
| 249 |
// DEFAULT_FETCH_TIMEOUT is the default time that the system will wait for an account fetch to return. |
| 250 |
DEFAULT_ACCOUNT_FETCH_TIMEOUT = 1900 * time.Millisecond |
| 251 |
) |