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): implement manifold func pool (#276)
- Loading branch information
1 parent
212f623
commit 1789e48
Showing
24 changed files
with
703 additions
and
88 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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
"cSpell.words": [ | ||
"Alloc", | ||
"Assistable", | ||
"Berthe", | ||
"binaryheap", | ||
"bodyclose", | ||
"cenkalti", | ||
|
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,23 @@ | ||
package boost | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
type ( | ||
basePool[O any] struct { | ||
wg *sync.WaitGroup | ||
sequence int32 | ||
outputDupCh *Duplex[JobOutput[O]] | ||
ending bool | ||
} | ||
) | ||
|
||
func (p *basePool[O]) next() int32 { | ||
return atomic.AddInt32(&p.sequence, int32(1)) | ||
} | ||
|
||
func (p *basePool[O]) Observe() JobOutputStreamR[O] { | ||
return p.outputDupCh.ReaderCh | ||
} |
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
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,118 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/snivilised/lorax/boost" | ||
) | ||
|
||
// Demonstrates use of manifold func base worker pool where | ||
// the client manifold func returns an output and an error. An | ||
// output channel is created through which the client receives | ||
// all generated outputs. | ||
|
||
const ( | ||
AntsSize = 1000 | ||
n = 100000 | ||
OutputChSize = 10 | ||
Param = 100 | ||
OutputChTimeout = time.Second / 2 // do not use a value that is similar to interval | ||
interval = time.Second / 10 | ||
) | ||
|
||
func produce(ctx context.Context, | ||
pool *boost.ManifoldFuncPool[int, int], | ||
wg *sync.WaitGroup, | ||
) { | ||
defer wg.Done() | ||
|
||
// Only the producer (observable) knows when the workload is complete | ||
// but clearly it has no idea when the worker-pool is complete. Initially, | ||
// one might think that the worker-pool knows when work is complete | ||
// but this is in correct. The pool only knows when the pool is dormant, | ||
// not that no more jobs will be submitted. | ||
// This poses a problem from the perspective of the consumer; it does | ||
// not know when to exit its output processing loop. | ||
// What this indicates to us is that the knowledge of end of workload is | ||
// a combination of multiple events: | ||
// | ||
// 1) The producer knows when it will submit no more work | ||
// 2) The pool knows when all it's workers are dormant | ||
// | ||
// A non deterministic way for the consumer to exit it's output processing | ||
// loop, is to use a timeout. But what is a sensible value? Only the client | ||
// knows this and even so, it can't really be sure no more outputs will | ||
// arrive after the timeout; essentially its making an educated guess, which | ||
// is not reliable. | ||
// | ||
for i, n := 0, 100; i < n; i++ { | ||
_ = pool.Post(ctx, Param) | ||
} | ||
|
||
pool.EndWork(ctx, interval) | ||
} | ||
|
||
func consume(ctx context.Context, | ||
pool *boost.ManifoldFuncPool[int, int], | ||
wg *sync.WaitGroup, | ||
) { | ||
defer wg.Done() | ||
|
||
rch := pool.Observe() | ||
for { | ||
select { | ||
case output, ok := <-rch: | ||
if !ok { | ||
return | ||
} else { | ||
fmt.Printf("🍒 payload: '%v', id: '%v', seq: '%v' (e: '%v')\n", | ||
output.Payload, output.ID, output.SequenceNo, output.Error, | ||
) | ||
} | ||
case <-time.After(OutputChTimeout): | ||
fmt.Printf("⏱️ timeout!\n") | ||
return | ||
case <-ctx.Done(): | ||
fmt.Printf("❌ cancelled!\n") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
var wg sync.WaitGroup | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
pool, err := boost.NewManifoldFuncPool( | ||
ctx, AntsSize, func(input int) (int, error) { | ||
time.Sleep(time.Duration(input) * time.Millisecond) | ||
|
||
return n + 1, nil | ||
}, &wg, | ||
boost.WithOutput(OutputChSize), | ||
) | ||
|
||
defer pool.Release(ctx) | ||
|
||
if err != nil { | ||
fmt.Printf("🔥 error creating pool: '%v'\n", err) | ||
return | ||
} | ||
|
||
wg.Add(1) | ||
go produce(ctx, pool, &wg) //nolint:wsl // pendant | ||
|
||
wg.Add(1) | ||
go consume(ctx, pool, &wg) //nolint:wsl // pendant | ||
|
||
fmt.Printf("pool with func, no of running workers:%d\n", | ||
pool.Running(), | ||
) | ||
wg.Wait() | ||
fmt.Println("🏁 (manifold-func-pool) FINISHED") | ||
} |
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,49 @@ | ||
package boost | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/snivilised/lorax/internal/ants" | ||
) | ||
|
||
// functionalPool | ||
type functionalPool struct { | ||
pool *ants.PoolWithFunc | ||
} | ||
|
||
func (p *functionalPool) Post(ctx context.Context, job InputParam) error { | ||
return p.pool.Invoke(ctx, job) | ||
} | ||
|
||
func (p *functionalPool) Release(ctx context.Context) { | ||
p.pool.Release(ctx) | ||
} | ||
|
||
func (p *functionalPool) Running() int { | ||
return p.pool.Running() | ||
} | ||
|
||
func (p *functionalPool) Waiting() int { | ||
return p.pool.Waiting() | ||
} | ||
|
||
// taskPool | ||
type taskPool struct { | ||
pool *ants.Pool | ||
} | ||
|
||
func (p *taskPool) Post(ctx context.Context, task TaskFunc) error { | ||
return p.pool.Submit(ctx, task) | ||
} | ||
|
||
func (p *taskPool) Release(ctx context.Context) { | ||
p.pool.Release(ctx) | ||
} | ||
|
||
func (p *taskPool) Running() int { | ||
return p.pool.Running() | ||
} | ||
|
||
func (p *taskPool) Waiting() int { | ||
return p.pool.Waiting() | ||
} |
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,15 @@ | ||
package boost | ||
|
||
// withDefaults prepends boost withDefaults to the sequence of options | ||
func withDefaults(options ...Option) []Option { | ||
const ( | ||
noDefaults = 1 | ||
) | ||
o := make([]Option, 0, len(options)+noDefaults) | ||
o = append(o, WithGenerator(&Sequential{ | ||
Format: "ID:%v", | ||
})) | ||
o = append(o, options...) | ||
|
||
return o | ||
} |
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.