-
Notifications
You must be signed in to change notification settings - Fork 23
/
server.go
212 lines (172 loc) · 4.19 KB
/
server.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
package basalt
import (
"bufio"
"errors"
"io"
"log"
"net"
"os"
"github.com/smallnest/rpcx/protocol"
"github.com/smallnest/rpcx/server"
"github.com/soheilhy/cmux"
"github.com/tidwall/redcon"
)
// Errors for bitmaps
var (
ErrPersistFileNotFound = errors.New("persist file not found")
)
// Server is the bitmap server that supports multiple services.
type Server struct {
addr string
bitmaps *Bitmaps
ln net.Listener
confChangeCallback ConfChange
rpcxOptions []ConfigRpcxOption
persistFile string
}
// NewServer returns a server.
func NewServer(addr string, bitmaps *Bitmaps, rpcxOptions []ConfigRpcxOption, persistFile string) *Server {
return &Server{
addr: addr,
bitmaps: bitmaps,
rpcxOptions: rpcxOptions,
persistFile: persistFile,
}
}
// SetConfChangeCallback must invoke before Serve.
func (s *Server) SetConfChangeCallback(confChangeCallback ConfChange) {
s.confChangeCallback = confChangeCallback
}
// Serve serves basalt services.
func (s *Server) Serve() error {
ln, err := net.Listen("tcp", s.addr)
if err != nil {
return err
}
return s.configListener(ln)
}
// Close closes this server.
func (s *Server) Close() error {
if s.ln == nil {
return nil
}
return s.ln.Close()
}
func (s *Server) configListener(ln net.Listener) error {
m := cmux.New(ln)
// rpcx
rpcxLn := m.Match(rpcxPrefixByteMatcher())
// admin http
httpLn := m.Match(cmux.HTTP1Fast())
// redis
redisLn := m.Match(cmux.Any())
go s.startRpcxService(rpcxLn)
go s.startHTTPService(httpLn)
go s.startRedisService(redisLn)
return m.Serve()
}
func (s *Server) startRpcxService(ln net.Listener) {
srv := server.NewServer()
for _, opt := range s.rpcxOptions {
opt(s, srv)
}
srv.RegisterName("Bitmap", &RpcxBitmapService{s: s, confChangeCallback: s.confChangeCallback}, "")
if err := srv.ServeListener("tcp", ln); err != nil {
log.Fatalf("failed to start rpcx services: %v", err)
}
}
// if not config adminAddr, we don't start admin service.
// It is useful for security purpose.
func (s *Server) startHTTPService(ln net.Listener) {
hs := &HTTPService{
s: s,
confChangeCallback: s.confChangeCallback,
}
if err := hs.Serve(ln); err != nil {
log.Fatalf("failed to start http service: %v", err)
}
}
func (s *Server) startRedisService(ln net.Listener) {
redisService := &RedisService{
s: s,
confChangeCallback: s.confChangeCallback,
}
if err := redcon.Serve(ln, redisService.redisHandler, redisService.redisAccept, redisService.redisClose); err != nil {
log.Fatalf("failed to start redis services: %v", err)
}
}
func rpcxPrefixByteMatcher() cmux.Matcher {
magic := protocol.MagicNumber()
return func(r io.Reader) bool {
buf := make([]byte, 1)
n, _ := r.Read(buf)
return n == 1 && buf[0] == magic
}
}
// Save saves the data into file.
func (s *Server) Save() error {
if s.persistFile == "" {
return ErrPersistFileNotFound
}
file, err := os.Create(s.persistFile)
if err != nil {
return err
}
w := bufio.NewWriter(file)
err = s.bitmaps.Save(w)
if err != nil {
file.Close()
return err
}
err = w.Flush()
if err != nil {
return err
}
return file.Close()
}
// Restore retores the data from file.
func (s *Server) Restore() error {
if s.persistFile == "" {
return ErrPersistFileNotFound
}
file, err := os.Open(s.persistFile)
if err != nil {
return err
}
r := bufio.NewReader(file)
err = s.bitmaps.Read(r)
if err != nil {
return err
}
return file.Close()
}
func (s *Server) add(name, value string, callback bool) error {
v, err := str2uint32(value)
if err != nil {
return err
}
s.bitmaps.Add(name, v, callback)
return nil
}
func (s *Server) addMany(name, values string, callback bool) error {
vs, err := str2uint32s(values)
if err != nil {
return err
}
s.bitmaps.AddMany(name, vs, callback)
return nil
}
func (s *Server) remove(name, value string, callback bool) error {
v, err := str2uint32(value)
if err != nil {
return err
}
s.bitmaps.Remove(name, v, callback)
return err
}
func (s *Server) drop(name string, callback bool) {
s.bitmaps.RemoveBitmap(name, callback)
}
func (s *Server) clear(name string, callback bool) {
s.bitmaps.ClearBitmap(name, callback)
}