-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathasync.go
117 lines (94 loc) · 2.15 KB
/
async.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package routeros
import (
"context"
"github.com/go-routeros/routeros/v3/proto"
)
type sentenceProcessor interface {
processSentence(sen *proto.Sentence) (bool, error)
}
type replyCloser interface {
close(err error)
}
// Async starts asynchronous mode and returns immediately.
func (c *Client) Async() <-chan error {
return c.AsyncContext(context.Background())
}
// AsyncContext starts asynchronous mode with context and returns immediately.
func (c *Client) AsyncContext(ctx context.Context) <-chan error {
c.mu.Lock()
defer c.mu.Unlock()
errC := make(chan error, 1)
if c.async {
errC <- errAlreadyAsync
close(errC)
return errC
}
c.async = true
c.tags = make(map[string]sentenceProcessor)
go c.asyncLoopChan(ctx, errC)
return errC
}
func (c *Client) asyncLoopChan(ctx context.Context, errC chan<- error) {
defer close(errC)
// If c.Close() has been called, c.closing will be true, and
// err will be “use of closed network connection”. Ignore that error.
if err := c.asyncLoop(ctx); err != nil {
c.mu.Lock()
closing := c.closing
c.mu.Unlock()
if !closing {
errC <- err
}
}
}
// asyncLoop - main goroutine for async mode. Read and process sentences, handle context done.
func (c *Client) asyncLoop(ctx context.Context) error {
go func() {
<-ctx.Done()
c.r.Cancel()
}()
for {
sen, err := c.r.ReadSentence()
if err != nil {
c.closeTags(err)
return err
}
c.mu.Lock()
r, ok := c.tags[sen.Tag]
c.mu.Unlock()
// cannot find tag for this sentence, ignore
if !ok {
continue
}
done, err := r.processSentence(sen)
if done || err != nil {
c.mu.Lock()
delete(c.tags, sen.Tag)
c.mu.Unlock()
closeReply(r, err)
}
}
}
func (c *Client) closeTags(err error) {
c.mu.Lock()
defer c.mu.Unlock()
// If c.Close() has been called, c.closing will be true, and
// err will be “use of closed network connection”. Ignore that error.
if c.closing {
for _, r := range c.tags {
closeReply(r, nil)
}
c.tags = nil
return
}
for _, r := range c.tags {
closeReply(r, err)
}
c.tags = nil
}
func closeReply(r sentenceProcessor, err error) {
rr, ok := r.(replyCloser)
if ok {
rr.close(err)
}
}