-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFbOptionsBuilder.cs
196 lines (160 loc) · 5.8 KB
/
FbOptionsBuilder.cs
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
using System;
using FeatBit.Sdk.Server.Bootstrapping;
using FeatBit.Sdk.Server.Retry;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace FeatBit.Sdk.Server.Options
{
public class FbOptionsBuilder
{
private TimeSpan _startWaitTime;
private bool _offline;
private readonly string _envSecret;
private Uri _streamingUri;
private Uri _eventUri;
private TimeSpan _connectTimeout;
private TimeSpan _closeTimeout;
private TimeSpan _keepAliveInterval;
private TimeSpan[] _reconnectRetryDelays;
private int _maxFlushWorker;
private TimeSpan _autoFlushInterval;
private TimeSpan _flushTimeout;
private int _maxEventsInQueue;
private int _maxEventPerRequest;
private int _maxSendEventAttempts;
private TimeSpan _sendEventRetryInterval;
private IBootstrapProvider _bootstrapProvider;
private ILoggerFactory _loggerFactory;
public FbOptionsBuilder() : this(string.Empty)
{
}
public FbOptionsBuilder(string envSecret)
{
_startWaitTime = TimeSpan.FromSeconds(5);
_offline = false;
_envSecret = envSecret;
// uris
_streamingUri = new Uri("ws://localhost:5100");
_eventUri = new Uri("http://localhost:5100");
// websocket configs
_connectTimeout = TimeSpan.FromSeconds(3);
_closeTimeout = TimeSpan.FromSeconds(2);
_keepAliveInterval = TimeSpan.FromSeconds(15);
_reconnectRetryDelays = DefaultRetryPolicy.DefaultRetryDelays;
// event configs
_maxFlushWorker = Math.Min(Math.Max(Environment.ProcessorCount / 2, 1), 4);
_autoFlushInterval = TimeSpan.FromSeconds(5);
_flushTimeout = TimeSpan.FromSeconds(5);
_maxEventsInQueue = 10_000;
_maxEventPerRequest = 50;
_maxSendEventAttempts = 2;
_sendEventRetryInterval = TimeSpan.FromMilliseconds(200);
_bootstrapProvider = new NullBootstrapProvider();
_loggerFactory = NullLoggerFactory.Instance;
}
public FbOptions Build()
{
return new FbOptions(_startWaitTime, _offline, _envSecret, _streamingUri, _eventUri, _connectTimeout,
_closeTimeout, _keepAliveInterval, _reconnectRetryDelays, _maxFlushWorker, _autoFlushInterval,
_flushTimeout, _maxEventsInQueue, _maxEventPerRequest, _maxSendEventAttempts, _sendEventRetryInterval,
_bootstrapProvider, _loggerFactory);
}
public FbOptionsBuilder StartWaitTime(TimeSpan timeout)
{
if (timeout < _connectTimeout)
{
throw new InvalidOperationException("The start wait time must be greater than the connect timeout.");
}
_startWaitTime = timeout;
return this;
}
public FbOptionsBuilder Offline(bool offline)
{
_offline = offline;
return this;
}
public FbOptionsBuilder Steaming(Uri uri)
{
_streamingUri = uri;
return this;
}
public FbOptionsBuilder Event(Uri uri)
{
_eventUri = uri;
return this;
}
public FbOptionsBuilder ConnectTimeout(TimeSpan timeout)
{
if (timeout > _startWaitTime)
{
throw new InvalidOperationException("The connect timeout must be lower than the start wait time.");
}
_connectTimeout = timeout;
return this;
}
public FbOptionsBuilder CloseTimeout(TimeSpan timeout)
{
_closeTimeout = timeout;
return this;
}
public FbOptionsBuilder MaxFlushWorker(int maxFlushWorker)
{
_maxFlushWorker = maxFlushWorker;
return this;
}
public FbOptionsBuilder AutoFlushInterval(TimeSpan autoFlushInterval)
{
_autoFlushInterval = autoFlushInterval;
return this;
}
public FbOptionsBuilder FlushTimeout(TimeSpan timeout)
{
_flushTimeout = timeout;
return this;
}
public FbOptionsBuilder MaxEventsInQueue(int maxEventsInQueue)
{
_maxEventsInQueue = maxEventsInQueue;
return this;
}
public FbOptionsBuilder MaxEventPerRequest(int maxEventPerRequest)
{
_maxEventPerRequest = maxEventPerRequest;
return this;
}
public FbOptionsBuilder MaxSendEventAttempts(int maxSendEventAttempts)
{
_maxSendEventAttempts = maxSendEventAttempts;
return this;
}
public FbOptionsBuilder SendEventRetryInterval(TimeSpan sendEventRetryInterval)
{
_sendEventRetryInterval = sendEventRetryInterval;
return this;
}
public FbOptionsBuilder KeepAliveInterval(TimeSpan interval)
{
_keepAliveInterval = interval;
return this;
}
public FbOptionsBuilder ReconnectRetryDelays(TimeSpan[] delays)
{
_reconnectRetryDelays = delays;
return this;
}
public FbOptionsBuilder LoggerFactory(ILoggerFactory loggerFactory)
{
_loggerFactory = loggerFactory;
return this;
}
public FbOptionsBuilder UseJsonBootstrapProvider(string json)
{
if (!_offline)
{
throw new InvalidOperationException("Bootstrap provider can only be set when offline mode is enabled.");
}
_bootstrapProvider = new JsonBootstrapProvider(json);
return this;
}
}
}