Skip to content

Commit

Permalink
Update to open and close connection as needed since connection reuse …
Browse files Browse the repository at this point in the history
…has issues

fix #2, fix #3
  • Loading branch information
jonathan-robertson committed Jan 26, 2017
1 parent a96c328 commit a877fa6
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions logentrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ type Hook struct {
token string
formatter *logrus.JSONFormatter
tlsConfig *tls.Config
conn net.Conn
}

const (
host = "data.logentries.com"
port = 443
retryCount = 3
host = "data.logentries.com"
port = 443
)

// New creates and returns a new hook for use in Logrus
Expand All @@ -40,7 +38,11 @@ func New(token, timestampFormat string, priority logrus.Level, config *tls.Confi
},
}

err = hook.dial()
// Test connection
if conn, err := hook.dial(); err == nil {
conn.Close()
}

}
return
}
Expand All @@ -65,31 +67,21 @@ func (hook *Hook) Fire(entry *logrus.Entry) (err error) {
return
}

func (hook *Hook) dial() (err error) {
hook.conn, err = tls.Dial("tcp", fmt.Sprintf("%s:%d", host, port), hook.tlsConfig)
return
// dial establishes a new connection which caller responsible for closing
func (hook Hook) dial() (net.Conn, error) {
return tls.Dial("tcp", fmt.Sprintf("%s:%d", host, port), hook.tlsConfig)
}

// write dials a connection and writes token and line in bytes to connection
func (hook *Hook) write(line string) (err error) {
data := []byte(hook.token + line)

_, err = hook.conn.Write(data) // initial write attempt
for i := 0; err != nil && i < retryCount; i++ {
fmt.Fprintf(os.Stderr, "WARNING: Trouble writing to conn; retrying %d of %d | err: %v\n", i, retryCount, err)
hook.conn.Close() // close connection and ignore error
if dialErr := hook.dial(); dialErr != nil { // Problem with write, so dial new connection and retry if possible
fmt.Fprintf(os.Stderr, "ERROR: Unable to dial new connection | dialErr: %v\n", dialErr)
return err
}

if _, err = hook.conn.Write(data); err == nil { // retry write
fmt.Fprintln(os.Stderr, "RECOVERED: Connection redialed and wrote successfully")
}
if conn, err := hook.dial(); err == nil {
defer conn.Close()
_, err = conn.Write([]byte(hook.token + line))
}

return
}

// format serializes the entry as JSON regardless of logentries's overall formatting
func (hook Hook) format(entry *logrus.Entry) (string, error) {
serialized, err := hook.formatter.Format(entry)
if err != nil {
Expand Down

0 comments on commit a877fa6

Please sign in to comment.