diff --git a/README.md b/README.md index fa35d00..d983800 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ func main() { | Option | Default | Description | |-------------------------|-------------------|-------------------------------------------| +| `WithMinWorkers` | `0` | Set the minimum number of workers | | `WithMaxWorkers` | `5` | Set the maximum number of workers | | `WithWorkerIdleTimeout` | `3 * time.Second` | Set the destroyed timeout of idle workers | diff --git a/go.mod b/go.mod index 83ebb3e..ac1ceb5 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/B1NARY-GR0UP/violin -go 1.18 +go 1.21 require ( github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 34a046a..c6aeed2 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,7 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -12,5 +13,6 @@ go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/helper.go b/helper.go index ba2c1e4..d2b0dc8 100644 --- a/helper.go +++ b/helper.go @@ -138,7 +138,7 @@ LOOP: v.waitingQ.AddLast(task) } case <-timer.C: - if v.WorkerNum() > 0 { + if int(v.WorkerNum()) > v.MinWorkerNum() { v.tryDismiss() } timer.Reset(v.options.workerIdleTimeout) diff --git a/option.go b/option.go index 0f97bb3..3a8b620 100644 --- a/option.go +++ b/option.go @@ -20,17 +20,20 @@ import "time" type Option func(*options) type options struct { + minWorkers int maxWorkers int workerIdleTimeout time.Duration } var defaultOptions = options{ + minWorkers: 0, maxWorkers: 5, workerIdleTimeout: 3 * time.Second, } func newOptions(opts ...Option) *options { options := &options{ + minWorkers: defaultOptions.minWorkers, maxWorkers: defaultOptions.maxWorkers, workerIdleTimeout: defaultOptions.workerIdleTimeout, } @@ -44,6 +47,16 @@ func (o *options) apply(opts ...Option) { } } +// WithMinWorkers set the minimum number of workers +func WithMinWorkers(min int) Option { + if min < 0 { + min = 0 + } + return func(o *options) { + o.minWorkers = min + } +} + // WithMaxWorkers set the maximum number of workers func WithMaxWorkers(max int) Option { if max < 1 { diff --git a/option_test.go b/option_test.go index a91a790..094dfbf 100644 --- a/option_test.go +++ b/option_test.go @@ -24,15 +24,18 @@ import ( func TestOptions(t *testing.T) { options := newOptions( + WithMinWorkers(5), WithMaxWorkers(13), WithWorkerIdleTimeout(time.Second*10), ) + assert.Equal(t, 5, options.minWorkers) assert.Equal(t, 13, options.maxWorkers) assert.Equal(t, time.Second*10, options.workerIdleTimeout) } func TestDefaultOptions(t *testing.T) { options := newOptions() + assert.Equal(t, 0, options.minWorkers) assert.Equal(t, 5, options.maxWorkers) assert.Equal(t, time.Second*3, options.workerIdleTimeout) } diff --git a/violin.go b/violin.go index 134c725..701dff2 100644 --- a/violin.go +++ b/violin.go @@ -52,6 +52,7 @@ const ( ) // New VIOLIN worker pool +// TODO: improve performance func New(opts ...Option) *Violin { options := newOptions(opts...) v := &Violin{ @@ -120,6 +121,11 @@ func (v *Violin) IsShutdown() bool { return atomic.LoadUint32(&v.status) == statusShutdown } +// MinWorkerNum returns the minimum number of workers +func (v *Violin) MinWorkerNum() int { + return v.options.minWorkers +} + // MaxWorkerNum returns the maximum number of workers func (v *Violin) MaxWorkerNum() int { return v.options.maxWorkers