From e47eea3f3acd25ac17c383e6986de1d2ed77d03e Mon Sep 17 00:00:00 2001 From: James Phillips Date: Fri, 24 Jul 2015 14:30:53 -0700 Subject: [PATCH] Adds a magic "self" node name to distance queries. --- command/agent/http.go | 6 +++++- command/agent/http_test.go | 12 ++++++++++++ website/source/docs/agent/http/catalog.html.markdown | 6 ++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/command/agent/http.go b/command/agent/http.go index b217c52637..350862cae4 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -491,7 +491,11 @@ func (s *HTTPServer) parseToken(req *http.Request, token *string) { func (s *HTTPServer) parseSource(req *http.Request, source *structs.QuerySource) { s.parseDC(req, &source.Datacenter) if node := req.URL.Query().Get("near"); node != "" { - source.Node = node + if node == "self" { + source.Node = s.agent.config.NodeName + } else { + source.Node = node + } } } diff --git a/command/agent/http_test.go b/command/agent/http_test.go index ff10a1fff5..1b39e68116 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -380,6 +380,18 @@ func TestParseSource(t *testing.T) { if source.Datacenter != "foo" || source.Node != "bob" { t.Fatalf("bad: %v", source) } + + // The magic "self" node name will use the agent's local node name. + req, err = http.NewRequest("GET", + "/v1/catalog/nodes?near=self", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + source = structs.QuerySource{} + srv.parseSource(req, &source) + if source.Datacenter != "dc1" || source.Node != srv.agent.config.NodeName { + t.Fatalf("bad: %v", source) + } } func TestParseWait(t *testing.T) { diff --git a/website/source/docs/agent/http/catalog.html.markdown b/website/source/docs/agent/http/catalog.html.markdown index ccf67311b0..300e8b008a 100644 --- a/website/source/docs/agent/http/catalog.html.markdown +++ b/website/source/docs/agent/http/catalog.html.markdown @@ -181,7 +181,8 @@ however, the dc can be provided using the "?dc=" query parameter. Adding the optional "?near=" parameter with a node name will sort the node list in ascending order based on the estimated round trip -time from that node. +time from that node. Passing "?near=self" will use the agent's local +node for the sort. It returns a JSON body like this: @@ -236,7 +237,8 @@ by tag using the "?tag=" query parameter. Adding the optional "?near=" parameter with a node name will sort the node list in ascending order based on the estimated round trip -time from that node. +time from that node. Passing "?near=self" will use the agent's local +node for the sort. It returns a JSON body like this: