forked from hyperledger/fabric-sdk-go
/
opts.go
executable file
·192 lines (161 loc) · 5.27 KB
/
opts.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package client
import (
"time"
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/events/client/dispatcher"
)
type params struct {
connEventCh chan *dispatcher.ConnectionEvent
reconnInitialDelay time.Duration
timeBetweenConnAttempts time.Duration
respTimeout time.Duration
eventConsumerBufferSize uint
maxConnAttempts uint
maxReconnAttempts uint
permitBlockEvents bool
reconn bool
}
func defaultParams() *params {
return ¶ms{
eventConsumerBufferSize: 100,
reconn: true,
maxConnAttempts: 1,
maxReconnAttempts: 0, // Try forever
reconnInitialDelay: 0,
timeBetweenConnAttempts: 5 * time.Second,
respTimeout: 5 * time.Second,
}
}
// WithBlockEvents indicates that block events are to be received.
// Note that the caller must have sufficient privileges for this option.
func WithBlockEvents() options.Opt {
return func(p options.Params) {
if setter, ok := p.(permitBlockEventsSetter); ok {
setter.PermitBlockEvents()
}
}
}
// WithReconnect indicates whether the client should automatically attempt to reconnect
// to the server after a connection has been lost
func WithReconnect(value bool) options.Opt {
return func(p options.Params) {
if setter, ok := p.(reconnectSetter); ok {
setter.SetReconnect(value)
}
}
}
// WithMaxConnectAttempts sets the maximum number of times that the client will attempt
// to connect to the server. If set to 0 then the client will try until it is stopped.
func WithMaxConnectAttempts(value uint) options.Opt {
return func(p options.Params) {
if setter, ok := p.(maxConnectAttemptsSetter); ok {
setter.SetMaxConnectAttempts(value)
}
}
}
// WithMaxReconnectAttempts sets the maximum number of times that the client will attempt
// to reconnect to the server after a connection has been lost. If set to 0 then the
// client will try until it is stopped.
func WithMaxReconnectAttempts(value uint) options.Opt {
return func(p options.Params) {
if setter, ok := p.(maxReconnectAttemptsSetter); ok {
setter.SetMaxReconnectAttempts(value)
}
}
}
// WithReconnectInitialDelay sets the initial delay before attempting to reconnect.
func WithReconnectInitialDelay(value time.Duration) options.Opt {
return func(p options.Params) {
if setter, ok := p.(reconnectInitialDelaySetter); ok {
setter.SetReconnectInitialDelay(value)
}
}
}
// WithConnectionEvent sets the channel that is to receive connection events, i.e. when the client connects and/or
// disconnects from the channel event service.
func WithConnectionEvent(value chan *dispatcher.ConnectionEvent) options.Opt {
return func(p options.Params) {
if setter, ok := p.(connectEventChSetter); ok {
setter.SetConnectEventCh(value)
}
}
}
// WithTimeBetweenConnectAttempts sets the time between connection attempts.
func WithTimeBetweenConnectAttempts(value time.Duration) options.Opt {
return func(p options.Params) {
if setter, ok := p.(timeBetweenConnectAttemptsSetter); ok {
setter.SetTimeBetweenConnectAttempts(value)
}
}
}
// WithResponseTimeout sets the timeout when waiting for a response from the event server
func WithResponseTimeout(value time.Duration) options.Opt {
return func(p options.Params) {
if setter, ok := p.(responseTimeoutSetter); ok {
setter.SetResponseTimeout(value)
}
}
}
func (p *params) SetEventConsumerBufferSize(value uint) {
p.eventConsumerBufferSize = value
}
func (p *params) SetReconnect(value bool) {
logger.Debugf("Reconnect: %t", value)
p.reconn = value
}
func (p *params) SetMaxConnectAttempts(value uint) {
logger.Debugf("MaxConnectAttempts: %d", value)
p.maxConnAttempts = value
}
func (p *params) SetMaxReconnectAttempts(value uint) {
logger.Debugf("MaxReconnectAttempts: %d", value)
p.maxReconnAttempts = value
}
func (p *params) SetReconnectInitialDelay(value time.Duration) {
logger.Debugf("ReconnectInitialDelay: %s", value)
p.reconnInitialDelay = value
}
func (p *params) SetTimeBetweenConnectAttempts(value time.Duration) {
logger.Debugf("TimeBetweenConnectAttempts: %d", value)
p.timeBetweenConnAttempts = value
}
func (p *params) SetConnectEventCh(value chan *dispatcher.ConnectionEvent) {
logger.Debugf("ConnectEventCh: %#v", value)
p.connEventCh = value
}
func (p *params) SetResponseTimeout(value time.Duration) {
logger.Debugf("ResponseTimeout: %s", value)
p.respTimeout = value
}
func (p *params) PermitBlockEvents() {
logger.Debugf("PermitBlockEvents")
p.permitBlockEvents = true
}
type reconnectSetter interface {
SetReconnect(value bool)
}
type maxConnectAttemptsSetter interface {
SetMaxConnectAttempts(value uint)
}
type maxReconnectAttemptsSetter interface {
SetMaxReconnectAttempts(value uint)
}
type reconnectInitialDelaySetter interface {
SetReconnectInitialDelay(value time.Duration)
}
type connectEventChSetter interface {
SetConnectEventCh(value chan *dispatcher.ConnectionEvent)
}
type timeBetweenConnectAttemptsSetter interface {
SetTimeBetweenConnectAttempts(value time.Duration)
}
type responseTimeoutSetter interface {
SetResponseTimeout(value time.Duration)
}
type permitBlockEventsSetter interface {
PermitBlockEvents()
}