Skip to content

Commit

Permalink
Merge pull request #169 from buger/http-client-bugfixes
Browse files Browse the repository at this point in the history
Improve http-client reliability and improve debug
  • Loading branch information
buger committed Jul 10, 2015
2 parents b38cb45 + acaf935 commit 531bd1d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
4 changes: 4 additions & 0 deletions emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func CopyMulty(src io.Reader, writers ...io.Writer) (err error) {
}
}

if Settings.debug {
Debug("[EMITTER] Sending paylod:", string(payload))
}

if Settings.splitOutput {
// Simple round robin
writers[wIndex].Write(payload)
Expand Down
32 changes: 25 additions & 7 deletions http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/url"
"strings"
"time"
"runtime/debug"
"log"
)

var defaultPorts = map[string]string{
Expand Down Expand Up @@ -89,9 +91,25 @@ func (c *HTTPClient) isAlive() bool {
}

func (c *HTTPClient) Send(data []byte) (response []byte, err error) {
// Don't exit on panic
defer func() {
if r := recover(); r != nil {
Debug("[HTTPClient]", r, string(data))

if _, ok := r.(error); !ok {
log.Println("[HTTPClient] Failed to send request: ", string(data))
log.Println("PANIC: pkg:", r, debug.Stack())
}
}
}()


if c.conn == nil || !c.isAlive() {
Debug("Connecting:", c.baseURL)
c.Connect()
Debug("[HTTPClient] Connecting:", c.baseURL)
if err = c.Connect(); err != nil {
log.Println("[HTTPClient] Connection error:", err)
return
}
}

timeout := time.Now().Add(5 * time.Second)
Expand All @@ -101,26 +119,26 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) {
data = proto.SetHost(data, []byte(c.baseURL), []byte(c.host))

if c.config.Debug {
Debug("Sending:", string(data))
Debug("[HTTPClient] Sending:", string(data))
}

if _, err = c.conn.Write(data); err != nil {
Debug("Write error:", err, c.baseURL)
Debug("[HTTPClient] Write error:", err, c.baseURL)
return
}

c.conn.SetReadDeadline(timeout)
n, err := c.conn.Read(c.respBuf)

if err != nil {
Debug("READ ERRORR!", err, c.conn)
Debug("[HTTPClient] Response read error", err, c.conn)
return
}

payload := c.respBuf[:n]

if c.config.Debug {
Debug("Received:", string(payload))
Debug("[HTTPClient] Received:", string(payload))
}

if c.config.FollowRedirects > 0 && c.redirectsCount < c.config.FollowRedirects {
Expand All @@ -134,7 +152,7 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) {
redirectPayload := []byte("GET " + string(location) + " HTTP/1.1\r\n\r\n")

if c.config.Debug {
Debug("Redirecting to: " + string(location))
Debug("[HTTPClient] Redirecting to: " + string(location))
}

return c.Send(redirectPayload)
Expand Down
8 changes: 5 additions & 3 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

const (
VERSION = "0.9.6"
VERSION = "0.9.7"
)

// Allows to specify multiple flags with same name and collects all values to array
Expand All @@ -25,6 +25,7 @@ func (h *MultiOption) Set(value string) error {

type AppSettings struct {
verbose bool
debug bool
stats bool

splitOutput bool
Expand Down Expand Up @@ -59,7 +60,8 @@ func usage() {
func init() {
flag.Usage = usage

flag.BoolVar(&Settings.verbose, "verbose", false, "Turn on verbose/debug output")
flag.BoolVar(&Settings.verbose, "verbose", false, "Turn on more verbose output")
flag.BoolVar(&Settings.debug, "debug", false, "Turn on debug output, shows all itercepted traffic. Works only when with `verbose` flag")
flag.BoolVar(&Settings.stats, "stats", false, "Turn on queue stats output")

flag.BoolVar(&Settings.splitOutput, "split-output", false, "By default each output gets same traffic. If set to `true` it splits traffic equally among all outputs.")
Expand Down Expand Up @@ -113,7 +115,7 @@ func init() {

func Debug(args ...interface{}) {
if Settings.verbose {
log.Print("[DEBUG] ")
fmt.Print("[DEBUG] ")
log.Println(args...)
}
}

0 comments on commit 531bd1d

Please sign in to comment.