pool certificate issuance requests

This commit is contained in:
yoan
2025-01-16 14:42:54 +08:00
parent 2218be5d34
commit 2dd8fb2ee2
2 changed files with 73 additions and 1 deletions

View File

@@ -0,0 +1,46 @@
package pool
import (
"context"
)
type Task[I, O any] func(I) O
type Pool[I, O any] struct {
ch chan struct{}
size int
}
func NewPool[I, O any](size int) *Pool[I, O] {
return &Pool[I, O]{
ch: make(chan struct{}, size),
size: size,
}
}
func (p *Pool[I, O]) Submit(ctx context.Context, task Task[I, O], input I) <-chan O {
resultChan := make(chan O, 1)
go func() {
select {
case p.ch <- struct{}{}:
defer func() {
<-p.ch
close(resultChan)
}()
result := task(input)
select {
case <-ctx.Done():
return
case resultChan <- result:
}
case <-ctx.Done():
close(resultChan)
return
}
}()
return resultChan
}