forked from justloop/navigator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
72 lines (57 loc) · 2.14 KB
/
config.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
package navigator
import (
"time"
"github.com/justloop/navigator/discovery"
"github.com/justloop/navigator/partition"
"github.com/justloop/navigator/seeds"
"github.com/justloop/navigator/utils"
)
const (
// defaultReplication is the default replication points
defaultReplication = 1
)
// Config is the configuration related to Navigator
type Config struct {
// ReplicaPoints is the number of replications, optional, default 1
ReplicaPoints int
// DConfig is the configuration for discovery client, optional, default use default discovery config
DConfig *discovery.Config
// Strategy is the partition strategy, optional, default IdentityStrategy
Strategy partition.Strategy
// SeedsService is a implementation of seedsService that used to auto get a list of seeds to join
SeedsService seeds.Seeds
// SeedsRefreshInterval is the frequency to refresh current node to seeds service
SeedsRefreshInterval time.Duration
// KeyHashFunc is the customized key hash function, if return 0, it will use default hash function as well
KeyHashFunc func(key string) uint32
// OnMemberEvent is the callback handler to be invoked when repartition happens,
// and the node will need to reload data, optional, default, no rehashHandler
OnMemberEvent func(event discovery.MemberEvent) error
// OnShutdown is the callback when discovery is shutdown or reaped by the cluster
OnShutdown func()
}
// setDefaultConfig sets the default context
func setDefaultConfig(config *Config) *Config {
// Set default discovery config
if config.DConfig == nil {
config.DConfig = discovery.DefaultConfig()
}
// Set default partition strategy
if config.Strategy == nil {
config.Strategy = partition.NewIndentityStrategy()
}
if config.SeedsRefreshInterval == 0 {
config.SeedsRefreshInterval = 1 * time.Minute
}
// Set default replication
config.ReplicaPoints = utils.SelectInt(config.ReplicaPoints, defaultReplication)
// set default rehashHandler
if config.OnMemberEvent == nil {
config.OnMemberEvent = func(event discovery.MemberEvent) error { return nil }
}
// set default shutdown
if config.OnShutdown == nil {
config.OnShutdown = func() {}
}
return config
}