-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
118 lines (96 loc) · 1.86 KB
/
queue.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
118
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"sync"
"github.com/sasha-s/go-deadlock"
)
type Queue struct {
dir string
cond *sync.Cond
maxStep int64
}
const bucketName = "steps"
func newQueue(tmpDir string) (*Queue, error) {
dir, err := os.MkdirTemp(tmpDir, "queue")
if err != nil {
return nil, err
}
lock := new(deadlock.Mutex)
cond := sync.NewCond(lock)
return &Queue{
dir: dir,
cond: cond,
}, nil
}
func (q *Queue) path(step int64) string {
return filepath.Join(q.dir, fmt.Sprintf("%d.html", step))
}
func (q *Queue) putStep(step int64, event []byte) error {
q.cond.L.Lock()
if step > q.maxStep {
q.maxStep = step
}
q.cond.L.Unlock()
// dumb cleanup
go func() {
oldstep := step - 1000
p := q.path(oldstep)
os.Remove(p)
}()
return os.WriteFile(q.path(step), event, 0644)
}
func (q *Queue) getStep(step int64) ([]byte, error) {
return os.ReadFile(q.path(step))
}
func (q *Queue) PushStep(step int64, event []byte) error {
if err := q.putStep(step, event); err != nil {
return err
}
q.cond.Broadcast()
return nil
}
func (q *Queue) Steps(ctx context.Context, currentStep int64) <-chan []byte {
ch := make(chan []byte)
go func() {
defer close(ch)
nextStep := currentStep + 1
for {
for {
q.cond.L.Lock()
maxStep := q.maxStep
q.cond.L.Unlock()
if maxStep < nextStep {
break
}
event, err := q.getStep(nextStep)
if err != nil {
log.Printf("error in getStep: %s (subscriber exited)\n", err.Error())
return
}
nextStep++
select {
case <-ctx.Done():
return
case ch <- event:
}
}
q.cond.L.Lock()
for q.maxStep < nextStep {
q.cond.Wait()
if ctx.Err() != nil {
q.cond.L.Unlock()
return
}
}
q.cond.L.Unlock()
}
}()
return ch
}
func (q *Queue) DeleteAllSteps() error {
return os.RemoveAll(q.dir)
}