|
| 1 | +// Copyright (c) 2024 Bryan Frimin <bryan@frimin.fr>. |
| 2 | +// |
| 3 | +// Permission to use, copy, modify, and/or distribute this software |
| 4 | +// for any purpose with or without fee is hereby granted, provided |
| 5 | +// that the above copyright notice and this permission notice appear |
| 6 | +// in all copies. |
| 7 | +// |
| 8 | +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
| 9 | +// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
| 10 | +// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE |
| 11 | +// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR |
| 12 | +// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
| 13 | +// OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 14 | +// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 15 | +// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 | + |
| 17 | +package pg |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/jackc/pgx/v5" |
| 24 | +) |
| 25 | + |
| 26 | +// Listen opens a dedicated connection and calls handler for each |
| 27 | +// notification delivered on channel. It returns when ctx is canceled |
| 28 | +// or the connection fails. The caller decides whether to reconnect. |
| 29 | +// |
| 30 | +// The connection is not taken from the pool. LISTEN holds it until |
| 31 | +// the call returns, and the pool would otherwise recycle it. |
| 32 | +func (c *Client) Listen( |
| 33 | + ctx context.Context, |
| 34 | + channel string, |
| 35 | + handler func(payload string), |
| 36 | +) error { |
| 37 | + if channel == "" { |
| 38 | + return fmt.Errorf("cannot listen for PostgreSQL notifications: channel is empty") |
| 39 | + } |
| 40 | + |
| 41 | + if handler == nil { |
| 42 | + return fmt.Errorf("cannot listen for PostgreSQL notifications: handler is nil") |
| 43 | + } |
| 44 | + |
| 45 | + conn, err := pgx.ConnectConfig(ctx, c.connConfig) |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("cannot connect PostgreSQL notification listener: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + defer func() { _ = conn.Close(context.Background()) }() |
| 51 | + |
| 52 | + listen := "LISTEN " + pgx.Identifier{channel}.Sanitize() |
| 53 | + if _, err := conn.Exec(ctx, listen); err != nil { |
| 54 | + return fmt.Errorf("cannot listen for PostgreSQL notifications: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + for { |
| 58 | + notification, err := conn.WaitForNotification(ctx) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("cannot wait for PostgreSQL notification: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + handler(notification.Payload) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Notify publishes payload on channel using a pooled connection. |
| 68 | +// |
| 69 | +// A notification that must commit together with a write has to run |
| 70 | +// inside that transaction, with SELECT pg_notify($1, $2). This method |
| 71 | +// publishes immediately on its own connection. |
| 72 | +func (c *Client) Notify( |
| 73 | + ctx context.Context, |
| 74 | + channel string, |
| 75 | + payload string, |
| 76 | +) error { |
| 77 | + if channel == "" { |
| 78 | + return fmt.Errorf("cannot notify PostgreSQL channel: channel is empty") |
| 79 | + } |
| 80 | + |
| 81 | + return c.WithConn( |
| 82 | + ctx, |
| 83 | + func(ctx context.Context, conn Querier) error { |
| 84 | + _, err := conn.Exec(ctx, "SELECT pg_notify($1, $2)", channel, payload) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("cannot notify PostgreSQL channel: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + return nil |
| 90 | + }, |
| 91 | + ) |
| 92 | +} |
0 commit comments