-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
148 additions
and
38 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
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
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 |
---|---|---|
@@ -1,33 +1,49 @@ | ||
package res | ||
|
||
type work struct { | ||
s *Service | ||
wid string // Worker ID for the work queue | ||
queue []func() // Callback queue | ||
s *Service | ||
wid string // Worker ID for the work queue | ||
single [1]func() | ||
queue []func() // Callback queue | ||
} | ||
|
||
// startWorker starts a new resource worker that will listen for resources to | ||
// process requests on. | ||
func (s *Service) startWorker(ch chan *work) { | ||
for w := range ch { | ||
func (s *Service) startWorker() { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
defer s.wg.Done() | ||
// workqueue being nil signals we the service is closing | ||
for s.workqueue != nil { | ||
for len(s.workqueue) == 0 { | ||
s.workcond.Wait() | ||
if s.workqueue == nil { | ||
return | ||
} | ||
} | ||
w := s.workqueue[0] | ||
if len(s.workqueue) == 1 { | ||
s.workqueue = s.workbuf[:0] | ||
} else { | ||
s.workqueue = s.workqueue[1:] | ||
} | ||
w.processQueue() | ||
} | ||
s.wg.Done() | ||
} | ||
|
||
func (w *work) processQueue() { | ||
var f func() | ||
idx := 0 | ||
|
||
w.s.mu.Lock() | ||
for len(w.queue) > idx { | ||
f = w.queue[idx] | ||
w.s.mu.Unlock() | ||
idx++ | ||
f() | ||
w.s.mu.Lock() | ||
} | ||
// Work complete | ||
delete(w.s.rwork, w.wid) | ||
w.s.mu.Unlock() | ||
// Work complete. Delete if it has a work ID. | ||
if w.wid != "" { | ||
delete(w.s.rwork, w.wid) | ||
} | ||
} |