mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-02 17:50:39 +00:00
39 lines
749 B
Go
39 lines
749 B
Go
package controls
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/weaveworks/scope/common/xfer"
|
|
)
|
|
|
|
var (
|
|
mtx = sync.Mutex{}
|
|
handlers = map[string]xfer.ControlHandlerFunc{}
|
|
)
|
|
|
|
// HandleControlRequest performs a control request.
|
|
func HandleControlRequest(req xfer.Request) xfer.Response {
|
|
mtx.Lock()
|
|
handler, ok := handlers[req.Control]
|
|
mtx.Unlock()
|
|
if !ok {
|
|
return xfer.ResponseErrorf("Control %q not recognised", req.Control)
|
|
}
|
|
|
|
return handler(req)
|
|
}
|
|
|
|
// Register a new control handler under a given id.
|
|
func Register(control string, f xfer.ControlHandlerFunc) {
|
|
mtx.Lock()
|
|
defer mtx.Unlock()
|
|
handlers[control] = f
|
|
}
|
|
|
|
// Rm deletes the handler for a given name
|
|
func Rm(control string) {
|
|
mtx.Lock()
|
|
defer mtx.Unlock()
|
|
delete(handlers, control)
|
|
}
|