-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
100 lines (86 loc) · 1.72 KB
/
api.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
package golwpush
import (
"github.com/NullpointerW/golwpush/errs"
"github.com/NullpointerW/golwpush/logger"
"github.com/NullpointerW/golwpush/pkg"
"github.com/NullpointerW/golwpush/protocol"
"time"
)
type BizTyp int
const (
Info BizTyp = iota
Kick
)
type BizReq struct {
Res chan any
Uid uint64
Typ BizTyp
}
type defaultPush struct {
banned map[uint64]struct{}
}
func (p defaultPush) Push(id uint64, msg string) (err error) {
PushCh <- Content{Id: id, Msg: msg}
return
}
func (p defaultPush) Broadcast(broadMsg string) (err error) {
if len(broadMsg) > (protocol.MaxLen - pkg.MsgModeExtraLen) {
err = errs.SendDataOutOfSize
return
}
select {
case Broadcast <- broadMsg: //限流
default:
// TODO log
logger.Error("request limiting")
}
return
}
func (p defaultPush) MultiPush(cts *Contents) (err error, success uint64) {
MultiPushCh <- cts
t := time.NewTicker(time.Second * 10)
defer t.Stop()
select {
case success = <-cts.Res:
case <-t.C:
err = errs.ServiceCallTimedOut
}
return
}
func (p defaultPush) Count() uint64 {
return LoadConnNum()
}
func (p defaultPush) Info(biz BizReq) (*ConnInfo, error) {
t := time.NewTicker(time.Second * 10)
defer t.Stop()
BizCh <- biz
select {
case r := <-biz.Res:
if info, exist := r.(ConnInfo); exist {
return &info, nil
}
return nil, nil //offline
case <-t.C:
err := errs.ServiceCallTimedOut
return nil, err
}
}
var (
Default Adapter = defaultPush{banned: nil}
)
type SinglePush interface {
Push(uint64, string) error
}
type AllPush interface {
Broadcast(string) error
MultiPush(cts *Contents) (error, uint64)
}
type ConnManger interface {
Count() uint64
Info(biz BizReq) (*ConnInfo, error)
}
type Adapter interface {
SinglePush
AllPush
ConnManger
}