Skip to content

Commit

Permalink
feat: support min workers
Browse files Browse the repository at this point in the history
  • Loading branch information
justlorain committed Sep 21, 2023
1 parent 5d8899d commit a6de653
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/B1NARY-GR0UP/violin

go 1.18
go 1.21

require (
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
2 changes: 1 addition & 1 deletion helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 6 additions & 0 deletions violin.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
)

// New VIOLIN worker pool
// TODO: improve performance
func New(opts ...Option) *Violin {
options := newOptions(opts...)
v := &Violin{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a6de653

Please sign in to comment.