/
server.go
49 lines (42 loc) · 1005 Bytes
/
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
package main
import (
"imgo/libs/hash/cityhash"
"time"
log "github.com/thinkboy/log4go"
)
var (
maxInt = 1<<31 - 1
emptyJSONBody = []byte("{}")
)
type ServerOptions struct {
CliProto int
SvrProto int
HandshakeTimeout time.Duration
TCPKeepalive bool
TCPRcvbuf int
TCPSndbuf int
}
type Server struct {
Buckets []*Bucket // subkey bucket
bucketIdx uint32
round *Round // accept round store
operator Operator
Options ServerOptions
}
// NewServer returns a new Server.
func NewServer(b []*Bucket, r *Round, o Operator, options ServerOptions) *Server {
s := new(Server)
s.Buckets = b
s.bucketIdx = uint32(len(b))
s.round = r
s.operator = o
s.Options = options
return s
}
func (server *Server) Bucket(subKey string) *Bucket {
idx := cityhash.CityHash32([]byte(subKey), uint32(len(subKey))) % server.bucketIdx
if Debug {
log.Debug("\"%s\" hit channel bucket index: %d use cityhash", subKey, idx)
}
return server.Buckets[idx]
}