-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.go
163 lines (142 loc) · 4.89 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package pubsub
import (
"time"
"github.com/gorilla/websocket"
"github.com/MicahParks/websocket-pubsub/clients"
)
var (
// closeDeadlineVar is the CloseDeadline as a variable instead of a constant.
closeDeadlineVar = clients.CloseDeadline
// defaultSubscriptionOptions contains the default options a subscription can have.
defaultSubscriptionOptions = SubscriptionOptions{
CloseDeadline: &closeDeadlineVar,
Events: nil,
MessageBuffer: 10,
PublisherBuffer: 1,
SubscriberBuffer: 1,
SubscriberWriteTimeout: &[]time.Duration{time.Second * 10}[0],
}
)
// SubscriptionOptions represents information used to create a subscription that already has default values.
type SubscriptionOptions struct {
// CloseDeadline is the time to wait to gracefully closing something. After that, it's closed regardless.
CloseDeadline *time.Duration
// Events is a channel of events for the subscription.
Events chan<- Event
// MessageBuffer is the internal channel buffer for messages for the subscription.
MessageBuffer uint
// PongDeadline is the time to wait for a pong message after a ping.
PongDeadline *time.Duration
// PublisherBuffer is the internal channel buffer for adding and removing publishers.
PublisherBuffer uint
// SubscriberBuffer is the internal channel buffer for adding and remove subscribers.
SubscriberBuffer uint
// SubscriberWriteTimeout is the time to wait for a message to be written to a subscriber. If the message is not
// written in this amount of time, the subscriber is closed.
SubscriberWriteTimeout *time.Duration
}
// Options represents all of the websocket pubsub information that already has default values.
type Options struct {
// Options are the options for all subscriptions created by the service.
Options *SubscriptionOptions
// Upgrader is the websocket upgrader to when clients connect.
Upgrader *websocket.Upgrader
}
// directClientOptions represents subscription information that already has default values.
type directClientOptions struct {
closeDeadline *time.Duration
events chan<- Event
pongDeadline *time.Duration
}
// flattenOptions takes in a slice of Options, uses the highest index of their fields' values to create one Options.
func flattenOptions(options []Options) (option Options) {
for _, opt := range options {
if opt.Options != nil {
option.Options = opt.Options
}
if opt.Upgrader != nil {
option.Upgrader = opt.Upgrader
}
}
if option.Options == nil {
option.Options = &defaultSubscriptionOptions
}
if option.Upgrader == nil {
option.Upgrader = &websocket.Upgrader{}
}
return option
}
// flattenSubscriptionOptions takes in a slice of SubscriptionOptions, uses the highest index of their fields' values to
// create one SubscriptionOptions.
func flattenSubscriptionOptions(options []SubscriptionOptions) (option SubscriptionOptions) {
for _, opt := range options {
if opt.CloseDeadline != nil {
option.CloseDeadline = opt.CloseDeadline
}
if opt.Events != nil {
option.Events = opt.Events
}
if opt.MessageBuffer != 0 {
option.MessageBuffer = opt.MessageBuffer
}
if opt.PongDeadline != nil {
option.PongDeadline = opt.PongDeadline
}
if opt.PublisherBuffer != 0 {
option.PublisherBuffer = opt.PublisherBuffer
}
if opt.SubscriberBuffer != 0 {
option.SubscriberBuffer = opt.SubscriberBuffer
}
if opt.SubscriberWriteTimeout != nil {
option.SubscriberWriteTimeout = opt.SubscriberWriteTimeout
}
}
if option.CloseDeadline == nil {
option.CloseDeadline = defaultSubscriptionOptions.CloseDeadline
}
if option.Events == nil {
option.Events = defaultSubscriptionOptions.Events
}
if option.MessageBuffer == 0 {
option.MessageBuffer = defaultSubscriptionOptions.MessageBuffer
}
if option.PongDeadline == nil {
deadline := clients.PongDeadline
option.PongDeadline = &deadline
}
if option.PublisherBuffer == 0 {
option.PublisherBuffer = defaultSubscriptionOptions.PublisherBuffer
}
if option.SubscriberBuffer == 0 {
option.SubscriberBuffer = defaultSubscriptionOptions.SubscriberBuffer
}
if option.SubscriberWriteTimeout == nil {
option.SubscriberWriteTimeout = defaultSubscriptionOptions.SubscriberWriteTimeout
}
return option
}
// flattenDirectClientOptions takes in a slice of directClientOptions, uses the highest index of their fields' values to
// create one directClientOptions.
func flattenDirectClientOptions(options []directClientOptions) (option directClientOptions) {
for _, opt := range options {
if opt.closeDeadline != nil {
option.closeDeadline = opt.closeDeadline
}
if opt.events != nil {
option.events = opt.events
}
if opt.pongDeadline != nil {
option.pongDeadline = opt.pongDeadline
}
}
if option.closeDeadline == nil {
deadline := clients.CloseDeadline
option.closeDeadline = &deadline
}
if option.pongDeadline == nil {
deadline := clients.PongDeadline
option.pongDeadline = &deadline
}
return option
}