Files
weave-scope/probe/controls/controls.go
2015-11-06 17:39:48 +00:00

45 lines
835 B
Go

package controls
import (
"fmt"
"sync"
"github.com/weaveworks/scope/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.Response{
ID: req.ID,
Error: fmt.Sprintf("Control '%s' not recognised", req.Control),
}
}
response := handler(req)
response.ID = req.ID
return response
}
// 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)
}