This repository has been archived on 2024-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
orion/reach/option.go

92 lines
1.8 KiB
Go

package reach
import (
"log"
"os"
"time"
"forge.cadoles.com/Pyxis/golang-socketio"
)
type Logger interface {
Printf(format string, args ...interface{})
}
// Options are ReachRS client configuration options
type Options struct {
Endpoint string
PingInterval time.Duration
PingTimeout time.Duration
ReceiveTimeout time.Duration
SendTimeout time.Duration
BufferSize int
Logger Logger
}
type OptionFunc func(opts *Options)
func WithEndpoint(host string, port int) OptionFunc {
return func(opts *Options) {
opts.Endpoint = gosocketio.GetUrl(host, port, false)
}
}
func WithPingInterval(interval time.Duration) OptionFunc {
return func(opts *Options) {
opts.PingInterval = interval
}
}
func WithPingTimeout(timeout time.Duration) OptionFunc {
return func(opts *Options) {
opts.PingTimeout = timeout
}
}
func WithReceiveTimeout(timeout time.Duration) OptionFunc {
return func(opts *Options) {
opts.ReceiveTimeout = timeout
}
}
func WithSendTimeout(timeout time.Duration) OptionFunc {
return func(opts *Options) {
opts.SendTimeout = timeout
}
}
func WithBufferSize(size int) OptionFunc {
return func(opts *Options) {
opts.BufferSize = size
}
}
func WithLogger(logger Logger) OptionFunc {
return func(opts *Options) {
opts.Logger = logger
}
}
func WithStandardLogger() OptionFunc {
return func(opts *Options) {
logger := log.New(os.Stdout, "[reachrs] ", log.LstdFlags)
opts.Logger = logger
}
}
func DefaultOptions() *Options {
opts := &Options{}
defaults := []OptionFunc{
WithEndpoint("192.168.42.1", 80),
WithPingInterval(30 * time.Second),
WithPingTimeout(60 * time.Second),
WithReceiveTimeout(60 * time.Second),
WithSendTimeout(60 * time.Second),
WithBufferSize(1024 * 32),
}
for _, o := range defaults {
o(opts)
}
return opts
}