Skip to content

Instantly share code, notes, and snippets.

@cloudaice
Created March 19, 2014 09:07
Show Gist options
  • Save cloudaice/9638095 to your computer and use it in GitHub Desktop.
Save cloudaice/9638095 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/garyburd/redigo/redis"
"os"
"runtime"
"sync"
"time"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
}
func main() {
pool := &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
return nil, err
}
_, err = c.Do("AUTH", "123456")
if err != nil {
return nil, err
}
return c, nil
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
wg := sync.WaitGroup{}
for {
fmt.Println(pool.ActiveCount())
time.Sleep(time.Second)
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
conn := pool.Get()
if err := conn.Err(); err != nil {
fmt.Fprintln(os.Stderr, "Get Conn: ", err)
wg.Done()
return
}
for {
_, err := conn.Do("LPUSH", "testqueue", time.Now().Format("2006-01-02 15:04:05"))
if err != nil {
fmt.Fprintln(os.Stderr, "RPOP: ", err)
wg.Done()
break
}
}
}()
}
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
conn := pool.Get()
defer conn.Close()
if err := conn.Err(); err != nil {
fmt.Fprintln(os.Stderr, "Get Conn: ", err)
wg.Done()
return
}
for {
_, err := conn.Do("RPOP", "testqueue")
if err == redis.ErrNil {
time.Sleep(time.Second)
continue
}
if err != nil {
fmt.Fprintln(os.Stderr, "RPOP: ", err)
wg.Done()
break
}
}
}()
}
wg.Wait()
fmt.Fprintln(os.Stderr, "wait done")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment