Skip to content

Instantly share code, notes, and snippets.

@FZambia
Created December 31, 2016 17:51
Show Gist options
  • Select an option

  • Save FZambia/80a5241e06b4662f7fe89cfaf24072c3 to your computer and use it in GitHub Desktop.

Select an option

Save FZambia/80a5241e06b4662f7fe89cfaf24072c3 to your computer and use it in GitHub Desktop.
Proof of eventual consistency of Redis PUBSUB CHANNELS * command
package main
// Proof of eventual consistency of Redis PUBSUB CHANNELS * command.
import (
"fmt"
"strconv"
"time"
"github.com/garyburd/redigo/redis"
)
func newPool(server string) *redis.Pool {
return &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", server)
if err != nil {
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
if time.Since(t) < time.Minute {
return nil
}
_, err := c.Do("PING")
return err
},
}
}
func main() {
pool := newPool(":6379")
conn := redis.PubSubConn{Conn: pool.Get()}
defer conn.Close()
c := pool.Get()
for j := 0; j < 1000; j++ {
for i := 0; i < 10000; i++ {
conn.Subscribe("pubsubtest-" + strconv.Itoa(i))
conn.Unsubscribe("pubsubtest-" + strconv.Itoa(i))
}
values, _ := redis.Values(c.Do("PUBSUB", "CHANNELS", "*"))
if len(values) != 0 {
println("ooops")
time.Sleep(time.Second)
values, _ := redis.Values(c.Do("PUBSUB", "CHANNELS", "*"))
if len(values) != 0 {
channels := make([]string, 0, len(values))
for i := 0; i < len(values); i++ {
value, okValue := values[i].([]byte)
if !okValue {
panic(1)
}
channels = append(channels, string(value))
}
fmt.Printf("%#v", channels)
} else {
println("ok")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment