2
0

Unlisten tweaks

- Use unlisten * when releasing connection with listeners to pool
- Only unlisten on releasing connection to pool when necessary
- Remove Unlisten("") as was to release all listeners
This commit is contained in:
Jack Christensen
2015-09-21 08:18:10 -05:00
parent 9fd5c7e6ab
commit ca16a4c98f
2 changed files with 19 additions and 26 deletions
+12 -25
View File
@@ -602,39 +602,26 @@ 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) error {
_, err = c.Exec("listen " + channel) _, err := c.Exec("listen " + channel)
if err != nil { if err != nil {
return return err
} }
var s struct{}
c.channels[channel] = s c.channels[channel] = struct{}{}
return
return nil
} }
// Unlisten unsubscribes from a listen channel // Unlisten unsubscribes from a listen channel
// If channel is empty then unsubscribe from all channels func (c *Conn) Unlisten(channel string) error {
func (c *Conn) Unlisten(channel string) (err error) { _, err := c.Exec("unlisten " + channel)
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 { if err != nil {
return return err
} }
delete(c.channels, channel) delete(c.channels, channel)
return return nil
} }
// WaitForNotification waits for a PostgreSQL notification for up to timeout. // WaitForNotification waits for a PostgreSQL notification for up to timeout.
+7 -1
View File
@@ -118,7 +118,13 @@ func (p *ConnPool) Release(conn *Conn) {
if conn.TxStatus != 'I' { if conn.TxStatus != 'I' {
conn.Exec("rollback") conn.Exec("rollback")
} }
conn.Unlisten("")
if len(conn.channels) > 0 {
if err := conn.Unlisten("*"); err != nil {
conn.die(err)
}
conn.channels = make(map[string]struct{})
}
p.cond.L.Lock() p.cond.L.Lock()
if conn.IsAlive() { if conn.IsAlive() {