File tree 2 files changed +21
-1
lines changed
2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -152,6 +152,9 @@ type conn struct {
152
152
153
153
// If not nil, notices will be synchronously sent here
154
154
noticeHandler func (* Error )
155
+
156
+ // If not nil, notifications will be synchronously sent here
157
+ notificationHandler func (* Notification )
155
158
}
156
159
157
160
// Handle driver-side settings in parsed connection string.
@@ -977,6 +980,10 @@ func (cn *conn) recv() (t byte, r *readBuf) {
977
980
if n := cn .noticeHandler ; n != nil {
978
981
n (parseError (r ))
979
982
}
983
+ case 'A' :
984
+ if n := cn .notificationHandler ; n != nil {
985
+ n (recvNotification (r ))
986
+ }
980
987
default :
981
988
return
982
989
}
@@ -994,7 +1001,9 @@ func (cn *conn) recv1Buf(r *readBuf) byte {
994
1001
995
1002
switch t {
996
1003
case 'A' :
997
- // ignore
1004
+ if n := cn .notificationHandler ; n != nil {
1005
+ n (recvNotification (r ))
1006
+ }
998
1007
case 'N' :
999
1008
if n := cn .noticeHandler ; n != nil {
1000
1009
n (parseError (r ))
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ package pq
4
4
// This module contains support for Postgres LISTEN/NOTIFY.
5
5
6
6
import (
7
+ "database/sql/driver"
7
8
"errors"
8
9
"fmt"
9
10
"sync"
@@ -29,6 +30,16 @@ func recvNotification(r *readBuf) *Notification {
29
30
return & Notification {bePid , channel , extra }
30
31
}
31
32
33
+ // SetNotificationHandler sets the given notification handler on the given
34
+ // connection. A runtime panic occurs if c is not a pq connection. A nil handler
35
+ // may be used to unset it.
36
+ //
37
+ // Note: Notification handlers are executed synchronously by pq meaning commands
38
+ // won't continue to be processed until the handler returns.
39
+ func SetNotificationHandler (c driver.Conn , handler func (* Notification )) {
40
+ c .(* conn ).notificationHandler = handler
41
+ }
42
+
32
43
const (
33
44
connStateIdle int32 = iota
34
45
connStateExpectResponse
You can’t perform that action at this time.
0 commit comments