1// Copyright 2020-2021 InfluxData, Inc. All rights reserved.
2// Use of this source code is governed by MIT
3// license that can be found in the LICENSE file.
4
5package write
6
7import (
8	"time"
9)
10
11// Options holds write configuration properties
12type Options struct {
13	// Maximum number of points sent to server in single request. Default 5000
14	batchSize uint
15	// Interval, in ms, in which is buffer flushed if it has not been already written (by reaching batch size) . Default 1000ms
16	flushInterval uint
17	// Precision to use in writes for timestamp. In unit of duration: time.Nanosecond, time.Microsecond, time.Millisecond, time.Second
18	// Default time.Nanosecond
19	precision time.Duration
20	// Whether to use GZip compression in requests. Default false
21	useGZip bool
22	// Tags added to each point during writing. If a point already has a tag with the same key, it is left unchanged.
23	defaultTags map[string]string
24	// Default retry interval in ms, if not sent by server. Default 5,000ms
25	retryInterval uint
26	// Maximum count of retry attempts of failed writes
27	maxRetries uint
28	// Maximum number of points to keep for retry. Should be multiple of BatchSize. Default 50,000
29	retryBufferLimit uint
30	// Maximum retry interval, default 5min (300,000ms)
31	maxRetryInterval uint
32}
33
34// BatchSize returns size of batch
35func (o *Options) BatchSize() uint {
36	return o.batchSize
37}
38
39// SetBatchSize sets number of points sent in single request
40func (o *Options) SetBatchSize(batchSize uint) *Options {
41	o.batchSize = batchSize
42	return o
43}
44
45// FlushInterval returns flush interval in ms
46func (o *Options) FlushInterval() uint {
47	return o.flushInterval
48}
49
50// SetFlushInterval sets flush interval in ms in which is buffer flushed if it has not been already written
51func (o *Options) SetFlushInterval(flushIntervalMs uint) *Options {
52	o.flushInterval = flushIntervalMs
53	return o
54}
55
56// RetryInterval returns the retry interval in ms
57func (o *Options) RetryInterval() uint {
58	return o.retryInterval
59}
60
61// SetRetryInterval sets retry interval in ms, which is set if not sent by server
62func (o *Options) SetRetryInterval(retryIntervalMs uint) *Options {
63	o.retryInterval = retryIntervalMs
64	return o
65}
66
67// MaxRetries returns maximum count of retry attempts of failed writes
68func (o *Options) MaxRetries() uint {
69	return o.maxRetries
70}
71
72// SetMaxRetries sets maximum count of retry attempts of failed writes.
73// Setting zero value disables retry strategy.
74func (o *Options) SetMaxRetries(maxRetries uint) *Options {
75	o.maxRetries = maxRetries
76	return o
77}
78
79// RetryBufferLimit returns retry buffer limit
80func (o *Options) RetryBufferLimit() uint {
81	return o.retryBufferLimit
82}
83
84// SetRetryBufferLimit sets maximum number of points to keep for retry. Should be multiple of BatchSize.
85func (o *Options) SetRetryBufferLimit(retryBufferLimit uint) *Options {
86	o.retryBufferLimit = retryBufferLimit
87	return o
88}
89
90// MaxRetryInterval return maximum retry interval in ms. Default 5min.
91func (o *Options) MaxRetryInterval() uint {
92	return o.maxRetryInterval
93}
94
95// SetMaxRetryInterval set maximum retry interval in ms
96func (o *Options) SetMaxRetryInterval(maxRetryIntervalMs uint) *Options {
97	o.maxRetryInterval = maxRetryIntervalMs
98	return o
99}
100
101// Precision returns time precision for writes
102func (o *Options) Precision() time.Duration {
103	return o.precision
104}
105
106// SetPrecision sets time precision to use in writes for timestamp. In unit of duration: time.Nanosecond, time.Microsecond, time.Millisecond, time.Second
107func (o *Options) SetPrecision(precision time.Duration) *Options {
108	o.precision = precision
109	return o
110}
111
112// UseGZip returns true if write request are gzip`ed
113func (o *Options) UseGZip() bool {
114	return o.useGZip
115}
116
117// SetUseGZip specifies whether to use GZip compression in write requests.
118func (o *Options) SetUseGZip(useGZip bool) *Options {
119	o.useGZip = useGZip
120	return o
121}
122
123// AddDefaultTag adds a default tag. DefaultTags are added to each written point.
124// If a tag with the same key already exist it is overwritten.
125// If a point already defines such a tag, it is left unchanged.
126func (o *Options) AddDefaultTag(key, value string) *Options {
127	o.DefaultTags()[key] = value
128	return o
129}
130
131// DefaultTags returns set of default tags
132func (o *Options) DefaultTags() map[string]string {
133	if o.defaultTags == nil {
134		o.defaultTags = make(map[string]string)
135	}
136	return o.defaultTags
137}
138
139// DefaultOptions returns Options object with default values
140func DefaultOptions() *Options {
141	return &Options{batchSize: 5000, maxRetries: 3, retryInterval: 5000, maxRetryInterval: 300000, flushInterval: 1000, precision: time.Nanosecond, useGZip: false, retryBufferLimit: 50000, defaultTags: make(map[string]string)}
142}
143