mirror of https://github.com/jackc/pgx.git
Track Listen/Unlisten and clear subscriptions on conn release
parent
516c88fde3
commit
eb5cadccf9
32
conn.go
32
conn.go
|
@ -53,6 +53,7 @@ type Conn struct {
|
||||||
config ConnConfig // config used when establishing this connection
|
config ConnConfig // config used when establishing this connection
|
||||||
TxStatus byte
|
TxStatus byte
|
||||||
preparedStatements map[string]*PreparedStatement
|
preparedStatements map[string]*PreparedStatement
|
||||||
|
channels map[string]struct{}
|
||||||
notifications []*Notification
|
notifications []*Notification
|
||||||
alive bool
|
alive bool
|
||||||
causeOfDeath error
|
causeOfDeath error
|
||||||
|
@ -197,6 +198,7 @@ func (c *Conn) connect(config ConnConfig, network, address string, tlsConfig *tl
|
||||||
|
|
||||||
c.RuntimeParams = make(map[string]string)
|
c.RuntimeParams = make(map[string]string)
|
||||||
c.preparedStatements = make(map[string]*PreparedStatement)
|
c.preparedStatements = make(map[string]*PreparedStatement)
|
||||||
|
c.channels = make(map[string]struct{})
|
||||||
c.alive = true
|
c.alive = true
|
||||||
c.lastActivityTime = time.Now()
|
c.lastActivityTime = time.Now()
|
||||||
|
|
||||||
|
@ -602,6 +604,36 @@ func (c *Conn) Deallocate(name string) (err error) {
|
||||||
// Listen establishes a PostgreSQL listen/notify to channel
|
// Listen establishes a PostgreSQL listen/notify to channel
|
||||||
func (c *Conn) Listen(channel string) (err error) {
|
func (c *Conn) Listen(channel string) (err error) {
|
||||||
_, err = c.Exec("listen " + channel)
|
_, err = c.Exec("listen " + channel)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var s struct{}
|
||||||
|
c.channels[channel] = s
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unlisten unsubscribes from a listen channel
|
||||||
|
// If channel is empty then unsubscribe from all channels
|
||||||
|
func (c *Conn) Unlisten(channel string) (err error) {
|
||||||
|
if channel != "" {
|
||||||
|
err = c.unlisten(channel)
|
||||||
|
} else {
|
||||||
|
for s, _ := range c.channels {
|
||||||
|
err = c.unlisten(s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Conn) unlisten(channel string) (err error) {
|
||||||
|
_, err = c.Exec("unlisten " + channel)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
delete(c.channels, channel)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -118,6 +118,7 @@ func (p *ConnPool) Release(conn *Conn) {
|
||||||
if conn.TxStatus != 'I' {
|
if conn.TxStatus != 'I' {
|
||||||
conn.Exec("rollback")
|
conn.Exec("rollback")
|
||||||
}
|
}
|
||||||
|
conn.Unlisten("")
|
||||||
|
|
||||||
p.cond.L.Lock()
|
p.cond.L.Lock()
|
||||||
if conn.IsAlive() {
|
if conn.IsAlive() {
|
||||||
|
|
Loading…
Reference in New Issue