Skip to content

Commit

Permalink
don't quit listener after a temporary accept error (fclairamb#211)
Browse files Browse the repository at this point in the history
* don't quit listener after a temporary accept error

Fixes fclairamb#210

* fix lint issues

Co-authored-by: Florent Clairambault <florent@clairambault.fr>
  • Loading branch information
drakkan and fclairamb authored Dec 24, 2020
1 parent 6026640 commit 87b8b93
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
22 changes: 22 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net"
"time"

"github.com/fclairamb/ftpserverlib/log"
)
Expand Down Expand Up @@ -177,6 +178,8 @@ func (server *FtpServer) Listen() error {

// Serve accepts and processes any new incoming client
func (server *FtpServer) Serve() error {
var tempDelay time.Duration // how long to sleep on accept failure

for {
connection, err := server.listener.Accept()

Expand All @@ -190,6 +193,25 @@ func (server *FtpServer) Serve() error {
}
}

if ne, ok := err.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}

if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}

server.Logger.Warn(
"accept error", err,
"retry delay", tempDelay)
time.Sleep(tempDelay)

continue
}

server.Logger.Error("Listener accept error", "err", err)

return err
Expand Down
71 changes: 71 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,82 @@
package ftpserver

import (
"errors"
"net"
"testing"

"github.com/stretchr/testify/require"

"github.com/fclairamb/ftpserverlib/log"
)

var errListenerAccept = errors.New("error accepting a connection")

type fakeNetError struct {
error
count int
}

func (e *fakeNetError) Timeout() bool {
return false
}

func (e *fakeNetError) Temporary() bool {
e.count++

return e.count < 10
}

func (e *fakeNetError) Error() string {
return e.error.Error()
}

type fakeListener struct {
server net.Conn
client net.Conn
err error
}

func (l *fakeListener) Accept() (net.Conn, error) {
return l.client, l.err
}

func (l *fakeListener) Close() error {
errClient := l.client.Close()
errServer := l.server.Close()

if errServer != nil {
return errServer
}

return errClient
}

func (l *fakeListener) Addr() net.Addr {
return l.server.LocalAddr()
}

func newFakeListener(err error) net.Listener {
server, client := net.Pipe()

return &fakeListener{
server: server,
client: client,
err: err,
}
}

func TestListernerAcceptErrors(t *testing.T) {
errNetFake := &fakeNetError{error: errListenerAccept}

server := FtpServer{
listener: newFakeListener(errNetFake),
Logger: log.Nothing(),
}
err := server.Serve()
require.EqualError(t, err, errListenerAccept.Error())
}

func TestPortCommandFormatOK(t *testing.T) {
net, err := parsePORTAddr("127,0,0,1,239,163")
require.NoError(t, err, "Problem parsing")
Expand Down

0 comments on commit 87b8b93

Please sign in to comment.