-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.Get() on pool not following the pool capacity #42
Comments
Please give me the actual script to reproduce the behavior. |
I'm running faktory server with docker
Following script should help you reproduce the issue. package main
import (
"log"
faktory "github.com/contribsys/faktory/client"
worker "github.com/contribsys/faktory_worker_go"
)
// LazyWorker ..
func LazyWorker(ctx worker.Context, args ...interface{}) error {
log.Println("Executing Job ", ctx.Jid(), args)
return nil
}
func produce() {
log.Println("Connecting to faktory")
pool, err := faktory.NewPool(5)
if err != nil {
log.Fatalln("Unable to create to Faktory pool", err.Error())
}
// Array to hold 10 clients
cli := []*faktory.Client{}
// Get 10 clients continuously
for i := 0; i < 10; i++ {
c, err := pool.Get()
if err != nil { // Expecting to hit this case on 6th iteration or it should wait for a clients to be returned to the pool?
log.Fatalln("Unable to get client from pool", i, err.Error())
}
log.Println("Got client ", i)
log.Println("Pool length ", pool.Len())
cli = append(cli, c)
}
// Schedule 1 job with all 10 clients
for i := 0; i < 10; i++ {
job := faktory.NewJob("lazy", i)
if err := cli[i].Push(job); err != nil {
log.Fatalln("Unable to push job", i, err.Error())
}
log.Println("Scheduled job ", i)
}
// Return all 10 jobs to the pool
for i := 0; i < 10; i++ {
pool.Put(cli[i])
log.Println("Returned client ", i)
log.Println("Pool length ", pool.Len())
}
}
func consume() {
mgr := worker.NewManager()
mgr.Concurrency = 1
mgr.Register("lazy", LazyWorker)
mgr.Run()
}
func main() {
produce()
consume()
} OUTPUT:
Please let me know, If I'm doing something wrong. |
Nevermind, I'm working on it. |
Yeah, looks like this pool impl (that I copied from fatih/pool) doesn't actually limit the number of connections, it only limits the number of connections held in the pool. Any extra will be created on demand, no matter the maximum pool capacity. Arguably a bug, arguably a feature. |
To understand how the pools work I created a pool with capacity of 3.
Then I called
.Get()
10 times, expecting it to fail from the fourth call to.Get()
. But all 10 calls succeeded.I was even able to push jobs using all 10
*faktory.Client
.Finally I tried to return all of them, but only 5 clients were returns.
Is this an expected behavior? If yes, how to control the total number of connections?
The text was updated successfully, but these errors were encountered: