Skip to content

Instantly share code, notes, and snippets.

@cezarsa
Created July 29, 2014 22:01
Show Gist options
  • Save cezarsa/30495a5617e156a49f67 to your computer and use it in GitHub Desktop.
Save cezarsa/30495a5617e156a49f67 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/garyburd/redigo/redis"
"time"
)
func main() {
pool := &redis.Pool{
MaxIdle: 1,
IdleTimeout: time.Second,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", "localhost:6379")
},
}
pubSub := &redis.PubSubConn{Conn: pool.Get()}
err := pubSub.Subscribe("somekey")
if err != nil {
panic(err)
}
go func() {
for {
switch v := pubSub.Receive().(type) {
case redis.Subscription:
if v.Count == 0 {
return
}
case error:
return
}
}
}()
time.Sleep(1 * time.Second)
err = pubSub.Unsubscribe("somekey")
if err != nil {
panic(err)
}
fmt.Println("before close")
err = pubSub.Close()
if err != nil {
panic(err)
}
fmt.Println("after close")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment