diff --git a/client.go b/client.go index db78983..7821cfc 100644 --- a/client.go +++ b/client.go @@ -17,9 +17,10 @@ const ( // HAProxyClient is the main structure of the library. type HAProxyClient struct { - Addr string - Timeout int - conn net.Conn + Addr string + Timeout int // Timeout for connect [ default = 30 sec ] + TimeoutOp int // Timeout for operation [ default = Timeout ] + conn net.Conn } // RunCommand is the entrypoint to the client. Sends an arbitray command string to HAProxy. @@ -32,6 +33,10 @@ func (h *HAProxyClient) RunCommand(cmd string) (*bytes.Buffer, error) { result := bytes.NewBuffer(nil) + err = h.conn.SetDeadline(time.Now().Add(time.Duration(h.TimeoutOp) * time.Second)) + if err != nil { + return nil, err + } _, err = h.conn.Write([]byte(cmd + "\n")) if err != nil { return nil, err @@ -53,6 +58,9 @@ func (h *HAProxyClient) dial() (err error) { if h.Timeout == 0 { h.Timeout = 30 } + if h.TimeoutOp == 0 { + h.TimeoutOp = h.Timeout + } timeout := time.Duration(h.Timeout) * time.Second diff --git a/client_test.go b/client_test.go index eb8f6e9..e8286b5 100644 --- a/client_test.go +++ b/client_test.go @@ -2,13 +2,18 @@ package haproxy_test import ( "fmt" + "github.com/vponomarev/go-haproxy" +) - "github.com/bcicen/go-haproxy" +const ( + HAPROXY_SOCKET_ADDR = "unix:///var/run/haproxy.sock" ) func ExampleHAProxyClient_Stats() { client := &haproxy.HAProxyClient{ - Addr: "unix:///var/run/haproxy.sock", + Addr: HAPROXY_SOCKET_ADDR, + Timeout: 30, + TimeoutOp: 5, } stats, err := client.Stats() if err != nil { @@ -27,7 +32,7 @@ func ExampleHAProxyClient_Stats() { func ExampleHAProxyClient_Info() { client := &haproxy.HAProxyClient{ - Addr: "unix:///var/run/haproxy.sock", + Addr: HAPROXY_SOCKET_ADDR, } info, err := client.Info() if err != nil { @@ -36,12 +41,12 @@ func ExampleHAProxyClient_Info() { } fmt.Printf("%s version %s\n", info.Name, info.Version) // Output: - //HAProxy version 1.6.3 + //HAProxy version 2.8.1 } func ExampleHAProxyClient_RunCommand() { client := &haproxy.HAProxyClient{ - Addr: "unix:///var/run/haproxy.sock", + Addr: HAPROXY_SOCKET_ADDR, } result, err := client.RunCommand("show info") if err != nil { diff --git a/go.mod b/go.mod index 3330ba5..e58855c 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/bcicen/go-haproxy +module github.com/vponomarev/go-haproxy go 1.16 diff --git a/info.go b/info.go index 47cc842..a63bd3a 100644 --- a/info.go +++ b/info.go @@ -3,7 +3,7 @@ package haproxy import ( "fmt" - "github.com/bcicen/go-haproxy/kvcodec" + "github.com/vponomarev/go-haproxy/kvcodec" ) // Response from HAProxy "show info" command.