mirror of
https://github.com/kevin-DL/m3o-go.git
synced 2026-01-11 18:44:26 +00:00
added timeout option
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user