Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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

Expand Down
15 changes: 10 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/bcicen/go-haproxy
module github.com/vponomarev/go-haproxy

go 1.16

Expand Down
2 changes: 1 addition & 1 deletion info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down