mirror of https://github.com/jackc/pgx.git
Remove Conn.Listen and Conn.Unlisten
Use Conn.Exec instead to listen or unlisten.pull/483/head
parent
77a2da2b46
commit
b69179cebb
25
conn.go
25
conn.go
|
@ -66,7 +66,6 @@ type Conn struct {
|
||||||
wbuf []byte
|
wbuf []byte
|
||||||
config *ConnConfig // config used when establishing this connection
|
config *ConnConfig // config used when establishing this connection
|
||||||
preparedStatements map[string]*PreparedStatement
|
preparedStatements map[string]*PreparedStatement
|
||||||
channels map[string]struct{}
|
|
||||||
logger Logger
|
logger Logger
|
||||||
logLevel LogLevel
|
logLevel LogLevel
|
||||||
fp *fastpath
|
fp *fastpath
|
||||||
|
@ -191,7 +190,6 @@ func connect(ctx context.Context, config *ConnConfig, connInfo *pgtype.ConnInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.preparedStatements = make(map[string]*PreparedStatement)
|
c.preparedStatements = make(map[string]*PreparedStatement)
|
||||||
c.channels = make(map[string]struct{})
|
|
||||||
c.cancelQueryCompleted = make(chan struct{})
|
c.cancelQueryCompleted = make(chan struct{})
|
||||||
close(c.cancelQueryCompleted)
|
close(c.cancelQueryCompleted)
|
||||||
c.doneChan = make(chan struct{})
|
c.doneChan = make(chan struct{})
|
||||||
|
@ -573,29 +571,6 @@ func (c *Conn) deallocateContext(ctx context.Context, name string) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen establishes a PostgreSQL listen/notify to channel
|
|
||||||
func (c *Conn) Listen(channel string) error {
|
|
||||||
_, err := c.Exec(context.TODO(), "listen "+quoteIdentifier(channel))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.channels[channel] = struct{}{}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unlisten unsubscribes from a listen channel
|
|
||||||
func (c *Conn) Unlisten(channel string) error {
|
|
||||||
_, err := c.Exec(context.TODO(), "unlisten "+quoteIdentifier(channel))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(c.channels, channel)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Conn) IsAlive() bool {
|
func (c *Conn) IsAlive() bool {
|
||||||
c.mux.Lock()
|
c.mux.Lock()
|
||||||
defer c.mux.Unlock()
|
defer c.mux.Unlock()
|
||||||
|
|
|
@ -617,7 +617,7 @@ func TestSetLogger(t *testing.T) {
|
||||||
t.Fatalf("Expected conn.SetLogger to return %v, but it was %v", nil, oldLogger)
|
t.Fatalf("Expected conn.SetLogger to return %v, but it was %v", nil, oldLogger)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := conn.Listen("foo"); err != nil {
|
if _, err := conn.Exec(context.Background(), "listen foo"); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -631,7 +631,7 @@ func TestSetLogger(t *testing.T) {
|
||||||
t.Fatalf("Expected conn.SetLogger to return %v, but it was %v", l1, oldLogger)
|
t.Fatalf("Expected conn.SetLogger to return %v, but it was %v", l1, oldLogger)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := conn.Listen("bar"); err != nil {
|
if _, err := conn.Exec(context.Background(), "listen bar"); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -657,7 +657,7 @@ func TestSetLogLevel(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := conn.Listen("foo"); err != nil {
|
if _, err := conn.Exec(context.Background(), "listen foo"); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -669,7 +669,7 @@ func TestSetLogLevel(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := conn.Listen("bar"); err != nil {
|
if _, err := conn.Exec(context.Background(), "listen bar"); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
doc.go
2
doc.go
|
@ -220,7 +220,7 @@ pgx can listen to the PostgreSQL notification system with the
|
||||||
WaitForNotification function. It takes a maximum time to wait for a
|
WaitForNotification function. It takes a maximum time to wait for a
|
||||||
notification.
|
notification.
|
||||||
|
|
||||||
err := conn.Listen("channelname")
|
err := conn.Exec("listen channelname")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ func listen() {
|
||||||
}
|
}
|
||||||
defer pool.Release(conn)
|
defer pool.Release(conn)
|
||||||
|
|
||||||
conn.Listen("chat")
|
conn.Exec(context.Background(), "listen chat")
|
||||||
|
|
||||||
for {
|
for {
|
||||||
notification, err := conn.WaitForNotification(context.Background())
|
notification, err := conn.WaitForNotification(context.Background())
|
||||||
|
|
Loading…
Reference in New Issue