Skip to content
This repository has been archived by the owner on Nov 5, 2020. It is now read-only.

Commit

Permalink
Add timeouts for syslog
Browse files Browse the repository at this point in the history
In order to detect dead syslog readers,
use a timout on (re-)connect and write.
  • Loading branch information
Ingo Oeser committed Nov 5, 2013
1 parent 1964c0d commit 672a225
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion syslog/syslog.go
Expand Up @@ -33,6 +33,9 @@ type Priority int
const severityMask = 0x07
const facilityMask = 0xf8

var writeTimeout = 1 * time.Second
var connectTimeout = 1 * time.Second

const (
// Severity.

Expand Down Expand Up @@ -100,6 +103,7 @@ type Writer struct {
type serverConn interface {
writeString(p Priority, hostname, tag, s, nl string) error
close() error
setWriteDeadline(t time.Time) error
}

type netConn struct {
Expand Down Expand Up @@ -273,7 +277,11 @@ func (w *Writer) write(p Priority, msg string) (int, error) {
nl = "\n"
}

err := w.conn.writeString(p, w.hostname, w.tag, msg, nl)
err := w.conn.setWriteDeadline(time.Now().Add(writeTimeout))
if err != nil {
return 0, err
}
err = w.conn.writeString(p, w.hostname, w.tag, msg, nl)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -305,6 +313,10 @@ func (n *netConn) close() error {
return n.conn.Close()
}

func (n *netConn) setWriteDeadline(t time.Time) error {
return n.conn.SetWriteDeadline(t)
}

// NewLogger creates a log.Logger whose output is written to
// the system log service with the specified priority. The logFlag
// argument is the flag set passed through to log.New to create
Expand Down
2 changes: 1 addition & 1 deletion syslog/syslog_unix.go
Expand Up @@ -19,7 +19,7 @@ func unixSyslog() (conn serverConn, err error) {
logPaths := []string{"/dev/log", "/var/run/syslog"}
for _, network := range logTypes {
for _, path := range logPaths {
conn, err := net.Dial(network, path)
conn, err := net.DialTimeout(network, path, connectTimeout)
if err != nil {
continue
} else {
Expand Down

0 comments on commit 672a225

Please sign in to comment.