options.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. package write
  5. import (
  6. "time"
  7. )
  8. // Options holds write configuration properties
  9. type Options struct {
  10. // Maximum number of points sent to server in single request. Default 5000
  11. batchSize uint
  12. // Interval, in ms, in which is buffer flushed if it has not been already written (by reaching batch size) . Default 1000ms
  13. flushInterval uint
  14. // Precision to use in writes for timestamp. In unit of duration: time.Nanosecond, time.Microsecond, time.Millisecond, time.Second
  15. // Default time.Nanosecond
  16. precision time.Duration
  17. // Whether to use GZip compression in requests. Default false
  18. useGZip bool
  19. // Tags added to each point during writing. If a point already has a tag with the same key, it is left unchanged.
  20. defaultTags map[string]string
  21. // Default retry interval in ms, if not sent by server. Default 5,000.
  22. retryInterval uint
  23. // Maximum count of retry attempts of failed writes, default 5.
  24. maxRetries uint
  25. // Maximum number of points to keep for retry. Should be multiple of BatchSize. Default 50,000.
  26. retryBufferLimit uint
  27. // The maximum delay between each retry attempt in milliseconds, default 125,000.
  28. maxRetryInterval uint
  29. // The maximum total retry timeout in millisecond, default 180,000.
  30. maxRetryTime uint
  31. // The base for the exponential retry delay
  32. exponentialBase uint
  33. // InfluxDB Enterprise write consistency as explained in https://docs.influxdata.com/enterprise_influxdb/v1.9/concepts/clustering/#write-consistency
  34. consistency Consistency
  35. }
  36. const (
  37. // ConsistencyOne requires at least one data node acknowledged a write.
  38. ConsistencyOne Consistency = "one"
  39. // ConsistencyAll requires all data nodes to acknowledge a write.
  40. ConsistencyAll Consistency = "all"
  41. // ConsistencyQuorum requires a quorum of data nodes to acknowledge a write.
  42. ConsistencyQuorum Consistency = "quorum"
  43. // ConsistencyAny allows for hinted hand off, potentially no write happened yet.
  44. ConsistencyAny Consistency = "any"
  45. )
  46. // Consistency defines enum for allows consistency values for InfluxDB Enterprise, as explained https://docs.influxdata.com/enterprise_influxdb/v1.9/concepts/clustering/#write-consistency
  47. type Consistency string
  48. // BatchSize returns size of batch
  49. func (o *Options) BatchSize() uint {
  50. return o.batchSize
  51. }
  52. // SetBatchSize sets number of points sent in single request
  53. func (o *Options) SetBatchSize(batchSize uint) *Options {
  54. o.batchSize = batchSize
  55. return o
  56. }
  57. // FlushInterval returns flush interval in ms
  58. func (o *Options) FlushInterval() uint {
  59. return o.flushInterval
  60. }
  61. // SetFlushInterval sets flush interval in ms in which is buffer flushed if it has not been already written
  62. func (o *Options) SetFlushInterval(flushIntervalMs uint) *Options {
  63. o.flushInterval = flushIntervalMs
  64. return o
  65. }
  66. // RetryInterval returns the default retry interval in ms, if not sent by server. Default 5,000.
  67. func (o *Options) RetryInterval() uint {
  68. return o.retryInterval
  69. }
  70. // SetRetryInterval sets the time to wait before retry unsuccessful write in ms, if not sent by server
  71. func (o *Options) SetRetryInterval(retryIntervalMs uint) *Options {
  72. o.retryInterval = retryIntervalMs
  73. return o
  74. }
  75. // MaxRetries returns maximum count of retry attempts of failed writes, default 5.
  76. func (o *Options) MaxRetries() uint {
  77. return o.maxRetries
  78. }
  79. // SetMaxRetries sets maximum count of retry attempts of failed writes.
  80. // Setting zero value disables retry strategy.
  81. func (o *Options) SetMaxRetries(maxRetries uint) *Options {
  82. o.maxRetries = maxRetries
  83. return o
  84. }
  85. // RetryBufferLimit returns retry buffer limit.
  86. func (o *Options) RetryBufferLimit() uint {
  87. return o.retryBufferLimit
  88. }
  89. // SetRetryBufferLimit sets maximum number of points to keep for retry. Should be multiple of BatchSize.
  90. func (o *Options) SetRetryBufferLimit(retryBufferLimit uint) *Options {
  91. o.retryBufferLimit = retryBufferLimit
  92. return o
  93. }
  94. // MaxRetryInterval returns the maximum delay between each retry attempt in milliseconds, default 125,000.
  95. func (o *Options) MaxRetryInterval() uint {
  96. return o.maxRetryInterval
  97. }
  98. // SetMaxRetryInterval sets the maximum delay between each retry attempt in millisecond
  99. func (o *Options) SetMaxRetryInterval(maxRetryIntervalMs uint) *Options {
  100. o.maxRetryInterval = maxRetryIntervalMs
  101. return o
  102. }
  103. // MaxRetryTime returns the maximum total retry timeout in millisecond, default 180,000.
  104. func (o *Options) MaxRetryTime() uint {
  105. return o.maxRetryTime
  106. }
  107. // SetMaxRetryTime sets the maximum total retry timeout in millisecond.
  108. func (o *Options) SetMaxRetryTime(maxRetryTimeMs uint) *Options {
  109. o.maxRetryTime = maxRetryTimeMs
  110. return o
  111. }
  112. // ExponentialBase returns the base for the exponential retry delay. Default 2.
  113. func (o *Options) ExponentialBase() uint {
  114. return o.exponentialBase
  115. }
  116. // SetExponentialBase sets the base for the exponential retry delay.
  117. func (o *Options) SetExponentialBase(retryExponentialBase uint) *Options {
  118. o.exponentialBase = retryExponentialBase
  119. return o
  120. }
  121. // Precision returns time precision for writes
  122. func (o *Options) Precision() time.Duration {
  123. return o.precision
  124. }
  125. // SetPrecision sets time precision to use in writes for timestamp. In unit of duration: time.Nanosecond, time.Microsecond, time.Millisecond, time.Second
  126. func (o *Options) SetPrecision(precision time.Duration) *Options {
  127. o.precision = precision
  128. return o
  129. }
  130. // UseGZip returns true if write request are gzip`ed
  131. func (o *Options) UseGZip() bool {
  132. return o.useGZip
  133. }
  134. // SetUseGZip specifies whether to use GZip compression in write requests.
  135. func (o *Options) SetUseGZip(useGZip bool) *Options {
  136. o.useGZip = useGZip
  137. return o
  138. }
  139. // AddDefaultTag adds a default tag. DefaultTags are added to each written point.
  140. // If a tag with the same key already exist it is overwritten.
  141. // If a point already defines such a tag, it is left unchanged.
  142. func (o *Options) AddDefaultTag(key, value string) *Options {
  143. o.DefaultTags()[key] = value
  144. return o
  145. }
  146. // DefaultTags returns set of default tags
  147. func (o *Options) DefaultTags() map[string]string {
  148. if o.defaultTags == nil {
  149. o.defaultTags = make(map[string]string)
  150. }
  151. return o.defaultTags
  152. }
  153. // Consistency returns consistency for param value
  154. func (o *Options) Consistency() Consistency {
  155. return o.consistency
  156. }
  157. // SetConsistency allows setting InfluxDB Enterprise write consistency, as explained in https://docs.influxdata.com/enterprise_influxdb/v1.9/concepts/clustering/#write-consistency */
  158. func (o *Options) SetConsistency(consistency Consistency) *Options {
  159. o.consistency = consistency
  160. return o
  161. }
  162. // DefaultOptions returns Options object with default values
  163. func DefaultOptions() *Options {
  164. return &Options{batchSize: 5_000, flushInterval: 1_000, precision: time.Nanosecond, useGZip: false, retryBufferLimit: 50_000, defaultTags: make(map[string]string),
  165. maxRetries: 5, retryInterval: 5_000, maxRetryInterval: 125_000, maxRetryTime: 180_000, exponentialBase: 2}
  166. }