forked from ava-labs/ortelius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connections.go
291 lines (252 loc) · 6.89 KB
/
connections.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// (c) 2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package services
import (
"bytes"
"context"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/corpetty/avalanchego/utils/wrappers"
"github.com/go-redis/redis/v8"
"github.com/gocraft/health"
"github.com/corpetty/ortelius/cfg"
"github.com/corpetty/ortelius/services/cache"
"github.com/corpetty/ortelius/services/db"
)
type Connections struct {
stream *health.Stream
quietStream *health.Stream
streamDBDedup *health.Stream
db *db.Conn
redis *redis.Client
cache *cache.Cache
}
func NewConnectionsFromConfig(conf cfg.Services, ro bool) (*Connections, error) {
// Always create a stream and log
stream := NewStream()
quietStream := NewQuietStream()
streamDBDedup := NewStreamDBDups()
// Create db and redis connections if configured
var (
err error
dbConn *db.Conn
redisClient *redis.Client
)
if conf.Redis != nil && conf.Redis.Addr != "" {
kvs := health.Kvs{"addr": conf.Redis.Addr, "db": strconv.Itoa(conf.Redis.DB)}
redisClient, err = NewRedisConn(&redis.Options{
DB: conf.Redis.DB,
Addr: conf.Redis.Addr,
Password: conf.Redis.Password,
})
if err != nil {
return nil, stream.EventErrKv("connect.redis", err, kvs)
}
stream.EventKv("connect.redis", kvs)
} else {
stream.Event("connect.redis.skip")
}
if conf.DB != nil || conf.DB.Driver == db.DriverNone {
// Setup logging kvs
kvs := health.Kvs{"driver": conf.DB.Driver}
// Create connection
dbConn, err = db.New(stream, *conf.DB, ro)
if err != nil {
return nil, stream.EventErrKv("connect.db", err, kvs)
}
stream.EventKv("connect.db", kvs)
} else {
stream.Event("connect.db.skip")
}
return NewConnections(stream, quietStream, streamDBDedup, dbConn, redisClient), nil
}
func NewDBFromConfig(conf cfg.Services, ro bool) (*Connections, error) {
// Always create a stream and log
stream := NewStream()
quietStream := NewQuietStream()
streamDBDedup := NewStreamDBDups()
// Create db and redis connections if configured
var (
dbConn *db.Conn
err error
)
if conf.DB != nil || conf.DB.Driver == db.DriverNone {
kvs := health.Kvs{}
// Create connection
dbConn, err = db.New(stream, *conf.DB, ro)
if err != nil {
return nil, stream.EventErrKv("connect.db", err, kvs)
}
} else {
return nil, fmt.Errorf("invalid database")
}
return NewConnections(stream, quietStream, streamDBDedup, dbConn, nil), nil
}
func NewConnections(s *health.Stream, quietStream *health.Stream, streamDBDedup *health.Stream, db *db.Conn, r *redis.Client) *Connections {
var c *cache.Cache
if r != nil {
c = cache.New(r)
}
return &Connections{
stream: s,
quietStream: quietStream,
streamDBDedup: streamDBDedup,
db: db,
redis: r,
cache: c,
}
}
func (c Connections) Stream() *health.Stream { return c.stream }
func (c Connections) QuietStream() *health.Stream { return c.quietStream }
func (c Connections) StreamDBDedup() *health.Stream { return c.streamDBDedup }
func (c Connections) DB() *db.Conn { return c.db }
func (c Connections) Redis() *redis.Client { return c.redis }
func (c Connections) Cache() *cache.Cache { return c.cache }
func (c Connections) Close() error {
errs := wrappers.Errs{}
errs.Add(c.db.Close(context.Background()))
if c.redis != nil {
errs.Add(c.redis.Close())
}
return errs.Err
}
func NewStream() *health.Stream {
s := health.NewStream()
s.AddSink(&health.WriterSink{Writer: os.Stdout})
return s
}
func NewStreamDBDups() *health.Stream {
s := health.NewStream()
s.AddSink(&WriterSinkExcludeDBDups{Writer: os.Stdout})
return s
}
func NewQuietStream() *health.Stream {
return health.NewStream()
}
func NewRedisConn(opts *redis.Options) (*redis.Client, error) {
client := redis.NewClient(opts)
ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()
_, err := client.Ping(ctx).Result()
if err != nil {
return nil, err
}
return client, nil
}
func timestamp() string {
return time.Now().UTC().Format(time.RFC3339Nano)
}
type WriterSinkExcludeDBDups struct {
io.Writer
}
func (s *WriterSinkExcludeDBDups) EmitEvent(job string, event string, kvs map[string]string) {
if event == "dbr.begin" || event == "dbr.commit" {
return
}
var b bytes.Buffer
b.WriteRune('[')
b.WriteString(timestamp())
b.WriteString("]: job:")
b.WriteString(job)
b.WriteString(" event:")
b.WriteString(event)
writeMapConsistently(&b, kvs)
b.WriteRune('\n')
_, _ = s.Writer.Write(b.Bytes())
}
func (s *WriterSinkExcludeDBDups) EmitEventErr(job string, event string, inputErr error, kvs map[string]string) {
errmsg := inputErr.Error()
if strings.HasPrefix(errmsg, "Error 1062: Duplicate entry") {
return
}
var b bytes.Buffer
b.WriteRune('[')
b.WriteString(timestamp())
b.WriteString("]: job:")
b.WriteString(job)
b.WriteString(" event:")
b.WriteString(event)
b.WriteString(" err:")
b.WriteString(inputErr.Error())
writeMapConsistently(&b, kvs)
b.WriteRune('\n')
_, _ = s.Writer.Write(b.Bytes())
}
func (s *WriterSinkExcludeDBDups) EmitTiming(job string, event string, nanos int64, kvs map[string]string) {
var b bytes.Buffer
b.WriteRune('[')
b.WriteString(timestamp())
b.WriteString("]: job:")
b.WriteString(job)
b.WriteString(" event:")
b.WriteString(event)
b.WriteString(" time:")
writeNanoseconds(&b, nanos)
writeMapConsistently(&b, kvs)
b.WriteRune('\n')
_, _ = s.Writer.Write(b.Bytes())
}
func (s *WriterSinkExcludeDBDups) EmitGauge(job string, event string, value float64, kvs map[string]string) {
var b bytes.Buffer
b.WriteRune('[')
b.WriteString(timestamp())
b.WriteString("]: job:")
b.WriteString(job)
b.WriteString(" event:")
b.WriteString(event)
b.WriteString(" gauge:")
fmt.Fprintf(&b, "%g", value)
writeMapConsistently(&b, kvs)
b.WriteRune('\n')
_, _ = s.Writer.Write(b.Bytes())
}
func (s *WriterSinkExcludeDBDups) EmitComplete(job string, status health.CompletionStatus, nanos int64, kvs map[string]string) {
var b bytes.Buffer
b.WriteRune('[')
b.WriteString(timestamp())
b.WriteString("]: job:")
b.WriteString(job)
b.WriteString(" status:")
b.WriteString(status.String())
b.WriteString(" time:")
writeNanoseconds(&b, nanos)
writeMapConsistently(&b, kvs)
b.WriteRune('\n')
_, _ = s.Writer.Write(b.Bytes())
}
func writeMapConsistently(b *bytes.Buffer, kvs map[string]string) {
if kvs == nil {
return
}
keys := make([]string, 0, len(kvs))
for k := range kvs {
keys = append(keys, k)
}
sort.Strings(keys)
keysLenMinusOne := len(keys) - 1
b.WriteString(" kvs:[")
for i, k := range keys {
b.WriteString(k)
b.WriteRune(':')
b.WriteString(kvs[k])
if i != keysLenMinusOne {
b.WriteRune(' ')
}
}
b.WriteRune(']')
}
func writeNanoseconds(b *bytes.Buffer, nanos int64) {
switch {
case nanos > 2000000:
fmt.Fprintf(b, "%d ms", nanos/1000000)
case nanos > 2000:
fmt.Fprintf(b, "%d μs", nanos/1000)
default:
fmt.Fprintf(b, "%d ns", nanos)
}
}