|
|
@@ -2,26 +2,40 @@ package wkrp
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
-type TJobChan chan IJob
|
|
|
+const (
|
|
|
+ def_POOL_SIZE = 64
|
|
|
+ def_ENQCHAN_SIZE = 64
|
|
|
+)
|
|
|
+
|
|
|
+type TJobChan chan *Job
|
|
|
type TJobPool chan TJobChan
|
|
|
|
|
|
type Dispacher struct {
|
|
|
- size int
|
|
|
- enqchan TJobChan
|
|
|
- pool TJobPool
|
|
|
- workers map[int]*Worker
|
|
|
- quit chan struct{}
|
|
|
- wg sync.WaitGroup
|
|
|
+ enqchan TJobChan
|
|
|
+ enqchanSz int
|
|
|
+ pool TJobPool
|
|
|
+ poolSz int
|
|
|
+ workers map[int]*Worker
|
|
|
+ quit chan struct{}
|
|
|
+ wg sync.WaitGroup
|
|
|
}
|
|
|
|
|
|
-func NewDispacher(q_size int) *Dispacher {
|
|
|
+func NewDispacher(pool_size int, cache_size int) *Dispacher {
|
|
|
+ if pool_size < 1 {
|
|
|
+ pool_size = def_POOL_SIZE
|
|
|
+ }
|
|
|
+ if cache_size < 1 {
|
|
|
+ cache_size = def_ENQCHAN_SIZE
|
|
|
+ }
|
|
|
+
|
|
|
p := &Dispacher{
|
|
|
- size: q_size,
|
|
|
- enqchan: make(TJobChan, 64),
|
|
|
- pool: make(TJobPool, q_size),
|
|
|
- workers: make(map[int]*Worker),
|
|
|
- quit: make(chan struct{}),
|
|
|
- wg: sync.WaitGroup{},
|
|
|
+ poolSz: pool_size,
|
|
|
+ enqchanSz: cache_size,
|
|
|
+ enqchan: make(TJobChan, cache_size),
|
|
|
+ pool: make(TJobPool, pool_size),
|
|
|
+ workers: make(map[int]*Worker),
|
|
|
+ quit: make(chan struct{}),
|
|
|
+ wg: sync.WaitGroup{},
|
|
|
}
|
|
|
|
|
|
go p.run()
|
|
|
@@ -44,7 +58,7 @@ func (self *Dispacher) Stop() {
|
|
|
}()
|
|
|
}
|
|
|
|
|
|
-func (self *Dispacher) Do(job IJob) {
|
|
|
+func (self *Dispacher) Do(job *Job) {
|
|
|
go func() {
|
|
|
select {
|
|
|
case <-self.quit:
|
|
|
@@ -54,11 +68,11 @@ func (self *Dispacher) Do(job IJob) {
|
|
|
}
|
|
|
|
|
|
func (self *Dispacher) run() {
|
|
|
- for i := 0; i < self.size; i++ {
|
|
|
+ for i := 0; i < self.poolSz; i++ {
|
|
|
w := newWorker(i+1, self)
|
|
|
self.workers[i+1] = w
|
|
|
|
|
|
- self.wg.Add(1);
|
|
|
+ self.wg.Add(1)
|
|
|
|
|
|
w.start()
|
|
|
}
|