-
Notifications
You must be signed in to change notification settings - Fork 0
/
center.go
179 lines (146 loc) · 3.45 KB
/
center.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
package castcenter
import (
"fmt"
"net"
"sync/atomic"
"time"
cache "github.com/patrickmn/go-cache"
"github.com/sirupsen/logrus"
)
const (
defaultServicePort = 52593
defaultMulticastAddr = "239.95.3.9:9539"
defaultChanSize = 2048
defaultCacheTimeout = time.Second
defaultCacheCleanupInterval = time.Second * 10
defaultClusterName = "abc950309/castcenter"
defaultClusterHeartbeat = time.Second * 10
defaultClusterTimeout = time.Second * 30
)
// CastCenter .
type CastCenter struct {
redis Redis
servicePort int
multicastAddr string
chanSize int
cacheTimeout time.Duration
cacheCleanupInterval time.Duration
clusterName string
clusterHeartbeat time.Duration
clusterTimeout time.Duration
serviceAddr string
clusterStatus atomic.Value // *ClusterStatus
multicastConn *net.UDPConn
cache *cache.Cache
}
// Option .
type Option func(*CastCenter) *CastCenter
// New .
func New(options ...Option) *CastCenter {
c := &CastCenter{}
for _, option := range options {
c = option(c)
}
if c.redis == nil {
logrus.Fatal(fmt.Errorf("castcenter new: expected option SetRedis"))
}
if c.servicePort == 0 {
c.servicePort = defaultServicePort
}
if c.multicastAddr == "" {
c.multicastAddr = defaultMulticastAddr
}
if c.clusterName == "" {
c.clusterName = defaultClusterName
}
if c.chanSize == 0 {
c.chanSize = defaultChanSize
}
if c.cacheTimeout == 0 {
c.cacheTimeout = defaultCacheTimeout
}
if c.cacheCleanupInterval == 0 {
c.cacheCleanupInterval = defaultCacheCleanupInterval
}
if c.clusterHeartbeat == 0 {
c.clusterHeartbeat = defaultClusterHeartbeat
}
if c.clusterTimeout == 0 {
c.clusterTimeout = defaultClusterTimeout
}
mcAddr, err := net.ResolveUDPAddr("udp", c.multicastAddr)
if err != nil {
logrus.Fatal(err)
}
mcConn, err := net.DialUDP("udp", nil, mcAddr)
if err != nil {
logrus.Fatal(err)
}
c.multicastConn = mcConn
c.serviceAddr = fmt.Sprintf("%s:%d", netinfo.IP, c.servicePort)
c.cache = cache.New(c.cacheTimeout, c.cacheCleanupInterval)
return c
}
// SetRedis .
func SetRedis(redis Redis) Option {
return func(c *CastCenter) *CastCenter {
c.redis = redis
return c
}
}
// SetClusterName .
func SetClusterName(name string) Option {
return func(c *CastCenter) *CastCenter {
c.clusterName = name
return c
}
}
// SetServicePort .
func SetServicePort(port int) Option {
return func(c *CastCenter) *CastCenter {
c.servicePort = port
return c
}
}
// SetMulticastAddr .
func SetMulticastAddr(addr string) Option {
return func(c *CastCenter) *CastCenter {
c.multicastAddr = addr
return c
}
}
// SetChanSize .
func SetChanSize(size int) Option {
return func(c *CastCenter) *CastCenter {
c.chanSize = size
return c
}
}
// SetCacheTimeout .
func SetCacheTimeout(timeout time.Duration) Option {
return func(c *CastCenter) *CastCenter {
c.cacheTimeout = timeout
return c
}
}
// SetCacheCleanupInterval .
func SetCacheCleanupInterval(interval time.Duration) Option {
return func(c *CastCenter) *CastCenter {
c.cacheCleanupInterval = interval
return c
}
}
// SetClusterHeartbeat .
func SetClusterHeartbeat(interval time.Duration) Option {
return func(c *CastCenter) *CastCenter {
c.clusterHeartbeat = interval
return c
}
}
// SetClusterTimeout .
func SetClusterTimeout(timeout time.Duration) Option {
return func(c *CastCenter) *CastCenter {
c.clusterTimeout = timeout
return c
}
}