Skip to content

Commit

Permalink
better flush and connection closed handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Jul 22, 2016
1 parent 16e31e3 commit 478a734
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions command/agent/fs_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"math"
"net"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -312,9 +313,9 @@ func (s *StreamFramer) run() {
var err error
defer func() {
close(s.exitCh)
close(s.outbound)

s.l.Lock()
close(s.outbound)
s.Err = err
s.running = false
s.l.Unlock()
Expand Down Expand Up @@ -342,7 +343,7 @@ func (s *StreamFramer) run() {
select {
case s.outbound <- s.f:
s.f = nil
default:
case <-s.exitCh:
}
s.l.Unlock()
case <-s.heartbeat.C:
Expand All @@ -369,13 +370,17 @@ OUTER:
}

// Flush any existing frames
select {
case o := <-s.outbound:
// Send the frame and then clear the current working frame
if err = s.enc.Encode(o); err != nil {
return
FLUSH:
for {
select {
case o := <-s.outbound:
// Send the frame and then clear the current working frame
if err = s.enc.Encode(o); err != nil {
return
}
default:
break FLUSH
}
default:
}

s.l.Lock()
Expand Down Expand Up @@ -602,10 +607,20 @@ OUTER:
if err := framer.Send(path, lastEvent, data[:n], offset); err != nil {

// Check if the connection has been closed
if err == io.ErrClosedPipe || strings.Contains(err.Error(), syscall.EPIPE.Error()) {
if err == io.ErrClosedPipe {
// The pipe check is for tests
return syscall.EPIPE
}

operr, ok := err.(*net.OpError)
if ok {
// The connection was closed by our peer
e := operr.Err.Error()
if strings.Contains(e, syscall.EPIPE.Error()) || strings.Contains(e, syscall.ECONNRESET.Error()) {
return syscall.EPIPE
}
}

return err
}
}
Expand Down Expand Up @@ -813,7 +828,7 @@ func (s *HTTPServer) logs(follow bool, offset int64,
}

// Check if the connection was closed
if strings.Contains(err.Error(), syscall.EPIPE.Error()) {
if err == syscall.EPIPE {
return nil
}

Expand Down

0 comments on commit 478a734

Please sign in to comment.