Created
March 1, 2019 17:41
-
-
Save kirugan/510d082f019238ae265f0fea537c3078 to your computer and use it in GitHub Desktop.
debug pool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/gomodule/redigo/redis" | |
"sync" | |
"sync/atomic" | |
"time" | |
) | |
type mock struct { | |
id int64 | |
sleepTime time.Duration | |
closed bool | |
} | |
func (m mock) Close() error { | |
fmt.Printf("%d: closed\n", m.id) | |
return nil | |
} | |
func (mock) Err() error { | |
return nil | |
} | |
func (m mock) Do(commandName string, args ...interface{}) (reply interface{}, err error) { | |
if m.closed { | |
panic("don't do if you closed") | |
} | |
time.Sleep(m.sleepTime) | |
fmt.Printf("%d: done doing\n", m.id) | |
return nil, nil | |
} | |
func (mock) Send(commandName string, args ...interface{}) error { | |
panic("implement me") | |
} | |
func (mock) Flush() error { | |
panic("implement me") | |
} | |
func (mock) Receive() (reply interface{}, err error) { | |
panic("implement me") | |
} | |
var id int64 | |
func main() { | |
pool := redis.Pool{ | |
MaxActive: 3, | |
MaxIdle: 100, | |
Wait: true, | |
Dial: func() (conn redis.Conn, e error) { | |
id := atomic.AddInt64(&id, 1) | |
fmt.Printf("%d: connecting\n", id) | |
return mock{id, time.Second * 2, false}, nil | |
}, | |
} | |
var nWorkers = 30 | |
var wg sync.WaitGroup | |
wg.Add(nWorkers) | |
for i := 0; i < nWorkers; i++ { | |
go func() { | |
defer wg.Done() | |
conn, err := pool.GetContext(context.Background()) | |
if err != nil { | |
panic(err) | |
} | |
_, err = conn.Do("MGET", "something") | |
if err != nil { | |
panic(err) | |
} | |
defer conn.Close() | |
}() | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment