server

package
v0.0.0-...-80c8cae Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2021 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CLIENT is an end user
	CLIENT = iota
	// ROUTER is another router in the cluster
	ROUTER
)

Type of client connection

View Source
const (
	ClientProtoZero = iota
	ClientProtoInfo
)
View Source
const (
	CommandStop   = Command("stop")
	CommandQuit   = Command("quit")
	CommandReopen = Command("reopen")
	CommandReload = Command("reload")
)

Valid Command values.

View Source
const (
	// VERSION is the current version for the server.
	VERSION = "1.0.0"

	// DEFAULT_PORT is the default port for client connections.
	DEFAULT_PORT = 6430

	// RANDOM_PORT is the value for port that, when supplied, will cause the
	// server to listen on a randomly-chosen available port. The resolved port
	// is available via the Addr() method.
	RANDOM_PORT = -1

	// DEFAULT_HOST defaults to all interfaces.
	DEFAULT_HOST = "0.0.0.0"

	// MAX_CONTROL_LINE_SIZE is the maximum allowed protocol control line size.
	// 1k should be plenty since payloads sans connect string are separate
	MAX_CONTROL_LINE_SIZE = 1024

	// MAX_PAYLOAD_SIZE is the maximum allowed payload size. Should be using
	// something different if > 1MB payloads are needed.
	MAX_PAYLOAD_SIZE = (1024 * 1024)

	// DEFAULT_MAX_CONNECTIONS is the default maximum connections allowed.
	DEFAULT_MAX_CONNECTIONS = (64 * 1024)

	// TLS_TIMEOUT is the TLS wait time.
	TLS_TIMEOUT = 500 * time.Millisecond

	// AUTH_TIMEOUT is the authorization wait time.
	AUTH_TIMEOUT = 2 * TLS_TIMEOUT

	// DEFAULT_PING_INTERVAL is how often pings are sent to clients and routes.
	DEFAULT_PING_INTERVAL = 2 * time.Minute

	// DEFAULT_PING_MAX_OUT is maximum allowed pings outstanding before disconnect.
	DEFAULT_PING_MAX_OUT = 2

	// CR_LF string
	CR_LF = "\r\n"

	// LEN_CR_LF hold onto the computed size.
	LEN_CR_LF = len(CR_LF)

	// DEFAULT_FLUSH_DEADLINE is the write/flush deadlines.
	DEFAULT_FLUSH_DEADLINE = 2 * time.Second

	// DEFAULT_HTTP_PORT is the default monitoring port.
	DEFAULT_HTTP_PORT = 8222

	// ACCEPT_MIN_SLEEP is the minimum acceptable sleep times on temporary errors.
	ACCEPT_MIN_SLEEP = 10 * time.Millisecond

	// ACCEPT_MAX_SLEEP is the maximum acceptable sleep times on temporary errors
	ACCEPT_MAX_SLEEP = 1 * time.Second

	// DEFAULT_ROUTE_CONNECT Route solicitation intervals.
	DEFAULT_ROUTE_CONNECT = 1 * time.Second

	// DEFAULT_ROUTE_RECONNECT Route reconnect intervals.
	DEFAULT_ROUTE_RECONNECT = 1 * time.Second

	// DEFAULT_ROUTE_DIAL Route dial timeout.
	DEFAULT_ROUTE_DIAL = 1 * time.Second

	// PROTO_SNIPPET_SIZE is the default size of proto to print on parse errors.
	PROTO_SNIPPET_SIZE = 32

	// MAX_MSG_ARGS Maximum possible number of arguments from MSG proto.
	MAX_MSG_ARGS = 4

	// MAX_PUB_ARGS Maximum possible number of arguments from PUB proto.
	MAX_PUB_ARGS = 3
)
View Source
const (
	OP_START = iota
	OP_PLUS
	OP_PLUS_O
	OP_PLUS_OK
	OP_MINUS
	OP_MINUS_E
	OP_MINUS_ER
	OP_MINUS_ERR
	OP_MINUS_ERR_SPC
	MINUS_ERR_ARG
	OP_C
	OP_CO
	OP_CON
	OP_CONN
	OP_CONNE
	OP_CONNEC
	OP_CONNECT
	CONNECT_ARG
	OP_P
	OP_PU
	OP_PUB
	OP_PUB_SPC
	PUB_ARG
	OP_PI
	OP_PIN
	OP_PING
	OP_PO
	OP_PON
	OP_PONG
	MSG_PAYLOAD
	MSG_END
	OP_S
	OP_SU
	OP_SUB
	OP_SUB_SPC
	SUB_ARG
	OP_U
	OP_UN
	OP_UNS
	OP_UNSU
	OP_UNSUB
	OP_UNSUB_SPC
	UNSUB_ARG
	OP_M
	OP_MS
	OP_MSG
	OP_MSG_SPC
	MSG_ARG
	OP_I
	OP_IN
	OP_INF
	OP_INFO
	INFO_ARG
)

Variables

