Skip to content

Commit

Permalink
Don't ask for a service worker until it's necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
afshin committed Jun 30, 2016
1 parent 84fd587 commit 9d761a6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
16 changes: 7 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,27 @@ func (c *Client) dispatch(payload []byte) error {
// sleuth://foo-service/bar?baz=qux
func (c *Client) Do(req *http.Request) (*http.Response, error) {
if c.closed {
return nil, newError(errClosed, "client is closed").escalate(errRequest)
return nil, newError(errClosed, "client is closed").escalate(errDo)
}
url := req.URL.String()
to := req.URL.Host
// Handles are hexadecimal strings that are incremented by one.
handle := strconv.FormatInt(c.handle, 16)
c.handle++
if req.URL.Scheme != scheme {
err := newError(errScheme,
"URL scheme must be \"%s\" in %s", scheme, req.URL.String())
err := newError(errScheme, "URL scheme must be \"%s\" in %s", scheme, url)
return nil, err
}
services, ok := c.services[to]
if !ok {
return nil, newError(errUnknownService, "%s is an unknown service", to)
}
p := services.next()
payload, err := reqMarshal(c.group, c.node.UUID(), handle, req)
if err != nil {
return nil, err.(*Error).escalate(errRequest)
return nil, err.(*Error).escalate(errDo)
}
c.log.Debug("sleuth: %s %s://%s@%s%s",
req.Method, scheme, to, p.name, req.URL.String())
p := services.next()
c.log.Debug("sleuth: %s %s://%s@%s%s", req.Method, scheme, to, p.name, url)
if err = c.node.Whisper(p.node, payload); err != nil {
return nil, newError(errReqWhisper, err.Error())
}
Expand All @@ -172,8 +171,7 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
if response != nil {
return response, nil
}
return nil, newError(errTimeout,
"%s {%s}%s timed out", req.Method, to, req.URL.String())
return nil, newError(errTimeout, "%s {%s}%s timed out", req.Method, to, url)
}

func (c *Client) has(services ...string) bool {
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const (
errLeave = 931
errUnzip = 932
errUnzipRead = 933
errRequest = 934
errDo = 934
errClosed = 935
errWait = 936
)
Expand Down
2 changes: 1 addition & 1 deletion sleuth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestClientDoClosed(t *testing.T) {
t.Errorf("expected client Do to fail when client is closed")
return
}
testCodes(t, err, []int{errClosed, errRequest})
testCodes(t, err, []int{errClosed, errDo})
}

func TestClientDoTimeout(t *testing.T) {
Expand Down

0 comments on commit 9d761a6

Please sign in to comment.