/
ack.go
72 lines (55 loc) · 1.46 KB
/
ack.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
package socket
import (
"context"
"errors"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/grpool"
"github.com/iimeta/iim-server/utility/logger"
"github.com/iimeta/iim-server/utility/timewheel"
"time"
)
var ack *AckBuffer
// AckBuffer Ack 确认缓冲区
type AckBuffer struct {
timeWheel *timewheel.SimpleTimeWheel[*AckBufferContent]
}
type AckBufferContent struct {
cid int64
uid int64
channel string
response *ClientResponse
}
func init() {
ack = &AckBuffer{}
ack.timeWheel = timewheel.NewSimpleTimeWheel[*AckBufferContent](1*time.Second, 30, ack.handle)
}
func (a *AckBuffer) Start(ctx context.Context) error {
_ = grpool.AddWithRecover(gctx.New(), func(ctx context.Context) {
a.timeWheel.Start()
}, nil)
<-ctx.Done()
a.timeWheel.Stop()
return errors.New("ack service stopped")
}
func (a *AckBuffer) insert(ackKey string, value *AckBufferContent) {
a.timeWheel.Add(ackKey, value, time.Duration(5)*time.Second)
}
func (a *AckBuffer) delete(ackKey string) {
a.timeWheel.Remove(ackKey)
}
func (a *AckBuffer) handle(_ *timewheel.SimpleTimeWheel[*AckBufferContent], _ string, bufferContent *AckBufferContent) {
ch, ok := Session.Channel(bufferContent.channel)
if !ok {
return
}
client, ok := ch.Client(bufferContent.cid)
if !ok {
return
}
if client.Closed() || int64(client.uid) != bufferContent.uid {
return
}
if err := client.Write(bufferContent.response); err != nil {
logger.Error(gctx.New(), "ack err:", err)
}
}