mirror of
https://github.com/kevin-DL/m3o-go.git
synced 2026-01-23 07:01:35 +00:00
added timeout option
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
@@ -29,6 +30,8 @@ type Options struct {
|
|||||||
Address string
|
Address string
|
||||||
// Helper flag to help users connect to the default local address
|
// Helper flag to help users connect to the default local address
|
||||||
Local bool
|
Local bool
|
||||||
|
// set a timeout
|
||||||
|
Timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request is the request of the generic `api-client` call
|
// Request is the request of the generic `api-client` call
|
||||||
@@ -92,6 +95,11 @@ func (client *Client) SetToken(t string) {
|
|||||||
client.options.Token = t
|
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
|
// Call enables you to access any endpoint of any service on Micro
|
||||||
func (client *Client) Call(service, endpoint string, request, response interface{}) error {
|
func (client *Client) Call(service, endpoint string, request, response interface{}) error {
|
||||||
// example curl: curl -XPOST -d '{"service": "go.micro.srv.greeter", "endpoint": "Say.Hello"}'
|
// 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")
|
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)
|
resp, err := httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user