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(ants,boost): add client context (#273)
- Loading branch information
1 parent
f541404
commit 667a9a8
Showing
16 changed files
with
300 additions
and
77 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/snivilised/lorax/boost" | ||
"github.com/snivilised/lorax/internal/ants" | ||
) | ||
|
||
// Demonstrates that when all workers are engaged and the pool is at capacity, | ||
// new incoming jobs are blocked, until a worker becomes free. The invoked function | ||
// takes a second to complete. The PRE and POST indicators reflect this: | ||
// | ||
// PRE: <--- (n: 0) [13:56:22] 🍋 | ||
// => running: '0') | ||
// POST: <--- (n: 0) [13:56:22] 🍊 | ||
// PRE: <--- (n: 1) [13:56:22] 🍋 | ||
// => running: '1') | ||
// POST: <--- (n: 1) [13:56:22] 🍊 | ||
// PRE: <--- (n: 2) [13:56:22] 🍋 | ||
// => running: '2') | ||
// POST: <--- (n: 2) [13:56:22] 🍊 | ||
// PRE: <--- (n: 3) [13:56:22] 🍋 | ||
// => running: '3') | ||
// <--- (n: 2)🍒 | ||
// <--- (n: 1)🍒 | ||
// <--- (n: 0)🍒 | ||
// <--- (n: 3)🍒 | ||
// POST: <--- (n: 3) [13:56:23] 🍊 | ||
// | ||
// Considering the above, whilst the pool is not at capacity, each new submission is | ||
// executed immediately, as a new worker can be allocated to those jobs (n=0..2). | ||
// Once the pool has reached capacity (n=3), the PRE is blocked, because its corresponding | ||
// POST doesn't happen until a second later; this illustrates the blocking. | ||
// | ||
|
||
func main() { | ||
var wg sync.WaitGroup | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
const NoW = 3 | ||
|
||
pool, _ := boost.NewFuncPool[int, int](ctx, NoW, func(inputCh ants.InputParam) { | ||
n, _ := inputCh.(int) | ||
fmt.Printf("<--- (n: %v)🍒 \n", n) | ||
time.Sleep(time.Second) | ||
}, &wg, ants.WithNonblocking(false)) | ||
|
||
defer pool.Release() | ||
|
||
for i := 0; i < 30; i++ { // producer | ||
fmt.Printf("PRE: <--- (n: %v) [%v] 🍋 \n", i, time.Now().Format(time.TimeOnly)) | ||
_ = pool.Post(i) | ||
fmt.Printf("POST: <--- (n: %v) [%v] 🍊 \n", i, time.Now().Format(time.TimeOnly)) | ||
} | ||
|
||
fmt.Printf("pool with func, running workers number:%d\n", | ||
pool.Running(), | ||
) | ||
wg.Wait() | ||
fmt.Println("🏁 (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,64 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/snivilised/lorax/boost" | ||
) | ||
|
||
// Demonstrates that when all workers are engaged and the pool is at capacity, | ||
// new incoming jobs are blocked, until a worker becomes free. The invoked function | ||
// takes a second to complete. The PRE and POST indicators reflect this: | ||
// | ||
// PRE: <--- (n: 0) [13:41:49] 🍋 | ||
// POST: <--- (n: 0) [13:41:49] 🍊 | ||
// PRE: <--- (n: 1) [13:41:49] 🍋 | ||
// POST: <--- (n: 1) [13:41:49] 🍊 | ||
// PRE: <--- (n: 2) [13:41:49] 🍋 | ||
// POST: <--- (n: 2) [13:41:49] 🍊 | ||
// PRE: <--- (n: 3) [13:41:49] 🍋 | ||
// => running: '3') | ||
// <--- (n: 2)🍒 | ||
// => running: '3') | ||
// <--- (n: 1)🍒 | ||
// => running: '3') | ||
// <--- (n: 0)🍒 | ||
// POST: <--- (n: 3) [13:41:50] 🍊 | ||
// | ||
// Considering the above, whilst the pool is not at capacity, each new submission is | ||
// executed immediately, as a new worker can be allocated to those jobs (n=0..2). | ||
// Once the pool has reached capacity (n=3), the PRE is blocked, because its corresponding | ||
// POST doesn't happen until a second later; this illustrates the blocking. | ||
// | ||
|
||
func main() { | ||
var wg sync.WaitGroup | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
const NoW = 3 | ||
|
||
pool, _ := boost.NewTaskPool[int, int](ctx, NoW, &wg) | ||
|
||
defer pool.Release() | ||
|
||
for i := 0; i < 30; i++ { // producer | ||
fmt.Printf("PRE: <--- (n: %v) [%v] 🍋 \n", i, time.Now().Format(time.TimeOnly)) | ||
_ = pool.Post(func() { | ||
fmt.Printf("=> running: '%v')\n", pool.Running()) | ||
fmt.Printf("<--- (n: %v)🍒 \n", i) | ||
time.Sleep(time.Second) | ||
}) | ||
fmt.Printf("POST: <--- (n: %v) [%v] 🍊 \n", i, time.Now().Format(time.TimeOnly)) | ||
} | ||
|
||
fmt.Printf("task pool, running workers number:%d\n", | ||
pool.Running(), | ||
) | ||
wg.Wait() | ||
fmt.Println("🏁 (task-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
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
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.