forked from hashicorp/consul-template
/
dedup.go
132 lines (108 loc) · 2.91 KB
/
dedup.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
package config
import (
"fmt"
"time"
)
const (
// DefaultDedupPrefix is the default prefix used for deduplication mode.
DefaultDedupPrefix = "consul-template/dedup/"
// DefaultDedupTTL is the default TTL for deduplicate mode.
DefaultDedupTTL = 15 * time.Second
// DefaultDedupMaxStale is the default max staleness for the deduplication
// manager.
DefaultDedupMaxStale = DefaultMaxStale
)
// DedupConfig is used to enable the de-duplication mode, which depends
// on electing a leader per-template and watching of a key. This is used
// to reduce the cost of many instances of CT running the same template.
type DedupConfig struct {
// Controls if deduplication mode is enabled
Enabled *bool `mapstructure:"enabled"`
// MaxStale is the maximum amount of time to allow for stale queries.
MaxStale *time.Duration `mapstructure:"max_stale"`
// Controls the KV prefix used. Defaults to defaultDedupPrefix
Prefix *string `mapstructure:"prefix"`
// TTL is the Session TTL used for lock acquisition, defaults to 15 seconds.
TTL *time.Duration `mapstructure:"ttl"`
}
// DefaultDedupConfig returns a configuration that is populated with the
// default values.
func DefaultDedupConfig() *DedupConfig {
return &DedupConfig{}
}
// Copy returns a deep copy of this configuration.
func (c *DedupConfig) Copy() *DedupConfig {
if c == nil {
return nil
}
var o DedupConfig
o.Enabled = c.Enabled
o.MaxStale = c.MaxStale
o.Prefix = c.Prefix
o.TTL = c.TTL
return &o
}
// Merge combines all values in this configuration with the values in the other
// configuration, with values in the other configuration taking precedence.
// Maps and slices are merged, most other values are overwritten. Complex
// structs define their own merge functionality.
func (c *DedupConfig) Merge(o *DedupConfig) *DedupConfig {
if c == nil {
if o == nil {
return nil
}
return o.Copy()
}
if o == nil {
return c.Copy()
}
r := c.Copy()
if o.Enabled != nil {
r.Enabled = o.Enabled
}
if o.MaxStale != nil {
r.MaxStale = o.MaxStale
}
if o.Prefix != nil {
r.Prefix = o.Prefix
}
if o.TTL != nil {
r.TTL = o.TTL
}
return r
}
// Finalize ensures there no nil pointers.
func (c *DedupConfig) Finalize() {
if c.Enabled == nil {
c.Enabled = Bool(false ||
TimeDurationPresent(c.MaxStale) ||
StringPresent(c.Prefix) ||
TimeDurationPresent(c.TTL))
}
if c.MaxStale == nil {
c.MaxStale = TimeDuration(DefaultDedupMaxStale)
}
if c.Prefix == nil {
c.Prefix = String(DefaultDedupPrefix)
}
if c.TTL == nil {
c.TTL = TimeDuration(DefaultDedupTTL)
}
}
// GoString defines the printable version of this struct.
func (c *DedupConfig) GoString() string {
if c == nil {
return "(*DedupConfig)(nil)"
}
return fmt.Sprintf("&DedupConfig{"+
"Enabled:%s, "+
"MaxStale:%s, "+
"Prefix:%s, "+
"TTL:%s"+
"}",
BoolGoString(c.Enabled),
TimeDurationGoString(c.MaxStale),
StringGoString(c.Prefix),
TimeDurationGoString(c.TTL),
)
}