-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectors.go
More file actions
73 lines (61 loc) · 1.88 KB
/
connectors.go
File metadata and controls
73 lines (61 loc) · 1.88 KB
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
package service
import (
"context"
"fmt"
"time"
"github.com/go-redis/redis/v8"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/domain"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// NewMongo - initialize mongo-driver client. Also pinging mongo.
func NewMongo(ctx context.Context, host string) (*mongo.Client, error) {
if host == "" {
host = "mongodb://localhost:27017"
}
client, err := mongo.Connect(ctx, options.Client().ApplyURI(host))
if err != nil {
return nil, fmt.Errorf("connect: %w", err)
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := client.Ping(ctx, nil); err != nil {
return nil, fmt.Errorf("ping: %w", err)
}
return client, nil
}
func NewRedis(ctx context.Context, host []string, password string) (*redis.Ring, error) {
addr := make(map[string]string)
for i, h := range host {
addr[fmt.Sprintf("server_%d", i+1)] = h
}
conn := redis.NewRing(&redis.RingOptions{
NewClient: func(name string, opt *redis.Options) *redis.Client {
opt.Password = password
return redis.NewClient(opt)
},
Addrs: addr,
})
if err := conn.Ping(ctx).Err(); err != nil {
return nil, fmt.Errorf("redis ping error: %w", err)
}
return conn, nil
}
func NewInflux(ctx context.Context, host, token string, opts *influxdb2.Options) (influxdb2.Client, error) {
client := influxdb2.NewClientWithOptions(host, token, opts)
if _, err := client.Ping(ctx); err != nil {
return nil, fmt.Errorf("ping influx: %w", err)
}
ready, err := client.Ready(ctx)
if err != nil {
return nil, fmt.Errorf("influx client: %w", err)
}
if ready.Status == nil {
return nil, fmt.Errorf("influx client ready status is nil: %w", err)
}
if *ready.Status != domain.ReadyStatusReady {
return nil, fmt.Errorf("influx client is not ready: %w", err)
}
return client, nil
}