forked from dunglas/mercure
-
Notifications
You must be signed in to change notification settings - Fork 3
/
postgres_transport.go
313 lines (267 loc) · 7.1 KB
/
postgres_transport.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package mercure
import (
"database/sql"
"net/url"
"time"
"sync"
"fmt"
"encoding/json"
"go.uber.org/zap"
_ "github.com/lib/pq"
"errors"
)
func init() { //nolint:gochecknoinits
RegisterTransportFactory("postgres", NewPostgresTransport)
RegisterTransportFactory("postgresql", NewPostgresTransport)
}
// PostgresTransport implements the TransportInterface for PostgreSQL databases
type PostgresTransport struct {
sync.RWMutex
subscribers *SubscriberList
logger Logger
db *sql.DB
closed chan struct{}
closedOnce sync.Once
eventTTL time.Duration
cleanupInterval time.Duration
}
func NewPostgresTransport(iu *url.URL, l Logger, tss *TopicSelectorStore) (Transport, error) {
var (
eventTTL time.Duration = 24 * time.Hour
cleanupInterval time.Duration
)
u, err := url.Parse(iu.String())
if err != nil {
return nil, fmt.Errorf("can't clone URL: %w", err)
}
q := u.Query()
if s := q.Get("event_ttl"); s != "" {
d, err := time.ParseDuration(s)
if err == nil {
if d < 0 {
eventTTL = - d
} else {
eventTTL = d
}
} else if c := l.Check(zap.ErrorLevel, "unparsable postgres event TTL"); c != nil {
c.Write(zap.String("event-ttl", s))
}
q.Del("event_ttl")
}
if s := q.Get("cleanup_interval"); s != "" {
d, err := time.ParseDuration(s)
if err == nil {
cleanupInterval = d
} else if c := l.Check(zap.ErrorLevel, "unparsable postgres cleanup interval"); c != nil {
c.Write(zap.String("cleanup-interval", s))
}
q.Del("cleanup_interval")
}
u.RawQuery = q.Encode()
db, err := sql.Open("postgres", u.String())
if err != nil {
return nil, fmt.Errorf("can't parse URL: %w", err)
}
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("can't connect to the database: %w", err)
}
transport := &PostgresTransport{
subscribers: NewSubscriberList(1e5),
logger: l,
db: db,
closed: make(chan struct{}),
eventTTL: eventTTL,
cleanupInterval: cleanupInterval,
}
if transport.cleanupInterval > 0 {
go transport.cleanup()
}
return transport, nil
}
func (t *PostgresTransport) storeUpdate(update *Update) error {
updateJSON, err := json.Marshal(*update)
if err != nil {
return fmt.Errorf("error marshalling update: %w", err)
}
if _, err := t.db.Exec("INSERT INTO events (id,message) VALUES($1,$2)", update.ID, updateJSON); err != nil {
return fmt.Errorf("error inserting update: %w", err)
}
return nil
}
func (t *PostgresTransport) Dispatch(update *Update) error {
select {
case <-t.closed:
return ErrClosedTransport
default:
}
t.Lock()
defer t.Unlock()
AssignUUID(update)
if err := t.storeUpdate(update); err != nil {
return err
}
for _, s := range t.subscribers.MatchAny(update) {
s.Dispatch(update, false)
}
return nil
}
func (t *PostgresTransport) dispatchHistory(s *Subscriber) error {
var ts time.Time
err := t.db.QueryRow(`SELECT ts from events where id = $1`, s.RequestLastEventID).Scan(&ts)
var rows *sql.Rows
if err == nil {
rows, err = t.db.Query(`SELECT message FROM events WHERE ts > $1 ORDER BY ts`,
ts.Format(`2006-01-02T15:04:05.999999`))
} else if errors.Is(err, sql.ErrNoRows) {
rows, err = t.db.Query(`SELECT message FROM events ORDER BY ts`)
} else {
return fmt.Errorf("Error getting timestamp: %w", err)
}
responseLastEventID := EarliestLastEventID
if err == nil {
for rows.Next() {
var (
message string
update *Update
)
if err := rows.Scan(&message); err != nil {
if c := t.logger.Check(zap.ErrorLevel, "Scan error"); c != nil {
c.Write(zap.Error(err))
}
break
}
if err := json.Unmarshal([]byte(message), &update); err != nil {
if c := t.logger.Check(zap.ErrorLevel, "Bad JSON"); c != nil {
c.Write(zap.String("message", message),
zap.Error(err))
}
break
}
if s.Match(update) && s.Dispatch(update, true) {
responseLastEventID = update.ID
}
}
} else if !errors.Is(err, sql.ErrNoRows) {
return err
}
s.HistoryDispatched(responseLastEventID)
return nil
}
// AddSubscriber adds a new subscriber to the transport.
func (t *PostgresTransport) AddSubscriber(s *Subscriber) error {
select {
case <-t.closed:
return ErrClosedTransport
default:
}
if dbg := t.logger.Check(zap.DebugLevel, "AddSubscriber"); dbg != nil {
dbg.Write(zap.String("ID", s.ID),
zap.String("s.RequestLastEventID", s.RequestLastEventID))
}
t.Lock()
t.subscribers.Add(s)
t.Unlock()
if s.RequestLastEventID != "" {
if err := t.dispatchHistory(s); err != nil {
if c := t.logger.Check(zap.ErrorLevel, "Dispatch error"); c != nil {
c.Write(zap.Error(err))
}
return err
}
}
s.Ready()
return nil
}
// RemoveSubscriber removes a new subscriber from the transport.
func (t *PostgresTransport) RemoveSubscriber(s *Subscriber) error {
select {
case <-t.closed:
return ErrClosedTransport
default:
}
t.Lock()
defer t.Unlock()
t.subscribers.Remove(s)
return nil
}
// Close closes the Transport.
func (t *PostgresTransport) Close() (err error) {
t.closedOnce.Do(func() {
close(t.closed)
t.Lock()
defer t.Unlock()
t.subscribers.Walk(0, func(s *Subscriber) bool {
s.Disconnect()
return true
})
err = t.db.Close()
})
return
}
func (t *PostgresTransport) lastEventID() (result string, err error) {
err = t.db.QueryRow(`SELECT e1.id FROM events e1 INNER JOIN (SELECT MAX(ts) ts FROM events) e2 ON e1.ts = e2.ts`).Scan(&result)
if err != nil && errors.Is(err, sql.ErrNoRows) {
result = EarliestLastEventID
}
return
}
// GetSubscribers gets the list of active subscribers.
func (t *PostgresTransport) GetSubscribers() (string, []*Subscriber, error) {
t.RLock()
defer t.RUnlock()
var subscribers []*Subscriber
t.subscribers.Walk(0, func(s *Subscriber) bool {
subscribers = append(subscribers, s)
return true
})
lastEventID, err := t.lastEventID()
return lastEventID, subscribers, err
}
func (t *PostgresTransport) trim() error {
select {
case <-t.closed:
return ErrClosedTransport
default:
}
t.Lock()
defer t.Unlock()
var ts time.Time
err := t.db.QueryRow(`SELECT now()`).Scan(&ts)
if err == nil {
ts = ts.Add(- t.eventTTL)
if dbg := t.logger.Check(zap.DebugLevel, "deleting events earlier than"); dbg != nil {
dbg.Write(zap.Time("ts", ts))
}
result, err := t.db.Exec("DELETE FROM events WHERE ts < $1",
ts.Format(`2006-01-02T15:04:05.999999`))
if err == nil {
if c := t.logger.Check(zap.InfoLevel, "Postgres trim"); c != nil {
n, err := result.RowsAffected()
if err == nil {
c.Write(zap.Int64("Events deleted", n))
} else {
c.Write(zap.Error(err))
}
}
} else if c := t.logger.Check(zap.ErrorLevel, "Deleting events"); c != nil {
c.Write(zap.Error(err))
}
} else if !errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("can't get recent timestamp: %w", err)
}
return nil
}
func (t *PostgresTransport) cleanup() {
if c := t.logger.Check(zap.DebugLevel, "Postgres cleanup"); c != nil {
c.Write(zap.Duration("cleanupInterval", t.cleanupInterval),
zap.Duration("envenTTL", t.eventTTL))
}
for t.trim() == nil {
time.Sleep(t.cleanupInterval)
}
}
// Interface guards.
var (
_ Transport = (*PostgresTransport)(nil)
_ TransportSubscribers = (*PostgresTransport)(nil)
)