-
Notifications
You must be signed in to change notification settings - Fork 2
/
im.go
74 lines (60 loc) · 1.63 KB
/
im.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
package im
import (
"context"
im "github.com/mongofs/api/im/v1"
grpc2 "github.com/mongofs/api/im/v1"
"github.com/mongofs/im/bucket"
"go.uber.org/atomic"
"google.golang.org/grpc"
"net/http"
)
type ImSrever struct {
http *http.ServeMux
rpc *grpc.Server
bs []bucket.Bucketer
ps atomic.Int64
buffer chan *im.BroadcastReq // 全局广播队列
cancel func()
opt *Option
}
func New(opts *Option) *ImSrever {
b := &ImSrever{
ps: atomic.Int64{},
opt: opts,
}
b.buffer = make(chan *grpc2.BroadcastReq,opts.BroadCastBuffer)
b.ps.Store(0)
b.prepareBucketer()
b.prepareGrpcServer()
b.prepareHttpServer()
return b
}
func (h *ImSrever) prepareBucketer() {
h.bs = make([]bucket.Bucketer, h.opt.ServerBucketNumber)
_, cancel := context.WithCancel(context.Background())
h.cancel = cancel
BucketOptionSet := &bucket.Option{
HeartBeatInterval: int64(h.opt.ClientHeartBeatInterval),
ReaderBufferSize: h.opt.ClientReaderBufferSize,
WriteBufferSize: h.opt.ClientWriteBufferSize,
ClientBufferSize: h.opt.ClientBufferSize,
MessageType: h.opt.ClientMessageType,
Protocol: h.opt.ClientProtocol,
BucketSize: h.opt.BucketSize,
}
for i:= 0 ;i<h.opt.ServerBucketNumber;i ++ {
h.bs[i] = bucket.New(h.opt.ServerLogger,BucketOptionSet)
}
}
func (b *ImSrever) prepareGrpcServer() {
b.rpc = grpc.NewServer()
grpc2.RegisterBasicServer(b.rpc, b)
}
func (b *ImSrever) prepareHttpServer() {
b.http = http.NewServeMux()
b.initRouter()
}
func (s *ImSrever) bucket(token string) bucket.Bucketer {
idx := Index(token,uint32(s.opt.ServerBucketNumber))
return s.bs[idx]
}