This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
serverOptions.go
100 lines (83 loc) · 2.28 KB
/
serverOptions.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
97
98
99
100
package webwire
import (
"fmt"
"log"
"os"
"time"
)
// OptionValue represents the setting value of an option
type OptionValue = int32
const (
// OptionUnset represents the default unset value
OptionUnset OptionValue = iota
// Disabled disables an option
Disabled
// Enabled enables an option
Enabled
)
// ServerOptions represents the options
// used during the creation of a new WebWire server instance
type ServerOptions struct {
Sessions OptionValue
SessionManager SessionManager
SessionKeyGenerator SessionKeyGenerator
SessionInfoParser SessionInfoParser
MaxSessionConnections uint
WarnLog *log.Logger
ErrorLog *log.Logger
ReadTimeout time.Duration
// SubProtocolName defines the optional name of the hosted webwire
// sub-protocol
SubProtocolName []byte
// MessageBufferSize defines the size of the message buffer
MessageBufferSize uint32
}
// Prepare verifies the specified options and sets the default values to
// unspecified options
func (op *ServerOptions) Prepare() error {
// Enable sessions by default
if op.Sessions == OptionUnset {
op.Sessions = Enabled
}
if op.Sessions == Enabled && op.SessionManager == nil {
// Force the default session manager
// to use the default session directory
op.SessionManager = NewDefaultSessionManager("")
}
if op.Sessions == Enabled && op.SessionKeyGenerator == nil {
op.SessionKeyGenerator = NewDefaultSessionKeyGenerator()
}
if op.SessionInfoParser == nil {
op.SessionInfoParser = GenericSessionInfoParser
}
if op.ReadTimeout < 1*time.Second {
op.ReadTimeout = 60 * time.Second
}
// Create default loggers to std-out/err when no loggers are specified
if op.WarnLog == nil {
op.WarnLog = log.New(
os.Stdout,
"WWR_WARN: ",
log.Ldate|log.Ltime|log.Lshortfile,
)
}
if op.ErrorLog == nil {
op.ErrorLog = log.New(
os.Stderr,
"WWR_ERR: ",
log.Ldate|log.Ltime|log.Lshortfile,
)
}
const minMsgBufferSize = 32
// Verify the message buffer size
if op.MessageBufferSize == 0 {
op.MessageBufferSize = 8192 // Default buffer size: 8K
} else if op.MessageBufferSize < minMsgBufferSize {
return fmt.Errorf(
"message buffer size too small: %d bytes (min: %d bytes)",
op.MessageBufferSize,
minMsgBufferSize,
)
}
return nil
}