This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
80 lines (66 loc) · 2.21 KB
/
client.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
package cluster
import (
"context"
"github.com/go-redis/redis/v8"
zLogger "github.com/mrz1836/go-logger"
)
type (
// Client is the client (configuration)
Client struct {
pubSubService
options *clientOptions
}
// clientOptions holds all the configuration for the client
clientOptions struct {
coordinator Coordinator // which coordinator to use, either 'memory' or 'redis'
debug bool // For extra logs and additional debug information
logger zLogger.GormLoggerInterface // Internal logger interface
newRelicEnabled bool // Whether to use New Relic
prefix string // the cluster key prefix to use before all keys
redisOptions *redis.Options
}
)
// NewClient create new cluster client
func NewClient(ctx context.Context, opts ...ClientOps) (*Client, error) {
// Create a new client with defaults
client := &Client{options: defaultClientOptions()}
// Overwrite defaults with any set by user
for _, opt := range opts {
opt(client.options)
}
// Use NewRelic if it's enabled (use existing txn if found on ctx)
ctx = client.options.getTxnCtx(ctx)
// Set logger if not set
if client.options.logger == nil {
client.options.logger = zLogger.NewGormLogger(client.IsDebug(), 4)
}
if client.options.coordinator == CoordinatorRedis {
pubSubClient, err := NewRedisPubSub(ctx, client.options.redisOptions)
if err != nil {
return nil, err
}
pubSubClient.debug = client.IsDebug()
pubSubClient.logger = client.options.logger
pubSubClient.prefix = client.GetClusterPrefix()
client.pubSubService = pubSubClient
} else {
pubSubClient, err := NewMemoryPubSub(ctx)
if err != nil {
return nil, err
}
pubSubClient.debug = client.IsDebug()
pubSubClient.logger = client.options.logger
pubSubClient.prefix = client.GetClusterPrefix()
client.pubSubService = pubSubClient
}
// Return the client
return client, nil
}
// IsDebug returns whether debugging is on or off
func (c *Client) IsDebug() bool {
return c.options.debug
}
// GetClusterPrefix returns the cluster key prefix that can be used in things like Redis
func (c *Client) GetClusterPrefix() string {
return c.options.prefix
}