-
Notifications
You must be signed in to change notification settings - Fork 0
/
replayer.go
50 lines (36 loc) · 850 Bytes
/
replayer.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 pgvcr
import (
"sync"
)
type constError string
func (e constError) Error() string {
return string(e)
}
const errNoMoreMessages constError = "no more messages"
type replayer struct {
lock sync.Mutex
messages []messageWithID
cursor int
greetingsLock sync.Mutex
greetings [][]messageWithID
greetingsCursor int
}
func (r *replayer) ConsumeGreetings(fn func(msgs []messageWithID) error) error {
r.greetingsLock.Lock()
defer r.greetingsLock.Unlock()
msgs := r.greetings[r.greetingsCursor]
if r.greetingsCursor+1 < len(r.messages) {
r.greetingsCursor++
}
return fn(msgs)
}
func (r *replayer) ConsumeNext(fn func(m messageWithID) error) error {
r.lock.Lock()
defer r.lock.Unlock()
if r.cursor >= len(r.messages) {
return errNoMoreMessages
}
msg := r.messages[r.cursor]
r.cursor++
return fn(msg)
}