generated from snivilised/astrolib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(boost): add worker pool wrapper (#271)
- Loading branch information
1 parent
d0a2280
commit 5c3b453
Showing
24 changed files
with
1,134 additions
and
222 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,10 @@ tasks: | |
cmds: | ||
- ginkgo -p ./internal/ants | ||
|
||
tb: | ||
cmds: | ||
- ginkgo -p ./boost | ||
|
||
ti: | ||
cmds: | ||
- go test ./i18n | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package boost | ||
|
||
import "github.com/snivilised/lorax/internal/ants" | ||
|
||
type ( | ||
Option = ants.Option | ||
) | ||
|
||
var ( | ||
WithDisablePurge = ants.WithDisablePurge | ||
WithExpiryDuration = ants.WithExpiryDuration | ||
WithMaxBlockingTasks = ants.WithMaxBlockingTasks | ||
WithNonblocking = ants.WithNonblocking | ||
WithOptions = ants.WithOptions | ||
WithPanicHandler = ants.WithPanicHandler | ||
WithPreAlloc = ants.WithPreAlloc | ||
) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package boost | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type Sequential struct { | ||
Format string | ||
id int | ||
} | ||
|
||
func (g *Sequential) Generate() string { | ||
g.id++ | ||
|
||
return fmt.Sprintf(g.Format, g.id) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package boost | ||
|
||
type observable[O any] struct { | ||
stream JobStreamR[O] | ||
} | ||
|
||
func (o *observable[O]) Observe() { | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package boost_test | ||
|
||
import ( | ||
"runtime" | ||
"sync/atomic" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" //nolint:revive // ok | ||
"github.com/snivilised/lorax/internal/ants" | ||
) | ||
|
||
const ( | ||
_ = 1 << (10 * iota) | ||
KiB // 1024 | ||
MiB // 1048576 | ||
) | ||
|
||
const ( | ||
Param = 100 | ||
AntsSize = 1000 | ||
TestSize = 10000 | ||
n = 100000 | ||
PoolSize = 10 | ||
) | ||
|
||
const ( | ||
RunTimes = 1e6 | ||
PoolCap = 5e4 | ||
BenchParam = 10 | ||
DefaultExpiredTime = 10 * time.Second | ||
) | ||
|
||
var curMem uint64 | ||
|
||
func demoFunc() { | ||
time.Sleep(time.Duration(BenchParam) * time.Millisecond) | ||
} | ||
|
||
func demoPoolFunc(inputCh ants.InputParam) { | ||
n, _ := inputCh.(int) | ||
time.Sleep(time.Duration(n) * time.Millisecond) | ||
} | ||
|
||
var stopLongRunningFunc int32 | ||
|
||
func longRunningFunc() { | ||
for atomic.LoadInt32(&stopLongRunningFunc) == 0 { | ||
runtime.Gosched() | ||
} | ||
} | ||
|
||
func ShowMemStats() { | ||
mem := runtime.MemStats{} | ||
runtime.ReadMemStats(&mem) | ||
curMem = mem.TotalAlloc/MiB - curMem | ||
GinkgoWriter.Printf("memory usage:%d MB", curMem) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package boost | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/snivilised/lorax/internal/ants" | ||
) | ||
|
||
type WorkerPoolInvoker[I, O any] struct { | ||
basePool | ||
functionalPool | ||
sourceJobsChIn JobStream[I] | ||
} | ||
|
||
// NewFuncPool creates a new worker pool using the native ants interface; ie | ||
// new jobs are submitted with Submit(task TaskFunc) | ||
func NewFuncPool[I, O any](size int, | ||
pf ants.PoolFunc, | ||
wg *sync.WaitGroup, | ||
options ...Option, | ||
) (*WorkerPoolInvoker[I, O], error) { | ||
// TODO: the automatic invocation of Add/Done might not | ||
// be valid, need to confirm. I thought that each gr was | ||
// allocated for each job, but this is not necessarily | ||
// the case, because each worker has its own job queue. | ||
// | ||
pool, err := ants.NewPoolWithFunc(size, func(i ants.InputParam) { | ||
defer wg.Done() | ||
pf(i) | ||
}, options...) | ||
|
||
return &WorkerPoolInvoker[I, O]{ | ||
basePool: basePool{ | ||
idGen: &Sequential{}, | ||
wg: wg, | ||
}, | ||
functionalPool: functionalPool{ | ||
pool: pool, | ||
}, | ||
}, err | ||
} | ||
|
||
func (p *WorkerPoolInvoker[I, O]) Post(job ants.InputParam) error { | ||
p.wg.Add(1) // because the gr lifetime is tied to the job not the worker | ||
|
||
return p.pool.Invoke(job) | ||
} | ||
|
||
func (p *WorkerPoolInvoker[I, O]) Running() int { | ||
return p.pool.Running() | ||
} | ||
|
||
func (p *WorkerPoolInvoker[I, O]) Release() { | ||
p.pool.Release() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package boost_test | ||
|
||
import ( | ||
"sync" | ||
|
||
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo ok | ||
. "github.com/onsi/gomega" //nolint:revive // gomega ok | ||
|
||
"github.com/snivilised/lorax/boost" | ||
) | ||
|
||
var _ = Describe("WorkerPoolFunc", func() { | ||
Context("ants", func() { | ||
It("should: not fail", func() { | ||
// TestNonblockingSubmit | ||
var wg sync.WaitGroup | ||
|
||
pool, err := boost.NewFuncPool[int, int](AntsSize, demoPoolFunc, &wg) | ||
|
||
defer pool.Release() | ||
|
||
for i := 0; i < n; i++ { // producer | ||
_ = pool.Post(Param) | ||
} | ||
wg.Wait() | ||
GinkgoWriter.Printf("pool with func, running workers number:%d\n", | ||
pool.Running(), | ||
) | ||
ShowMemStats() | ||
|
||
Expect(err).To(Succeed()) | ||
}) | ||
}) | ||
}) |
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
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
Oops, something went wrong.