-
Notifications
You must be signed in to change notification settings - Fork 4
/
puller.go
65 lines (52 loc) · 1.34 KB
/
puller.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
package ws
import (
"context"
"sync/atomic"
"github.com/liumingmin/goutils/log"
)
type Puller interface {
PullSend()
}
func NewDefaultPuller(conn IConnection, pullChannelId int, firstPullFunc, pullFunc func(context.Context, IConnection)) Puller {
return &defaultPuller{
conn: conn,
pullChannelId: pullChannelId,
firstPullFunc: firstPullFunc,
pullFunc: pullFunc,
}
}
type defaultPuller struct {
conn IConnection
pullChannelId int
firstPullFunc func(context.Context, IConnection) // first connected exec
pullFunc func(context.Context, IConnection) // every times exec
isRunning int32
}
func (c *defaultPuller) PullSend() {
ctx := log.ContextWithTraceId()
ok := atomic.CompareAndSwapInt32(&c.isRunning, 0, 1)
if !ok {
log.Debug(ctx, "comet is running, pullChannelId: %v", c.pullChannelId)
return
}
defer atomic.StoreInt32(&c.isRunning, 0)
pullChannel, ok := c.conn.GetPullChannel(c.pullChannelId)
if !ok {
return
}
if c.firstPullFunc != nil {
c.firstPullFunc(ctx, c.conn)
}
for {
ctx = log.ContextWithTraceId()
if c.conn.IsStopped() {
log.Info(ctx, "agent is stopped: %v", c.conn.Id())
return
}
c.pullFunc(ctx, c.conn)
if _, ok := <-pullChannel; !ok {
log.Debug(ctx, "Connect stop pull channel. connId: %v, channelId: %v", c.conn.Id(), c.pullChannelId)
return
}
}
}