-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen_notify.go
58 lines (50 loc) · 1.58 KB
/
listen_notify.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
package postgres
import (
"context"
"time"
)
func (pc *PostgresClient) StartListeningToNotifications(ctx context.Context) error {
poolConn, err := pc.connPool.Acquire(ctx)
if err != nil {
pc.log.Error("acquire connection error: ", err.Error())
return err
}
//* registra a conexão para o channel 'name'
conn := poolConn.Conn()
for _, ch := range pc.eventBus.Topics {
_, err = conn.Exec(ctx, "listen "+ch)
if err != nil {
pc.log.Warnw("failed to listen", "error", err.Error(), "channel", ch)
}
pc.log.Infow("listening with success", "channel", ch)
pc.activeChannels = append(pc.activeChannels, ch)
}
go func() {
defer func() {
pc.log.Debug("stoping event listening")
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
for _, ch := range pc.activeChannels {
_, err := conn.Exec(ctx, "unlisten "+ch)
if err != nil {
pc.log.Error("failed to unlisten event with: ", err.Error())
}
}
poolConn.Release() //! porque se não a conexão com o banco nunca será fechada
cancel()
}()
for {
//* if ctx is done, err will be non-nil and this func will return
msg, err := conn.WaitForNotification(ctx)
if err != nil {
pc.log.Info("stoped listening to postgres notifications with: ", err.Error())
return
}
pc.log.Infow("new notification from postgres", "channel", msg.Channel, "payload", msg.Payload)
err = pc.eventBus.Publish(msg.Channel, msg.Payload)
if err != nil {
pc.log.Warnw("failed to publish message", "error", err.Error(), "channel", msg.Channel, "message", msg.Payload)
}
}
}()
return nil
}