Documentation
¶
Overview ¶
Package pgln provides functionality for PostgreSQL LISTEN/NOTIFY operations.
Index ¶
- type DoneCallbackType
- type ErrorCallbackType
- type ListenOptions
- type NotificationCallbackType
- type NotifyQueryResult
- type OutOfSyncBlockingCallbackType
- type PGListenNotify
- func (r *PGListenNotify) Listen(channel string, options ListenOptions) (chan error, error)
- func (r *PGListenNotify) ListenAndWaitForListening(channel string, options ListenOptions) error
- func (r *PGListenNotify) Notify(channel string, payload string) error
- func (r *PGListenNotify) NotifyQuery(channel string, payload string) NotifyQueryResult
- func (r *PGListenNotify) Shutdown()
- func (r *PGListenNotify) Start() error
- func (r *PGListenNotify) UnListen(channel string) (chan struct{}, error)
- func (r *PGListenNotify) UnlistenAndWaitForUnlistening(channel string) error
- type PGListenNotifyBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DoneCallbackType ¶
type DoneCallbackType func(channel string)
DoneCallbackType is a function type called when listening is done.
type ErrorCallbackType ¶
ErrorCallbackType is a function type for handling errors.
type ListenOptions ¶
type ListenOptions struct { // NotificationCallback is an optional BLOCKING callback that is called for each channel when a notification arrives. // Blocking means that we leave it up to the callback to decide if everything should wait or to spawn a goroutine. NotificationCallback NotificationCallbackType // DoneCallback is an optional BLOCKING callback that is called for each channel when we are done listening. // Blocking means that we leave it up to the callback to decide if everything should wait or to spawn a goroutine. DoneCallback DoneCallbackType // ErrorCallback is an optional BLOCKING callback that is called for each channel when there is an unexpected error. // Blocking means that we leave it up to the callback to decide if everything should wait or to spawn a goroutine. ErrorCallback ErrorCallbackType // OutOfSyncBlockingCallback is an optional BLOCKING callback that is called for each channel when we first connect // and when we reconnect. It is called just before receiving the first notification. // It allows you to catch up or rebuild your caches while we are listening for new notifications, so you do not lose your messages // while rebuilding. // Blocking means that we leave it up to the callback to decide if everything should wait or to spawn a goroutine. OutOfSyncBlockingCallback OutOfSyncBlockingCallbackType // contains filtered or unexported fields }
ListenOptions is used when Listen is called. It also maintains internal state.
type NotificationCallbackType ¶
NotificationCallbackType is a function type for handling notifications.
type NotifyQueryResult ¶ added in v0.1.4
type NotifyQueryResult struct { Query string // The SQL query to be executed Params []any // The parameters for the query }
NotifyQueryResult represents the result of a NotifyQuery call.
type OutOfSyncBlockingCallbackType ¶
OutOfSyncBlockingCallbackType is a function type for handling out-of-sync situations.
type PGListenNotify ¶
type PGListenNotify struct {
// contains filtered or unexported fields
}
PGListenNotify handles listening and notifying operations. It is generated by the Builder.
func (*PGListenNotify) Listen ¶
func (r *PGListenNotify) Listen(channel string, options ListenOptions) (chan error, error)
Listen will "listen" to a channel specified in the options. It returns a channel that will receive an error if there's a failure to listen. To stop listening for a channel, use UnListen.
func (*PGListenNotify) ListenAndWaitForListening ¶ added in v0.1.4
func (r *PGListenNotify) ListenAndWaitForListening(channel string, options ListenOptions) error
ListenAndWaitForListening combines Listen and waiting for the listening to start. It returns an error if there was a failure in listening or if the context is cancelled.
func (*PGListenNotify) Notify ¶
func (r *PGListenNotify) Notify(channel string, payload string) error
Notify allows you to send a notification to a specified channel with a payload.
func (*PGListenNotify) NotifyQuery ¶ added in v0.1.4
func (r *PGListenNotify) NotifyQuery(channel string, payload string) NotifyQueryResult
NotifyQuery returns the query string and parameters for notifying a channel. This allows the caller to run the query themselves in their own transaction. The main reason is that pg_notify is run only if the transaction commits. If the transaction fails, it will not run, which is usually what we want.
func (*PGListenNotify) Shutdown ¶ added in v0.1.4
func (r *PGListenNotify) Shutdown()
Shutdown releases resources that were created internally.
func (*PGListenNotify) Start ¶ added in v0.1.4
func (r *PGListenNotify) Start() error
Start begins the monitoring process. It should be called after Build().
func (*PGListenNotify) UnListen ¶
func (r *PGListenNotify) UnListen(channel string) (chan struct{}, error)
UnListen stops listening for the specified channel. It returns a channel that will be closed when the unlisten operation is complete.
func (*PGListenNotify) UnlistenAndWaitForUnlistening ¶ added in v0.1.6
func (r *PGListenNotify) UnlistenAndWaitForUnlistening(channel string) error
UnlistenAndWaitForUnlistening stops listening for the specified channel and waits until it's completely removed. This function is important for safe shutdown as it ensures that no more notifications will be processed for this channel.
type PGListenNotifyBuilder ¶
type PGListenNotifyBuilder interface { // SetContext allows you to set your own custom context, so we can stop listening when the context is done. SetContext(ctx context.Context) PGListenNotifyBuilder // SetDB allows you to set your own sql.DB connection. SetDB(db *sql.DB) PGListenNotifyBuilder // SetReconnectInterval sets the interval in milliseconds to wait between reconnection attempts. SetReconnectInterval(reconnectInterval time.Duration) PGListenNotifyBuilder // SetHealthCheckTimeout sets the timeout for health check operations. SetHealthCheckTimeout(timeout time.Duration) PGListenNotifyBuilder // Build is the final call to construct the PGListenNotify structure. Build() (*PGListenNotify, error) }
PGListenNotifyBuilder builds a PGListenNotify structure you can use to listen for new notifications.
func NewPGListenNotifyBuilder ¶
func NewPGListenNotifyBuilder() PGListenNotifyBuilder
NewPGListenNotifyBuilder creates a new PGListenNotifyBuilder to configure the listener.