Adds a magic "self" node name to distance queries.

This commit is contained in:
James Phillips 2015-07-24 14:30:53 -07:00
parent 6d845c7d0d
commit e47eea3f3a
3 changed files with 21 additions and 3 deletions

View File

@ -491,7 +491,11 @@ func (s *HTTPServer) parseToken(req *http.Request, token *string) {
func (s *HTTPServer) parseSource(req *http.Request, source *structs.QuerySource) { func (s *HTTPServer) parseSource(req *http.Request, source *structs.QuerySource) {
s.parseDC(req, &source.Datacenter) s.parseDC(req, &source.Datacenter)
if node := req.URL.Query().Get("near"); node != "" { if node := req.URL.Query().Get("near"); node != "" {
source.Node = node if node == "self" {
source.Node = s.agent.config.NodeName
} else {
source.Node = node
}
} }
} }

View File

@ -380,6 +380,18 @@ func TestParseSource(t *testing.T) {
if source.Datacenter != "foo" || source.Node != "bob" { if source.Datacenter != "foo" || source.Node != "bob" {
t.Fatalf("bad: %v", source) 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) { func TestParseWait(t *testing.T) {

View File

@ -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 Adding the optional "?near=" parameter with a node name will sort
the node list in ascending order based on the estimated round trip 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: 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 Adding the optional "?near=" parameter with a node name will sort
the node list in ascending order based on the estimated round trip 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: It returns a JSON body like this: