-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
146 lines (116 loc) · 2.67 KB
/
service.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package events
import (
"context"
"errors"
"sync"
"time"
"github.com/oklog/ulid/v2"
"go.uber.org/zap"
)
var (
ErrEmptyPayload = errors.New("empty payload")
ErrIteratorNotFound = errors.New("iterator not found")
ErrInvalidType = errors.New("invalid type")
)
type Service interface {
Up()
Down()
Store(topic string, payload Payload, ids ...ulid.ULID) error
NewIterator(topic string, since time.Time) (string, error)
Iterator(id string) (Iterator, error)
// Iterator
FetchFromIterator(batch int, id string) ([]*Event, error)
CloseIterator(id string) error
}
type ServiceMiddleware func(Service) Service
type service struct {
log *zap.Logger
events Repository
iterators sync.Map
ctx context.Context
cancel context.CancelFunc
}
func NewService(events Repository) Service {
return &service{
events: events,
}
}
func (svc *service) Up() {
svc.log = zap.L().With(
zap.String("service", "events"),
)
ctx, cancel := context.WithCancel(context.Background())
svc.ctx = ctx
svc.cancel = cancel
svc.log.Info("done", zap.String("action", "up"))
}
func (svc *service) Down() {
svc.cancel()
svc.log.Info("done", zap.String("action", "down"))
}
func (svc *service) Store(topic string, payload Payload, ids ...ulid.ULID) error {
e := NewEvent(topic, payload, ids...)
err := svc.events.Store(e)
if err != nil {
return err
}
return nil
}
func (svc *service) NewIterator(topic string, since time.Time) (string, error) {
// TODO: options
it, err := svc.events.Iterator(svc.ctx, since)
if err != nil {
return "", err
}
go svc.doneHandler(it)
svc.iterators.Store(it.ID(), it)
return it.ID(), nil
}
func (svc *service) Iterator(id string) (Iterator, error) {
val, ok := svc.iterators.Load(id)
if !ok {
return nil, errors.New("iterator not found")
}
it, ok := val.(Iterator)
if !ok {
return nil, errors.New("invalid type")
}
return it, nil
}
func (svc *service) doneHandler(it Iterator) {
log := svc.log.With(
zap.String("iterator", it.ID()),
zap.String("handler", "iterator_done"),
)
<-it.Done()
if err := it.Err(); err != nil {
if !errors.Is(err, context.Canceled) {
log.Error(err.Error())
}
}
svc.iterators.Delete(it.ID())
log.Info("done")
}
func (svc *service) FetchFromIterator(batch int, id string) ([]*Event, error) {
val, ok := svc.iterators.Load(id)
if !ok {
return nil, ErrIteratorNotFound
}
it, ok := val.(Iterator)
if !ok {
return nil, ErrInvalidType
}
return it.Fetch(batch)
}
func (svc *service) CloseIterator(id string) error {
val, ok := svc.iterators.LoadAndDelete(id)
if !ok {
return ErrIteratorNotFound
}
it, ok := val.(Iterator)
if !ok {
return ErrInvalidType
}
it.Close(nil)
return nil
}