-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreader.go
50 lines (45 loc) · 951 Bytes
/
reader.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
package ibapi
import (
"bufio"
"context"
"sync"
)
// EReader starts the scan and decode goroutines
func EReader(ctx context.Context, scanner *bufio.Scanner, decoder *EDecoder, wg *sync.WaitGroup) {
msgChan := make(chan []byte, 300)
// Decode
wg.Add(1)
go func() {
log.Debug().Msg("decoder started")
defer log.Debug().Msg("decoder ended")
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case msg, ok := <-msgChan:
if !ok {
return
}
decoder.interpret(msg) // single worker and no go here!!
}
}
}()
// Scan
wg.Add(1)
go func() {
log.Debug().Msg("scanner started")
defer log.Debug().Msg("scanner ended")
defer wg.Done()
for scanner.Scan() {
msgBytes := make([]byte, len(scanner.Bytes()))
copy(msgBytes, scanner.Bytes())
msgChan <- msgBytes
if err := scanner.Err(); err != nil {
log.Error().Err(err).Msg("scanner error")
break
}
}
close(msgChan)
}()
}