View Source
var (
	// ErrConnectionClosed represents an error condition on a closed connection.
	ErrConnectionClosed = errors.New("Connection Closed")

	// ErrAuthorization represents an error condition on failed authorization.
	ErrAuthorization = errors.New("Authorization Error")

	// ErrAuthTimeout represents an error condition on failed authorization due to timeout.
	ErrAuthTimeout = errors.New("Authorization Timeout")

	// ErrMaxPayload represents an error condition when the payload is too big.
	ErrMaxPayload = errors.New("Maximum Payload Exceeded")

	// ErrMaxControlLine represents an error condition when the control line is too big.
	ErrMaxControlLine = errors.New("Maximum Control Line Exceeded")

	// ErrReservedPublishSubject represents an error condition when sending to a reserved subject, e.g. _SYS.>
	ErrReservedPublishSubject = errors.New("Reserved Internal Subject")

	// ErrBadClientProtocol signals a client requested an invalud client protocol.
	ErrBadClientProtocol = errors.New("Invalid Client Protocol")

	// ErrTooManyConnections signals a client that the maximum number of connections supported by the
	// server has been reached.
	ErrTooManyConnections = errors.New("Maximum Connections Exceeded")

	// ErrClientConnectedToRoutePort represents an error condition when a client
	// attempted to connect to the route listen port.
	ErrClientConnectedToRoutePort = errors.New("Attempted To Connect To Route Port")
)
View Source
var (
	ErrInvalidSubject = errors.New("sublist: Invalid Subject")
	ErrNotFound       = errors.New("sublist: No Matches Found")
)

Sublist related errors

Functions

func PrintAndDie

func PrintAndDie(msg string)

func Run

func Run(server *Server) error

Types

type ClusterOpts

type ClusterOpts struct {
	Host           string      `json:"addr"`
	Port           int         `json:"cluster_port"`
	Username       string      `json:"-"`
	Password       string      `json:"-"`
	AuthTimeout    float64     `json:"-"`
	TLSTimeout     float64     `json:"-"`
	TLSConfig      *tls.Config `json:"-"`
	ListenStr      string      `json:"-"`
	NoAdvertise    bool        `json:"-"` // 通知
	ConnectRetries int         `json:-`   // 重连
}

type Command

type Command string

Command is a signal used to control a running gnatsd process.

type Info

type Info struct {
	ID                string   `json:"server_id"`     // NATS服务器的ID
	Version           string   `json:"version"`       // NATS的版本
	GoVersion         string   `json:"go"`            // NATS用的go版本
	Host              string   `json:"host"`          // 服务器主机IP
	Port              int      `json:"port"`          // 服务器主机Port
	AuthRequired      bool     `json:"auth_required"` // 是否需要鉴权
	SSLRequired       bool     `json:"ssl_required"`  // 是否需要SSL
	TLSRequired       bool     `json:"tls_required"`  // 是否需要TLS
	TLSVerify         bool     `json:"tls_verify"`    // TLS需要的证书
	MaxPayload        int      `int:"max_payload"`    // 最大接受长度
	IP                string   `json:"ip,omitempty"`
	ClientConnectURLs []string `json:"connect_urls,omitempty"` // 一个URL列表,表示客户端可以连接的服务器地址
}

type Logger

type Logger interface {
	// Log a notice err
	Noticef(format string, v ...interface{})

	// Log a fatal error
	Fatalf(format string, v ...interface{})

	// Log an error
	Errorf(format string, v ...interface{})

	// Log a debug statement
	Debugf(format string, v ...interface{})

	// Log a trace statement
	Tracef(format string, v ...interface{})
}

type Options

type Options struct {
	// 基本配置
	ConfigFile    string  `json:"-"`
	Host          string  `json:"host"`
	Port          int     `json:"port"`
	Trace         bool    `json:"-"`
	Debug         bool    `json:"-"`
	MaxConn       int     `json:"max_connections"`
	Users         []*User `json:"-"`
	Username      string  `json:"-"`
	Password      string  `json:"-"`
	Authorization string  `json:"-"` // Authorization 授权

	PingInterval time.Duration `json:"ping_interval"`
	MaxPingsOut  int           `json:"ping_max"`

	MaxPayload int         `json:"max_payload"`
	Cluster    ClusterOpts `json:"cluster"`
	ProfPort   int         `json:"-"`
	PidFile    string      `josn:"-"`
	LogFile    string      `json:"-"`
	Routes     []*url.URL  `json:"-"`

	TLS           bool          `json:-`
	TLSConfig     *tls.Config   `json:"-"`
	WriteDeadline time.Duration `json:"-"`
}

type Permissions

type Permissions struct {
	Publish   []string `json:"publish"`
	Subscribe []string `json:"subscribe"`
}

type Server

type Server struct {
	// contains filtered or unexported fields
}

func New

func New(opt *Options) *Server

func (*Server) AcceptLoop

func (s *Server) AcceptLoop(clr chan struct{})

AcceptLoop is exported(可导出的) for easier testing

func (*Server) Debugf

func (s *Server) Debugf(format string, v ...interface{})

Log a debug statement

func (*Server) Errorf

func (s *Server) Errorf(format string, v ...interface{})

Log an error

func (*Server) Fatalf

func (s *Server) Fatalf(format string, v ...interface{})

Log a fatal error

func (*Server) Noticef

func (s *Server) Noticef(format string, v ...interface{})

Log a notice err

func (*Server) Shutdown

func (s *Server) Shutdown()

func (*Server) Start

func (s *Server) Start()

func (*Server) StartProfiler

func (s *Server) StartProfiler()

StartProfiler is called to enable dynamic profiling(描述)

func (*Server) StartRouting

func (s *Server) StartRouting(clientListenReady chan struct{})

StartRouting will start the accept loop om the cluster host:port and willl actively try to connect listed routes

func (*Server) Tracef

func (s *Server) Tracef(format string, v ...interface{})

Log a trace statement

type Sublist

type Sublist struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

A Sublist stores and efficiently(有效地) retrieves(检索) subscriptions(订阅).

func NewSubList

func NewSubList() *Sublist

func (*Sublist) Insert

func (s *Sublist) Insert(sub *subscription) error

Insert adds a subscription into the sublist

type SublistResult

type SublistResult struct {
	// contains filtered or unexported fields
}

A result structrue better optimized for queue subs.

type User

type User struct {
	Username    string       `json:"user"`
	Password    string       `json:"password"`
	Permissions *Permissions `json:"permissions"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL