You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ngs/options.go

154 lines
4.0 KiB
Go

package ngs
import (
"git.noahlan.cn/northlan/ngs/cluster"
"git.noahlan.cn/northlan/ngs/component"
"git.noahlan.cn/northlan/ngs/internal/env"
"git.noahlan.cn/northlan/ngs/internal/log"
"git.noahlan.cn/northlan/ngs/internal/message"
"git.noahlan.cn/northlan/ngs/pipeline"
"git.noahlan.cn/northlan/ngs/serialize"
"google.golang.org/grpc"
"net/http"
"time"
)
type Option func(*cluster.Options)
func WithPipeline(pipeline pipeline.Pipeline) Option {
return func(opt *cluster.Options) {
opt.Pipeline = pipeline
}
}
// WithAdvertiseAddr sets the advertisement address option, it will be the listen address in
// master node and an advertisement address which cluster member to connect
func WithAdvertiseAddr(addr string, retryInterval ...time.Duration) Option {
return func(opt *cluster.Options) {
opt.AdvertiseAddr = addr
if len(retryInterval) > 0 {
opt.RetryInterval = retryInterval[0]
}
}
}
// WithClientAddr sets the listen address which is used to establish connection between
// cluster members. Will select an available port automatically if no member address
// setting and panic if no available port
func WithClientAddr(addr string) Option {
return func(opt *cluster.Options) {
opt.ClientAddr = addr
}
}
// WithMaster sets the option to indicate whether the current node is master node
func WithMaster() Option {
return func(opt *cluster.Options) {
opt.IsMaster = true
}
}
// WithGrpcOptions sets the grpc dial options
func WithGrpcOptions(opts ...grpc.DialOption) Option {
return func(_ *cluster.Options) {
env.GrpcOptions = append(env.GrpcOptions, opts...)
}
}
// WithComponents sets the Components
func WithComponents(components *component.Components) Option {
return func(opt *cluster.Options) {
opt.Components = components
}
}
// WithHeartbeatInterval sets Heartbeat time interval
func WithHeartbeatInterval(d time.Duration) Option {
return func(_ *cluster.Options) {
env.Heartbeat = d
}
}
// WithCheckOriginFunc sets the function that check `Origin` in http headers
func WithCheckOriginFunc(fn func(*http.Request) bool) Option {
return func(opt *cluster.Options) {
env.CheckOrigin = fn
}
}
// WithDebugMode let 'ngs' to run under Debug mode.
func WithDebugMode() Option {
return func(_ *cluster.Options) {
env.Debug = true
}
}
// WithDictionary sets routes map
func WithDictionary(dict map[string]uint16) Option {
return func(_ *cluster.Options) {
message.SetDictionary(dict)
}
}
func WithWSPath(path string) Option {
return func(_ *cluster.Options) {
env.WSPath = path
}
}
// WithTimerPrecision sets the ticker precision, and time precision can not less
// than a Millisecond, and can not change after application running. The default
// precision is time.Second
func WithTimerPrecision(precision time.Duration) Option {
if precision < time.Millisecond {
panic("time precision can not less than a Millisecond")
}
return func(_ *cluster.Options) {
env.TimerPrecision = precision
}
}
// WithSerializer customizes application serializer, which automatically Marshal
// and UnMarshal handler payload
func WithSerializer(serializer serialize.Serializer) Option {
return func(opt *cluster.Options) {
env.Serializer = serializer
}
}
// WithLabel sets the current node label in cluster
func WithLabel(label string) Option {
return func(opt *cluster.Options) {
opt.Label = label
}
}
// WithIsWebsocket indicates whether current node WebSocket is enabled
func WithIsWebsocket(enableWs bool) Option {
return func(opt *cluster.Options) {
opt.IsWebsocket = enableWs
}
}
// WithTSLConfig sets the `key` and `certificate` of TSL
func WithTSLConfig(certificate, key string) Option {
return func(opt *cluster.Options) {
opt.TSLCertificate = certificate
opt.TSLKey = key
}
}
// WithLogger overrides the default logger
func WithLogger(l log.Logger) Option {
return func(opt *cluster.Options) {
log.SetLogger(l)
}
}
// WithHandshakeValidator sets the function that Verify `handshake` data
func WithHandshakeValidator(fn func([]byte) error) Option {
return func(opt *cluster.Options) {
env.HandshakeValidator = fn
}
}