-
Notifications
You must be signed in to change notification settings - Fork 8
/
options.go
75 lines (62 loc) · 1.8 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: Apache-2.0
package frisbee
import (
"crypto/tls"
"time"
"github.com/loopholelabs/logging/loggers/noop"
"github.com/loopholelabs/logging/types"
)
// Option is used to generate frisbee client and server options internally
type Option func(opts *Options)
// Options is used to provide the frisbee client and server with configuration options.
//
// Default Values:
//
// options := Options {
// KeepAlive: time.Minute * 3,
// Logger: &DefaultLogger,
// }
type Options struct {
KeepAlive time.Duration
Logger types.Logger
TLSConfig *tls.Config
}
func loadOptions(options ...Option) *Options {
opts := new(Options)
for _, option := range options {
option(opts)
}
if opts.Logger == nil {
opts.Logger = noop.New(types.InfoLevel)
}
if opts.KeepAlive == 0 {
opts.KeepAlive = time.Minute * 3
}
return opts
}
// WithOptions allows users to pass in an Options struct to configure a frisbee client or server
func WithOptions(options Options) Option {
return func(opts *Options) {
*opts = options
}
}
// WithKeepAlive allows users to define TCP keepalive options for the frisbee client or server (use -1 to disable)
func WithKeepAlive(keepAlive time.Duration) Option {
return func(opts *Options) {
opts.KeepAlive = keepAlive
}
}
// WithLogger sets the logger for the frisbee client or server
func WithLogger(logger types.Logger) Option {
return func(opts *Options) {
opts.Logger = logger
}
}
// WithTLS sets the TLS configuration for Frisbee. By default, no TLS configuration is used, and
// Frisbee will use unencrypted TCP connections. If the Frisbee Server is using TLS, then you must pass in
// a TLS config (even an empty one `&tls.Config{}`) for the Frisbee Client.
func WithTLS(tlsConfig *tls.Config) Option {
return func(opts *Options) {
opts.TLSConfig = tlsConfig
}
}