Commit 5dddf644 authored by Vladimir Barsukov's avatar Vladimir Barsukov
Browse files

zq buff

parent eed33237
...@@ -3,6 +3,7 @@ package zq ...@@ -3,6 +3,7 @@ package zq
import ( import (
"git.barsukov.pro/barsukov/zgo/zatomic" "git.barsukov.pro/barsukov/zgo/zatomic"
"git.barsukov.pro/barsukov/zgo/zwg" "git.barsukov.pro/barsukov/zgo/zwg"
"runtime"
) )
type ZQ struct { type ZQ struct {
...@@ -14,12 +15,12 @@ type ZQ struct { ...@@ -14,12 +15,12 @@ type ZQ struct {
jobs chan any jobs chan any
} }
func New(qtyWorkers int, w func(any)) *ZQ { func New(qtyWorkers int, buff int, fn func(any)) *ZQ {
q := &ZQ{ q := &ZQ{
WorkerCounter: zatomic.New(), WorkerCounter: zatomic.New(),
WorkerFn: w, WorkerFn: fn,
JobsWg: zwg.New(), JobsWg: zwg.New(),
jobs: make(chan any), jobs: make(chan any, buff),
complete: make(chan bool), complete: make(chan bool),
} }
...@@ -29,6 +30,43 @@ func New(qtyWorkers int, w func(any)) *ZQ { ...@@ -29,6 +30,43 @@ func New(qtyWorkers int, w func(any)) *ZQ {
return q return q
} }
func Default(fn func(any)) *ZQ {
return DefaultN(runtime.NumCPU(), fn)
}
func DefaultN(qtyWorkers int, fn func(any)) *ZQ {
return New(qtyWorkers, 0, fn)
}
func Default2(fn func(any)) *ZQ {
return DefaultN(2, fn)
}
func Default4(fn func(any)) *ZQ {
return DefaultN(4, fn)
}
func Default6(fn func(any)) *ZQ {
return DefaultN(6, fn)
}
func Default8(fn func(any)) *ZQ {
return DefaultN(8, fn)
}
func Default12(fn func(any)) *ZQ {
return DefaultN(12, fn)
}
func Default16(fn func(any)) *ZQ {
return DefaultN(16, fn)
}
func Default24(fn func(any)) *ZQ {
return DefaultN(24, fn)
}
func Default32(fn func(any)) *ZQ {
return DefaultN(32, fn)
}
func Default48(fn func(any)) *ZQ {
return DefaultN(48, fn)
}
func Default64(fn func(any)) *ZQ {
return DefaultN(64, fn)
}
func (q *ZQ) AddWorker(delta int) { func (q *ZQ) AddWorker(delta int) {
if delta < 1 { if delta < 1 {
...@@ -64,16 +102,6 @@ func (q *ZQ) RemoveWorker() { ...@@ -64,16 +102,6 @@ func (q *ZQ) RemoveWorker() {
func (q *ZQ) AddJob(a any) *ZQ { func (q *ZQ) AddJob(a any) *ZQ {
q.JobsWg.Inc() q.JobsWg.Inc()
go func() {
q.jobs <- a
}()
return q
}
func (q *ZQ) AddJobSync(a any) *ZQ {
q.JobsWg.Inc()
q.jobs <- a q.jobs <- a
return q return q
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment