diff --git a/client/client.go b/client/client.go index 73c4e06..e4ca3a9 100644 --- a/client/client.go +++ b/client/client.go @@ -8,6 +8,7 @@ import ( "net/http" "net/url" "strings" + "time" "github.com/gorilla/websocket" ) @@ -29,6 +30,8 @@ type Options struct { Address string // Helper flag to help users connect to the default local address Local bool + // set a timeout + Timeout time.Duration } // Request is the request of the generic `api-client` call @@ -92,6 +95,11 @@ func (client *Client) SetToken(t string) { client.options.Token = t } +// SetTimeout sets the http client's timeout +func (client *Client) SetTimeout(d time.Duration) { + client.options.Timeout = d +} + // Call enables you to access any endpoint of any service on Micro func (client *Client) Call(service, endpoint string, request, response interface{}) error { // example curl: curl -XPOST -d '{"service": "go.micro.srv.greeter", "endpoint": "Say.Hello"}' @@ -121,7 +129,11 @@ func (client *Client) Call(service, endpoint string, request, response interface req.Header.Set("Content-Type", "application/json") - httpClient := &http.Client{} + // if user didn't specify Timeout the default is 0 i.e no timeout + httpClient := &http.Client{ + Timeout: client.options.Timeout, + } + resp, err := httpClient.Do(req) if err != nil { return err