-
Notifications
You must be signed in to change notification settings - Fork 816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Register liveness check in gameservers.Controller #160
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
package workerqueue | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"agones.dev/agones/pkg/util/runtime" | ||
|
@@ -28,6 +30,10 @@ import ( | |
"k8s.io/client-go/util/workqueue" | ||
) | ||
|
||
const ( | ||
workFx = time.Second | ||
) | ||
|
||
// Handler is the handler for processing the work queue | ||
// This is usually a syncronisation handler for a controller or related | ||
type Handler func(string) error | ||
|
@@ -40,6 +46,10 @@ type WorkerQueue struct { | |
queue workqueue.RateLimitingInterface | ||
// SyncHandler is exported to make testing easier (hack) | ||
SyncHandler Handler | ||
|
||
mu sync.Mutex | ||
workers int | ||
running int | ||
} | ||
|
||
// NewWorkerQueue returns a new worker queue for a given name | ||
|
@@ -106,15 +116,59 @@ func (wq *WorkerQueue) processNextWorkItem() bool { | |
} | ||
|
||
// Run the WorkerQueue processing via the Handler. Will block until stop is closed. | ||
// Runs threadiness number workers to process the rate limited queue | ||
func (wq *WorkerQueue) Run(threadiness int, stop <-chan struct{}) { | ||
defer wq.queue.ShutDown() | ||
|
||
wq.logger.WithField("threadiness", threadiness).Info("Starting workers...") | ||
for i := 0; i < threadiness; i++ { | ||
go wait.Until(wq.runWorker, time.Second, stop) | ||
// Runs a certain number workers to process the rate limited queue | ||
func (wq *WorkerQueue) Run(workers int, stop <-chan struct{}) { | ||
wq.setWorkerCount(workers) | ||
wq.logger.WithField("workers", workers).Info("Starting workers...") | ||
for i := 0; i < workers; i++ { | ||
go wq.run(stop) | ||
} | ||
|
||
<-stop | ||
wq.logger.Info("...shutting down workers") | ||
wq.queue.ShutDown() | ||
} | ||
|
||
func (wq *WorkerQueue) run(stop <-chan struct{}) { | ||
wq.inc() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wq.inc()
defer wq.dec()
wait.Until(wq.runWorker, workFx, stop) ? |
||
defer wq.dec() | ||
wait.Until(wq.runWorker, workFx, stop) | ||
} | ||
|
||
// Healthy reports whether all the worker goroutines are running. | ||
func (wq *WorkerQueue) Healthy() error { | ||
wq.mu.Lock() | ||
defer wq.mu.Unlock() | ||
want := wq.workers | ||
got := wq.running | ||
|
||
if want != got { | ||
return fmt.Errorf("want %d worker goroutine(s), got %d", want, got) | ||
} | ||
return nil | ||
} | ||
|
||
// RunCount reports the number of running worker goroutines started by Run. | ||
func (wq *WorkerQueue) RunCount() int { | ||
wq.mu.Lock() | ||
defer wq.mu.Unlock() | ||
return wq.running | ||
} | ||
|
||
func (wq *WorkerQueue) setWorkerCount(n int) { | ||
wq.mu.Lock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wq.mu.Lock()
defer wq.mu.Unlock()
wq.workers = n ? |
||
defer wq.mu.Unlock() | ||
wq.workers = n | ||
} | ||
|
||
func (wq *WorkerQueue) inc() { | ||
wq.mu.Lock() | ||
defer wq.mu.Unlock() | ||
wq.running++ | ||
} | ||
|
||
func (wq *WorkerQueue) dec() { | ||
wq.mu.Lock() | ||
defer wq.mu.Unlock() | ||
wq.running-- | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a RWLock? I suppose there will only be a single thread hitting the
Healthy()
function, so there isn't much benefit to allow for multiple concurrentHealthy()
requests. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm tempted to start with a plain mutex and leave open the possibility for improving performance of the
WorkerQueue
once we understand the bottle necks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM!