代码拉取完成,页面将自动刷新
package pg
import (
"log"
"sync"
"time"
"gopkg.in/pg.v4/internal/pool"
)
// Not thread-safe.
type Listener struct {
channels []string
db *DB
_cn *pool.Conn
closed bool
mx sync.Mutex
}
func (l *Listener) conn(readTimeout time.Duration) (*pool.Conn, error) {
defer l.mx.Unlock()
l.mx.Lock()
if l.closed {
return nil, errListenerClosed
}
if l._cn == nil {
cn, err := l.db.conn()
if err != nil {
return nil, err
}
l._cn = cn
if len(l.channels) > 0 {
if err := l.listen(cn, l.channels...); err != nil {
return nil, err
}
}
}
l._cn.SetReadTimeout(readTimeout)
l._cn.SetWriteTimeout(l.db.opt.WriteTimeout)
return l._cn, nil
}
func (l *Listener) Listen(channels ...string) error {
cn, err := l.conn(l.db.opt.ReadTimeout)
if err != nil {
return err
}
if err := l.listen(cn, channels...); err != nil {
if err != nil {
l.freeConn(err)
}
return err
}
l.channels = append(l.channels, channels...)
return nil
}
func (l *Listener) listen(cn *pool.Conn, channels ...string) error {
for _, channel := range channels {
if err := writeQueryMsg(cn.Wr, "LISTEN ?", F(channel)); err != nil {
return err
}
}
return cn.Wr.Flush()
}
func (l *Listener) Receive() (channel string, payload string, err error) {
return l.ReceiveTimeout(0)
}
func (l *Listener) ReceiveTimeout(readTimeout time.Duration) (channel, payload string, err error) {
channel, payload, err = l.receiveTimeout(readTimeout)
if err != nil {
l.freeConn(err)
}
return channel, payload, err
}
func (l *Listener) receiveTimeout(readTimeout time.Duration) (channel, payload string, err error) {
cn, err := l.conn(readTimeout)
if err != nil {
return "", "", err
}
return readNotification(cn)
}
func (l *Listener) freeConn(err error) (retErr error) {
if !isBadConn(err, true) {
return nil
}
log.Printf("pg: discarding bad listener connection: %s", err)
return l.closeConn(err)
}
func (l *Listener) closeConn(err error) (retErr error) {
l.mx.Lock()
if l._cn != nil {
retErr = l.db.pool.Remove(l._cn, err)
l._cn = nil
}
l.mx.Unlock()
return retErr
}
func (l *Listener) Close() error {
l.mx.Lock()
closed := l.closed
l.closed = true
l.mx.Unlock()
if closed {
return errListenerClosed
}
return l.closeConn(errListenerClosed)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。