-
Notifications
You must be signed in to change notification settings - Fork 266
/
dynconfig.go
154 lines (128 loc) · 3.85 KB
/
dynconfig.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright 2020 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//go:generate mockgen -destination mocks/dynconfig_mock.go -source dynconfig.go -package mocks
package dynconfig
import (
"errors"
"time"
"github.com/mitchellh/mapstructure"
logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/pkg/cache"
)
type SourceType string
const (
// CacheDirName is dir name of dynconfig cache.
CacheDirName = "dynconfig"
)
const (
// defaultCacheKey represents cache key of dynconfig.
defaultCacheKey = "dynconfig"
)
type Dynconfig interface {
// Unmarshal unmarshals the config into a Struct. Make sure that the tags
// on the fields of the structure are properly set.
Unmarshal(rawVal any) error
// Refresh refreshes dynconfig in cache.
Refresh() error
}
type dynconfig struct {
cache cache.Cache
client ManagerClient
cachePath string
expire time.Duration
}
// New returns a new dynconfig instance.
func New(client ManagerClient, cachePath string, expire time.Duration) (Dynconfig, error) {
d := &dynconfig{
cache: cache.New(expire, cache.NoCleanup),
cachePath: cachePath,
expire: expire,
client: client,
}
if err := d.cache.LoadFile(d.cachePath); err != nil {
if err := d.load(); err != nil {
return nil, err
}
}
return d, nil
}
// Unmarshal unmarshals the config into a Struct. Make sure that the tags
// on the fields of the structure are properly set.
func (d *dynconfig) Unmarshal(rawVal any) error {
dynconfig, err := d.get()
if err != nil {
return errors.New("can't find the cached data")
}
return decode(dynconfig, defaultDecoderConfig(rawVal))
}
// Refresh refreshes dynconfig in cache.
func (d *dynconfig) Refresh() error {
return d.load()
}
// Get dynamic config.
func (d *dynconfig) get() (any, error) {
// Cache has not expired.
dynconfig, _, found := d.cache.GetWithExpiration(defaultCacheKey)
if found {
return dynconfig, nil
}
// Cache has expired, refresh and ignore client request error.
if err := d.load(); err != nil {
logger.Warn("reload failed ", err)
}
dynconfig, ok := d.cache.Get(defaultCacheKey)
if !ok {
return nil, errors.New("can't find the cached data")
}
return dynconfig, nil
}
// Load dynamic config from manager.
func (d *dynconfig) load() error {
dynconfig, err := d.client.Get()
if err != nil {
return err
}
d.cache.Set(defaultCacheKey, dynconfig, d.expire)
if err := d.cache.SaveFile(d.cachePath); err != nil {
return err
}
return nil
}
// A DecoderConfigOption can be passed to dynconfig Unmarshal to configure
// mapstructure.DecoderConfig options.
type DecoderConfigOption func(*mapstructure.DecoderConfig)
// defaultDecoderConfig returns default mapstructure.DecoderConfig with support
// of time.Duration values & string slices.
func defaultDecoderConfig(output any) *mapstructure.DecoderConfig {
c := &mapstructure.DecoderConfig{
Metadata: nil,
Result: output,
WeaklyTypedInput: true,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
),
}
return c
}
// A wrapper around mapstructure.Decode that mimics the WeakDecode functionality.
func decode(input any, config *mapstructure.DecoderConfig) error {
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(input)
}