-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
162 lines (150 loc) · 4.69 KB
/
env.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
155
156
157
158
159
160
161
162
package envx
import (
"context"
"errors"
"fmt"
"github.com/caarlos0/env/v6"
_ "github.com/joho/godotenv/autoload"
"github.com/nats-io/nats.go"
"github.com/redis/go-redis/v9"
"k8s.io/klog/v2"
"strings"
"sync"
"time"
)
func LoadEnv(v any, opts ...env.Options) error {
return env.Parse(v, opts...)
}
func MustLoadEnv(v any, opts ...env.Options) {
if err := LoadEnv(v, opts...); err != nil {
klog.Fatal(err)
}
}
type rdbEnvLoaderOptions struct {
envOptions env.Options
autoLoadProjectName string
notifyMq *nats.Conn
pendingLock *sync.RWMutex
ErrorAtNotFound bool
loadTimeout time.Duration
}
type RdbEnvLoaderOption func(options *rdbEnvLoaderOptions)
func WithRdbEnvCustomOptions(opt env.Options) RdbEnvLoaderOption {
return func(options *rdbEnvLoaderOptions) {
options.envOptions = opt
}
}
func WithRdbEnvAutoLoad(projectName string, mq *nats.Conn, lock *sync.RWMutex) RdbEnvLoaderOption {
return func(options *rdbEnvLoaderOptions) {
options.autoLoadProjectName = projectName
options.notifyMq = mq
options.pendingLock = lock
}
}
func WithRdbEnvErrorAtNotFound(at bool) RdbEnvLoaderOption {
return func(options *rdbEnvLoaderOptions) {
options.ErrorAtNotFound = at
}
}
func WithRdbEnvLoadTimeout(timeout time.Duration) RdbEnvLoaderOption {
return func(options *rdbEnvLoaderOptions) {
options.loadTimeout = timeout
}
}
func LoadEnvFromRedis(v any, r *redis.Client, key string, option ...RdbEnvLoaderOption) error {
opt := &rdbEnvLoaderOptions{loadTimeout: time.Second * 3}
for _, o := range option {
o(opt)
}
ctx, cancel := context.WithTimeout(context.Background(), opt.loadTimeout)
defer cancel()
val, err := r.HGetAll(ctx, key).Result()
if errors.Is(err, redis.Nil) {
if opt.ErrorAtNotFound {
return fmt.Errorf("LoadEnvFromRedis: key[%s] not found", key)
}
klog.Warningf("LoadEnvFromRedis: key[%s] not found", key)
}
if err != nil {
return err
}
opt.envOptions.Environment = val
err = LoadEnv(v, opt.envOptions)
if err != nil {
return err
}
// auto load
if opt.autoLoadProjectName != "" && opt.notifyMq != nil && opt.pendingLock != nil {
subjectPrefix := fmt.Sprintf("%s.%s", "envAutoLoad", opt.autoLoadProjectName)
handler := envAutoReloadHandler(v, key, r, subjectPrefix, opt)
if _, err := opt.notifyMq.Subscribe(subjectPrefix, handler); err != nil {
return fmt.Errorf("failed to subscribe to envAutoLoad subject [%s]: %s", opt.autoLoadProjectName, err.Error())
}
if _, err := opt.notifyMq.Subscribe(subjectPrefix+".>", handler); err != nil {
return fmt.Errorf("failed to subscribe to envAutoLoad subject [%s]: %s", opt.autoLoadProjectName+".>", err.Error())
}
klog.Infof("LoadEnvFromRedis: setup autoload at: %s", subjectPrefix)
}
return nil
}
func MustLoadEnvFromRedis(v any, r *redis.Client, key string, option ...RdbEnvLoaderOption) {
if err := LoadEnvFromRedis(v, r, key, option...); err != nil {
klog.Fatal(err)
}
}
func envAutoReloadHandler(v any, key string, r *redis.Client, subjectPrefix string, opt *rdbEnvLoaderOptions) func(msg *nats.Msg) {
return func(msg *nats.Msg) {
opt.pendingLock.Lock()
defer opt.pendingLock.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), opt.loadTimeout)
defer cancel()
subject := strings.TrimPrefix(msg.Subject, subjectPrefix)
if subject == "" {
// load all
klog.Info("[AutoRedisEnv]Auto reloading all config")
val, err := r.HGetAll(ctx, key).Result()
if errors.Is(err, redis.Nil) {
errMsg := fmt.Sprintf("LoadEnvFromRedis: key[%s] not found", key)
klog.Errorf("[AutoRedisEnv]%s", errMsg)
_ = msg.Respond([]byte(errMsg))
return
}
if err != nil {
klog.Errorf("[AutoRedisEnv]%s", err.Error())
_ = msg.Respond([]byte(err.Error()))
return
}
opt.envOptions.Environment = val
if err := LoadEnv(v, opt.envOptions); err != nil {
klog.Errorf("[AutoRedisEnv]%s", err.Error())
_ = msg.Respond([]byte(err.Error()))
return
}
_ = msg.Respond([]byte("ok"))
return
}
subject = strings.TrimPrefix(subject, ".")
// load single
klog.Infof("[AutoRedisEnv]Auto reloading config: %s", subject)
val, err := r.HGet(ctx, key, subject).Result()
if errors.Is(err, redis.Nil) {
errMsg := fmt.Sprintf("LoadEnvFromRedis: key[%s]->field[%s] not found", key, subject)
klog.Errorf("[AutoRedisEnv]%s", errMsg)
_ = msg.Respond([]byte(errMsg))
return
}
if err != nil {
klog.Errorf("[AutoRedisEnv]%s", err.Error())
_ = msg.Respond([]byte(err.Error()))
return
}
opt.envOptions.Environment[subject] = val
if err := LoadEnv(v, opt.envOptions); err != nil {
klog.Errorf("[AutoRedisEnv]%s", err.Error())
_ = msg.Respond([]byte(err.Error()))
return
}
_ = msg.Respond([]byte("ok"))
return
}
}