-
Notifications
You must be signed in to change notification settings - Fork 4
/
server_options.go
96 lines (83 loc) · 2.36 KB
/
server_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) Jim Lambert
// SPDX-License-Identifier: MIT
package gldap
import (
"crypto/tls"
"time"
"github.com/hashicorp/go-hclog"
)
type configOptions struct {
withTLSConfig *tls.Config
withLogger hclog.Logger
withReadTimeout time.Duration
withWriteTimeout time.Duration
withDisablePanicRecovery bool
withOnClose OnCloseHandler
}
func configDefaults() configOptions {
return configOptions{}
}
// getConfigOpts gets the defaults and applies the opt overrides passed
// in.
func getConfigOpts(opt ...Option) configOptions {
opts := configDefaults()
applyOpts(&opts, opt...)
return opts
}
// WithLogger provides the optional logger.
func WithLogger(l hclog.Logger) Option {
return func(o interface{}) {
if o, ok := o.(*configOptions); ok {
o.withLogger = l
}
}
}
// WithTLSConfig provides an optional tls.Config
func WithTLSConfig(tc *tls.Config) Option {
return func(o interface{}) {
switch v := o.(type) {
case *configOptions:
v.withTLSConfig = tc
}
}
}
// WithReadTimeout will set a read time out per connection
func WithReadTimeout(d time.Duration) Option {
return func(o interface{}) {
if o, ok := o.(*configOptions); ok {
o.withReadTimeout = d
}
}
}
// WithWriteTimeout will set a write timeout per connection
func WithWriteTimeout(d time.Duration) Option {
return func(o interface{}) {
if o, ok := o.(*configOptions); ok {
o.withWriteTimeout = d
}
}
}
// WithDisablePanicRecovery will disable recovery from panics which occur when
// handling a request. This is helpful for debugging since you'll get the
// panic's callstack.
func WithDisablePanicRecovery() Option {
return func(o interface{}) {
if o, ok := o.(*configOptions); ok {
o.withDisablePanicRecovery = true
}
}
}
// OnCloseHandler defines a function for a "on close" callback handler. See:
// NewServer(...) and WithOnClose(...) option for more information
type OnCloseHandler func(connectionID int)
// WithOnClose defines a OnCloseHandler that the server will use as a callback
// every time a connection to the server is closed. This allows callers to
// clean up resources for closed connections (using their ID to determine which
// one to clean up)
func WithOnClose(handler OnCloseHandler) Option {
return func(o interface{}) {
if o, ok := o.(*configOptions); ok {
o.withOnClose = handler
}
}
}