-
Notifications
You must be signed in to change notification settings - Fork 712
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
Add controls form container start, stop, pause, etc #598
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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"math/rand" | ||
"net/http" | ||
"net/rpc" | ||
"sync" | ||
|
||
"github.com/gorilla/mux" | ||
|
||
"github.com/weaveworks/scope/xfer" | ||
) | ||
|
||
func registerControlRoutes(router *mux.Router) { | ||
controlRouter := &controlRouter{ | ||
probes: map[string]controlHandler{}, | ||
} | ||
router.Methods("GET").Path("/api/control/ws").HandlerFunc(controlRouter.handleProbeWS) | ||
router.Methods("POST").MatcherFunc(URLMatcher("/api/control/{probeID}/{nodeID}/{control}")).HandlerFunc(controlRouter.handleControl) | ||
} | ||
|
||
type controlHandler struct { | ||
id int64 | ||
client *rpc.Client | ||
} | ||
|
||
func (ch *controlHandler) handle(req xfer.Request) xfer.Response { | ||
var res xfer.Response | ||
if err := ch.client.Call("control.Handle", req, &res); err != nil { | ||
return xfer.ResponseError(err) | ||
} | ||
return res | ||
} | ||
|
||
type controlRouter struct { | ||
sync.Mutex | ||
probes map[string]controlHandler | ||
} | ||
|
||
func (cr *controlRouter) get(probeID string) (controlHandler, bool) { | ||
cr.Lock() | ||
defer cr.Unlock() | ||
handler, ok := cr.probes[probeID] | ||
return handler, ok | ||
} | ||
|
||
func (cr *controlRouter) set(probeID string, handler controlHandler) { | ||
cr.Lock() | ||
defer cr.Unlock() | ||
cr.probes[probeID] = handler | ||
} | ||
|
||
func (cr *controlRouter) rm(probeID string, handler controlHandler) { | ||
cr.Lock() | ||
defer cr.Unlock() | ||
// NB probe might have reconnected in the mean time, need to ensure we do not | ||
// delete new connection! Also, it might have connected then deleted itself! | ||
if cr.probes[probeID].id == handler.id { | ||
delete(cr.probes, probeID) | ||
} | ||
} | ||
|
||
// handleControl routes control requests from the client to the appropriate | ||
// probe. Its is blocking. | ||
func (cr *controlRouter) handleControl(w http.ResponseWriter, r *http.Request) { | ||
var ( | ||
vars = mux.Vars(r) | ||
probeID = vars["probeID"] | ||
nodeID = vars["nodeID"] | ||
control = vars["control"] | ||
) | ||
handler, ok := cr.get(probeID) | ||
if !ok { | ||
log.Printf("Probe %s is not connected right now...", probeID) | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
result := handler.handle(xfer.Request{ | ||
ID: rand.Int63(), | ||
NodeID: nodeID, | ||
Control: control, | ||
}) | ||
if result.Error != "" { | ||
respondWith(w, http.StatusBadRequest, result.Error) | ||
return | ||
} | ||
respondWith(w, http.StatusOK, result.Value) | ||
} | ||
|
||
// handleProbeWS accepts websocket connections from the probe and registers | ||
// them in the control router, such that HandleControl calls can find them. | ||
func (cr *controlRouter) handleProbeWS(w http.ResponseWriter, r *http.Request) { | ||
probeID := r.Header.Get(xfer.ScopeProbeIDHeader) | ||
if probeID == "" { | ||
respondWith(w, http.StatusBadRequest, xfer.ScopeProbeIDHeader) | ||
return | ||
} | ||
|
||
conn, err := upgrader.Upgrade(w, r, nil) | ||
if err != nil { | ||
log.Printf("Error upgrading to websocket: %v", err) | ||
return | ||
} | ||
defer conn.Close() | ||
|
||
codec := xfer.NewJSONWebsocketCodec(conn) | ||
client := rpc.NewClientWithCodec(codec) | ||
handler := controlHandler{ | ||
id: rand.Int63(), | ||
client: client, | ||
} | ||
|
||
cr.set(probeID, handler) | ||
|
||
codec.WaitForReadError() | ||
|
||
cr.rm(probeID, handler) | ||
client.Close() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/weaveworks/scope/xfer" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
func TestControl(t *testing.T) { | ||
router := mux.NewRouter() | ||
registerControlRoutes(router) | ||
server := httptest.NewServer(router) | ||
defer server.Close() | ||
|
||
ip, port, err := net.SplitHostPort(strings.TrimPrefix(server.URL, "http://")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
probeConfig := xfer.ProbeConfig{ | ||
ProbeID: "foo", | ||
} | ||
client, err := xfer.NewAppClient(probeConfig, ip+":"+port, ip+":"+port) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer client.Stop() | ||
|
||
client.ControlConnection(xfer.ControlHandlerFunc(func(req xfer.Request) xfer.Response { | ||
if req.NodeID != "nodeid" { | ||
t.Fatalf("'%s' != 'nodeid'", req.NodeID) | ||
} | ||
|
||
if req.Control != "control" { | ||
t.Fatalf("'%s' != 'control'", req.Control) | ||
} | ||
|
||
return xfer.Response{ | ||
Value: "foo", | ||
} | ||
})) | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
httpClient := http.Client{ | ||
Timeout: 1 * time.Second, | ||
} | ||
resp, err := httpClient.Post(server.URL+"/api/control/foo/nodeid/control", "", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var response string | ||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if response != "foo" { | ||
t.Fatalf("'%s' != 'foo'", response) | ||
} | ||
} |
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
Oops, something went wrong.
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.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.