| 1 |
package ami |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"os" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type asyncMsg struct { |
| 12 |
id string |
| 13 |
msg *Message |
| 14 |
resp *Message |
| 15 |
err error |
| 16 |
result chan *asyncMsg |
| 17 |
cb func(v *asyncMsg) |
| 18 |
ctx context.Context |
| 19 |
done bool |
| 20 |
} |
| 21 |
|
| 22 |
func (async *asyncMsg) complete() { |
| 23 |
if async.done { |
| 24 |
return |
| 25 |
} |
| 26 |
if async.result != nil { |
| 27 |
async.result <- async |
| 28 |
close(async.result) |
| 29 |
} |
| 30 |
if async.cb != nil { |
| 31 |
go func() { |
| 32 |
async.cb(async) |
| 33 |
}() |
| 34 |
} |
| 35 |
async.done = true |
| 36 |
} |
| 37 |
|
| 38 |
func (c *Conn) read(ctx context.Context) (err error) { |
| 39 |
log := c.logger |
| 40 |
log.Debug("start read loop") |
| 41 |
defer func() { |
| 42 |
log.Debug("done read loop") |
| 43 |
}() |
| 44 |
for { |
| 45 |
msg := &Message{} |
| 46 |
|
| 47 |
if err = msg.Read(c.reader); err != nil && !errors.Is(err, os.ErrDeadlineExceeded) { |
| 48 |
return |
| 49 |
} |
| 50 |
|
| 51 |
if msg.Type != "" { |
| 52 |
c.recv <- msg |
| 53 |
} |
| 54 |
if ctx.Err() != nil { |
| 55 |
return ctx.Err() |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func (c *Conn) loop(ctx context.Context) (err error) { |
| 61 |
ids := map[string]*asyncMsg{} |
| 62 |
defer func() { |
| 63 |
for _, v := range ids { |
| 64 |
v.err = err |
| 65 |
v.complete() |
| 66 |
} |
| 67 |
}() |
| 68 |
|
| 69 |
log := c.logger |
| 70 |
cleanTicker := time.NewTicker(5 * time.Second) |
| 71 |
|
| 72 |
c.logger.Debug("start event loop") |
| 73 |
defer func() { |
| 74 |
log.Debug("done event loop") |
| 75 |
}() |
| 76 |
for { |
| 77 |
select { |
| 78 |
case <-ctx.Done(): |
| 79 |
return ctx.Err() |
| 80 |
case <-cleanTicker.C: |
| 81 |
c.cleanUnsub() |
| 82 |
case async := <-c.pending: |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
msg := async.msg |
| 90 |
|
| 91 |
|
| 92 |
err = msg.Write(c.conn) |
| 93 |
|
| 94 |
if err != nil { |
| 95 |
async.err = err |
| 96 |
async.complete() |
| 97 |
} else if async.id != "" { |
| 98 |
ids[async.id] = async |
| 99 |
} |
| 100 |
c.onSend(async) |
| 101 |
case msg := <-c.recv: |
| 102 |
|
| 103 |
if msg.Type == MessageTypeResponse { |
| 104 |
id := msg.AttrString(attrActionID) |
| 105 |
if id != "" { |
| 106 |
async := ids[id] |
| 107 |
if async == nil { |
| 108 |
log.Sugar().With("id", id).Warn("response untracked") |
| 109 |
} else { |
| 110 |
async.resp = msg |
| 111 |
async.complete() |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
c.onRecv(msg) |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
func (c *Conn) onSend(msg *asyncMsg) { |
| 121 |
subs := c.subs |
| 122 |
|
| 123 |
for _, v := range subs { |
| 124 |
if v.unsub || !v.onSent { |
| 125 |
continue |
| 126 |
} |
| 127 |
ctx := v.ctx |
| 128 |
if ctx == nil { |
| 129 |
ctx = c.ctx |
| 130 |
} |
| 131 |
v.unsub = !v.f(ctx, msg.msg) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func (c *Conn) onRecv(msg *Message) { |
| 136 |
subs := c.subs |
| 137 |
for _, v := range subs { |
| 138 |
if v.unsub || !v.onRecv { |
| 139 |
continue |
| 140 |
} |
| 141 |
ctx := v.ctx |
| 142 |
if ctx == nil { |
| 143 |
ctx = c.ctx |
| 144 |
} |
| 145 |
|
| 146 |
v.unsub = !v.f(ctx, msg) |
| 147 |
} |
| 148 |
} |