/
upstream_options.go
196 lines (168 loc) · 7.39 KB
/
upstream_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package iscp
import (
"time"
"github.com/aptpod/iscp-go/message"
)
// UpstreamConfigは、アップストリーム設定です。
type UpstreamConfig struct {
SessionID string // セッションID
AckInterval *time.Duration // Ackの返却間隔
CloseTimeout *time.Duration // Close時のタイムアウト
ExpiryInterval time.Duration // 有効期限
DataIDs []*message.DataID // データIDリスト
QoS message.QoS // QoS
Persist bool // 永続化するかどうか
FlushPolicy FlushPolicy
AckTimeout time.Duration // Ackのタイムアウト。この時間を過ぎてAckが返却された場合はコネクションを切断します。
// Ack受信時のフック
ReceiveAckHooker ReceiveAckHooker
// データポイント送信時のフック
SendDataPointsHooker SendDataPointsHooker
// アップストリームがクローズされたときのイベントハンドラ
ClosedEventHandler UpstreamClosedEventHandler
// アップストリームが再開されたときのイベントハンドラ
ResumedEventHandler UpstreamResumedEventHandler
}
var defaultUpstreamConfig = UpstreamConfig{
SessionID: "",
AckInterval: &defaultAckInterval,
CloseTimeout: &defaultCloseTimeout,
ExpiryInterval: defaultExpiryInterval,
DataIDs: []*message.DataID{},
QoS: message.QoSUnreliable,
Persist: false,
FlushPolicy: &flushPolicyIntervalOrBufferSize{
BufferPolicy: &flushPolicyBufferSizeOnly{BufferSize: uint32(defaultFlushBufferSize)},
IntervalPolicy: &flushPolicyIntervalOnly{Interval: defaultFlushInterval},
},
AckTimeout: 0,
ReceiveAckHooker: nil,
SendDataPointsHooker: nil,
ClosedEventHandler: nopUpstreamClosedEventHandler{},
ResumedEventHandler: nopUpstreamResumedEventHandler{},
}
type (
UpstreamOption func(conf *UpstreamConfig)
)
// WithUpstreamAckIntervalは、Ackの返却間隔を設定します。
func WithUpstreamAckInterval(ackInterval time.Duration) UpstreamOption {
return func(opt *UpstreamConfig) {
opt.AckInterval = &ackInterval
}
}
// WithUpstreamCloseTimeoutは、Close時のタイムアウトを設定します。
func WithUpstreamCloseTimeout(timeout time.Duration) UpstreamOption {
return func(opt *UpstreamConfig) {
opt.CloseTimeout = &timeout
}
}
// WithUpstreamExpiryIntervalは、有効期限を設定します。
func WithUpstreamExpiryInterval(expiryInterval time.Duration) UpstreamOption {
return func(opt *UpstreamConfig) {
opt.ExpiryInterval = expiryInterval
}
}
// WithUpstreamDataIDsは、データIDリストを設定します。
func WithUpstreamDataIDs(dataIDs []*message.DataID) UpstreamOption {
return func(opt *UpstreamConfig) {
opt.DataIDs = dataIDs
}
}
// WithUpstreamQoSは、QoSを設定します。
func WithUpstreamQoS(qoS message.QoS) UpstreamOption {
return func(opt *UpstreamConfig) {
opt.QoS = qoS
}
}
// WithUpstreamPersistは、永続化するかどうかを設定します。
func WithUpstreamPersist() UpstreamOption {
return func(opt *UpstreamConfig) {
opt.Persist = true
}
}
// WithUpstreamFlushPolicyは、フラッシュポリシーを設定します。
func WithUpstreamFlushPolicy(policy FlushPolicy) UpstreamOption {
return func(opt *UpstreamConfig) {
opt.FlushPolicy = policy
}
}
// WithUpstreamFlushPolicyNoneは、アップストリームの内部バッファをフラッシュしないポリシーを設定します。
//
// このポリシーを指定した場合、内部バッファのフラッシュはUpstreamのFlushメソッドを使用して、明示的にフラッシュを行う必要があります。
func WithUpstreamFlushPolicyNone() UpstreamOption {
return func(opt *UpstreamConfig) {
opt.FlushPolicy = &flushPolicyNone{}
}
}
// WithUpstreamFlushPolicyIntervalOnlyは、アップストリームのデータポイントの内部バッファを時間間隔でフラッシュするポリシーを設定します。
func WithUpstreamFlushPolicyIntervalOnly(interval time.Duration) UpstreamOption {
return func(opt *UpstreamConfig) {
opt.FlushPolicy = &flushPolicyIntervalOnly{Interval: interval}
}
}
// WithUpstreamFlushPolicyIntervalOrBufferSizeは、アップストリームのデータポイントの内部バッファを時間間隔、または指定したバッファサイズを超えた時にフラッシュするポリシーを設定します。
func WithUpstreamFlushPolicyIntervalOrBufferSize(interval time.Duration, bufferSize uint32) UpstreamOption {
return func(opt *UpstreamConfig) {
opt.FlushPolicy = &flushPolicyIntervalOrBufferSize{
BufferPolicy: &flushPolicyBufferSizeOnly{BufferSize: bufferSize},
IntervalPolicy: &flushPolicyIntervalOnly{Interval: interval},
}
}
}
// WithUpstreamFlushPolicyBufferSizeOnlyは、アップストリームのデータポイントの内部バッファを指定したバッファサイズを超えた時にフラッシュするポリシーを設定します。
func WithUpstreamFlushPolicyBufferSizeOnly(bufferSize uint32) UpstreamOption {
return func(opt *UpstreamConfig) {
opt.FlushPolicy = &flushPolicyBufferSizeOnly{BufferSize: bufferSize}
}
}
// WithUpstreamFlushPolicyImmediatelyは、アップストリームのデータポイントをバッファに書き込んだタイミングで即時フラッシュするポリシーを設定します。
func WithUpstreamFlushPolicyImmediately() UpstreamOption {
return func(opt *UpstreamConfig) {
opt.FlushPolicy = &flushPolicyImmediately{}
}
}
// WithUpstreamReceiveAckHookerは、Ack受信時のフックを設定します。
func WithUpstreamReceiveAckHooker(hooker ReceiveAckHooker) UpstreamOption {
return func(opt *UpstreamConfig) {
opt.ReceiveAckHooker = hooker
}
}
// WithUpstreamSendDataPointsHookerは、データポイント送信時のフックを設定します。
func WithUpstreamSendDataPointsHooker(hooker SendDataPointsHooker) UpstreamOption {
return func(opt *UpstreamConfig) {
opt.SendDataPointsHooker = hooker
}
}
// UpstreamCloseOptionは、Upstreamをクローズする時のオプションです。
type UpstreamCloseOption func(opts *upstreamCloseOptions)
var defaultUpstreamCloseOption = upstreamCloseOptions{
CloseSession: false,
}
// upstreamCloseOptionsは、Upstreamクローズ時のオプションです。
type upstreamCloseOptions struct {
CloseSession bool // セッションをクローズするかどうか
}
// WithUpstreamCloseEnableCloseSessionは、Upstreamクローズ時のセッションクローズを無効化します
func WithUpstreamCloseEnableCloseSession() UpstreamCloseOption {
return func(opts *upstreamCloseOptions) {
opts.CloseSession = true
}
}
// WithUpstreamResumedEventHandlerは、アップストリームが再開されたときのイベントハンドラの設定をします。
func WithUpstreamResumedEventHandler(h UpstreamResumedEventHandler) UpstreamOption {
return func(o *UpstreamConfig) {
o.ResumedEventHandler = h
}
}
// WithUpstreamClosedEventHandlerは、アップストリームがクローズされたときのイベントハンドラの設定をします。
func WithUpstreamClosedEventHandler(h UpstreamClosedEventHandler) UpstreamOption {
return func(o *UpstreamConfig) {
o.ClosedEventHandler = h
}
}
// WithUpstreamAckTimeoutは、Ackタイムアウトを指定します。 `0` の場合は無視されます。
func WithUpstreamAckTimeout(timeout time.Duration) UpstreamOption {
return func(o *UpstreamConfig) {
o.AckTimeout = timeout
}
}