Skip to content

Commit

Permalink
adding timeout parameter to socket writer
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott White committed Jul 22, 2013
1 parent d82a9eb commit 40cec3f
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions socket_writer.go
Expand Up @@ -14,22 +14,32 @@ type SocketWriter struct {
addr string
connSync *sync.RWMutex
restartOnce *sync.Once
Timeout time.Duration
}

func NewSocketWriter(network, addr string) (*SocketWriter, error) {
conn, err := net.Dial(network, addr)
if err != nil {
return nil, err
}
return &SocketWriter{conn, network, addr, &sync.RWMutex{}, &sync.Once{}}, nil
timeout := 5 * time.Millisecond // logging should be fast
return &SocketWriter{conn, network, addr, &sync.RWMutex{}, &sync.Once{}, timeout}, nil
}

func (sw *SocketWriter) LogWrite(msg string) {
sw.connSync.RLock()
// Starting with go1.1 (currently tested on go1.1.1)
// writing to /dev/log on linux with rsyslog will occasionally
// return EAGAIN. Unfortunately, the go socket code does
// a blocking wait which never returns when it gets an EAGAIN,
// so a timeout is necessary to unblock. I tested and a reconnect
// is not necessary when this happens but I think the code is more
// general this way so I'm leaving the reconnect on all errors.
sw.conn.SetWriteDeadline(time.Now().Add(sw.Timeout))
_, err := sw.conn.Write([]byte(msg))
sw.connSync.RUnlock()
if err != nil {
fmt.Printf("Socket logging error: %v", err)
fmt.Printf("Socket logging error: %v\n", err)
sw.restartOnce.Do(func() {
go sw.reconnect()
})
Expand Down

0 comments on commit 40cec3f

Please sign in to comment.