-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
48 lines (38 loc) · 855 Bytes
/
Copy pathmain.go
File metadata and controls
48 lines (38 loc) · 855 Bytes
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
// Basic example: single producer → single consumer
package main
import (
"context"
"fmt"
"time"
"github.com/gocronx/seqflow"
)
type Order struct {
ID int64
Price float64
}
// Order processor
type orderProcessor struct{}
func (p *orderProcessor) Handle(lower, upper int64) {
fmt.Printf("processed order sequences %d..%d\n", lower, upper)
}
func main() {
d, err := seqflow.New[Order](
seqflow.WithCapacity(1024),
seqflow.WithHandler("processor", &orderProcessor{}),
)
if err != nil {
panic(err)
}
go d.Listen()
// Publish 5 orders
rb := d.RingBuffer()
for i := int64(1); i <= 5; i++ {
upper, _ := d.Reserve(1)
rb.Set(upper, Order{ID: i, Price: float64(i) * 99.9})
d.Commit(upper, upper)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
d.Drain(ctx)
fmt.Println("done")
}