Skip to content

Commit

Permalink
support queue
Browse files Browse the repository at this point in the history
  • Loading branch information
2yz committed May 21, 2017
1 parent da5a00f commit 672af32
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
22 changes: 6 additions & 16 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,24 @@ func (wp *WorkerPool) clearWorkerQueue() {
}

func (wp *WorkerPool) Queue(unit WorkUnit) error {
return wp.queue(unit, NO_WAIT)
go wp.queue(unit)
return nil
}

func (wp *WorkerPool) QueueAndWait(unit WorkUnit) error {
return wp.queue(unit, WAIT)
return wp.queue(unit)
}

func (wp *WorkerPool) queue(unit WorkUnit, isWait bool) error {
w, err := wp.getWorker(isWait)
func (wp *WorkerPool) queue(unit WorkUnit) error {
w, err := wp.getWorker()
if err != nil {
return err
}
w.ch <- unit
return nil
}

func (wp *WorkerPool) getWorker(isWait bool) (*Worker, error) {
func (wp *WorkerPool) getWorker() (*Worker, error) {
createWorker := false

wp.cond.L.Lock()
Expand All @@ -111,17 +112,6 @@ func (wp *WorkerPool) getWorker(isWait bool) (*Worker, error) {
return w, nil
}

// NO_WAIT
if !isWait {
select {
case w := <-wp.workerQueue:
return w, nil
default:
return nil, errors.New("No Worker Available")
}
}

// WAIT
tick := time.Tick(10 * time.Millisecond)
for {
select {
Expand Down

0 comments on commit 672af32

Please sign in to comment.