forked from influxdata/influxdb-client-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriteOptions.cs
171 lines (154 loc) · 6.74 KB
/
WriteOptions.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
using System.Reactive.Concurrency;
using InfluxDB.Client.Core;
namespace InfluxDB.Client
{
/// <summary>
/// WriteOptions are used to configure writes the data point into InfluxDB 2.0.
///
/// <para>
///The default setting use the batching configured to (consistent with Telegraf):
/// <list>
/// <item><term>batchSize</term><description>1000</description></item>
/// <item><term>flushInterval</term><description>1000 ms</description></item>
/// <item><term>retryInterval</term><description>1000 ms</description></item>
/// <item><term>jitterInterval</term><description>0</description></item>
/// <item><term>bufferLimit</term><description>10 000</description></item>
/// </list>
/// </para>
///
/// </summary>
public class WriteOptions
{
private const int DefaultBatchSize = 1000;
private const int DefaultFlushInterval = 1000;
private const int DefaultJitterInterval = 0;
private const int DefaultRetryInterval = 1000;
/// <summary>
/// The number of data point to collect in batch.
/// </summary>
/// <seealso cref="Builder.BatchSize(int)"/>
internal int BatchSize { get; }
/// <summary>
/// The time to wait at most (milliseconds).
/// </summary>
/// <seealso cref="Builder.FlushInterval(int)"/>
internal int FlushInterval { get; }
/// <summary>
/// The batch flush jitter interval value (milliseconds).
/// </summary>
/// <seealso cref="Builder.JitterInterval(int)"/>
internal int JitterInterval { get; }
/// <summary>
/// The time to wait before retry unsuccessful write (milliseconds).
/// <para>
/// The retry interval is used when the InfluxDB server does not specify "Retry-After" header.
/// </para>
/// <para>
/// Retry-After: A non-negative decimal integer indicating the seconds to delay after the response is received.
/// </para>
/// </summary>
/// <seealso cref="Builder.RetryInterval(int)"/>
public int RetryInterval { get; }
/// <summary>
/// Set the scheduler which is used for write data points.
/// </summary>
/// <seealso cref="Builder.WriteScheduler(IScheduler)"/>
internal IScheduler WriteScheduler { get; }
private WriteOptions(Builder builder)
{
Arguments.CheckNotNull(builder, "builder");
BatchSize = builder.BatchSizeBuilder;
FlushInterval = builder.FlushIntervalBuilder;
JitterInterval = builder.JitterIntervalBuilder;
RetryInterval = builder.RetryIntervalBuilder;
WriteScheduler = builder.WriteSchedulerBuilder;
}
/// <summary>
/// Create a <see cref="WriteOptions"/> builder.
/// </summary>
/// <returns>builder</returns>
public static Builder CreateNew() {
return new Builder();
}
public sealed class Builder
{
internal int BatchSizeBuilder = DefaultBatchSize;
internal int FlushIntervalBuilder = DefaultFlushInterval;
internal int JitterIntervalBuilder = DefaultJitterInterval;
internal int RetryIntervalBuilder = DefaultRetryInterval;
internal IScheduler WriteSchedulerBuilder = NewThreadScheduler.Default;
/// <summary>
/// Set the number of data point to collect in batch.
/// </summary>
/// <param name="batchSize">the number of data point to collect in batch</param>
/// <returns>this</returns>
public Builder BatchSize(int batchSize)
{
Arguments.CheckPositiveNumber(batchSize, "batchSize");
BatchSizeBuilder = batchSize;
return this;
}
/// <summary>
/// Set the time to wait at most (milliseconds).
/// </summary>
/// <param name="milliseconds">the time to wait at most (milliseconds).</param>
/// <returns>this</returns>
public Builder FlushInterval(int milliseconds)
{
Arguments.CheckPositiveNumber(milliseconds, "flushInterval");
FlushIntervalBuilder = milliseconds;
return this;
}
/// <summary>
/// Jitters the batch flush interval by a random amount.
/// This is primarily to avoid large write spikes for users running a large number of client instances.
/// ie, a jitter of 5s and flush duration 10s means flushes will happen every 10-15s.
/// </summary>
/// <param name="milliseconds">Jitter interval in milliseconds</param>
/// <returns>this</returns>
public Builder JitterInterval(int milliseconds)
{
Arguments.CheckNotNegativeNumber(milliseconds, "jitterInterval");
JitterIntervalBuilder = milliseconds;
return this;
}
/// <summary>
/// Set the the time to wait before retry unsuccessful write (milliseconds).
/// <para>
/// The retry interval is used when the InfluxDB server does not specify "Retry-After" header.
/// </para>
/// <para>
/// Retry-After: A non-negative decimal integer indicating the seconds to delay after the response is received.
/// </para>
/// </summary>
/// <param name="milliseconds">the time to wait before retry unsuccessful write</param>
/// <returns>this</returns>
public Builder RetryInterval(int milliseconds)
{
Arguments.CheckPositiveNumber(milliseconds, "retryInterval");
RetryIntervalBuilder = milliseconds;
return this;
}
/// <summary>
/// Set the scheduler which is used for write data points. It is useful for disabling batch writes or
/// for tuning the performance. Default value is <see cref="Scheduler.CurrentThread"/>
/// </summary>
/// <param name="writeScheduler"></param>
/// <returns></returns>
public Builder WriteScheduler(IScheduler writeScheduler)
{
Arguments.CheckNotNull(writeScheduler, "Write scheduler");
WriteSchedulerBuilder = writeScheduler;
return this;
}
/// <summary>
/// Build an instance of WriteOptions.
/// </summary>
/// <returns><see cref="WriteOptions"/></returns>
public WriteOptions Build()
{
return new WriteOptions(this);
}
}
}
}