Skip to content

Commit

Permalink
Handle connection reset on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
becheran committed Mar 7, 2022
1 parent 3b1bcc7 commit 4e9614f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
7 changes: 3 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
)

Expand Down Expand Up @@ -1439,8 +1438,8 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)
err = bw.Flush()
}
c.releaseWriter(bw)
isECONNRESET := errors.Is(err, syscall.ECONNRESET)
if err != nil && !isECONNRESET {
isConnRST := isConnectionReset(err)
if err != nil && !isConnRST {
c.closeConn(cc)
return true, err
}
Expand Down Expand Up @@ -1472,7 +1471,7 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)
return retry, err
}

if resetConnection || req.ConnectionClose() || resp.ConnectionClose() || isECONNRESET {
if resetConnection || req.ConnectionClose() || resp.ConnectionClose() || isConnRST {
c.closeConn(cc)
} else {
c.releaseConn(cc)
Expand Down
4 changes: 1 addition & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"crypto/tls"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -17,7 +16,6 @@ import (
"strings"
"sync"
"sync/atomic"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -2955,7 +2953,7 @@ func TestRstConnClosedWithoutResponse(t *testing.T) {

err = client.Do(req, resp)

if !errors.Is(err, syscall.ECONNRESET) {
if !isConnectionReset(err) {
t.Fatalf("Expected connection reset error")
}
}
5 changes: 2 additions & 3 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net"
"os"
"sync"
"syscall"
"time"

"github.com/valyala/bytebufferpool"
Expand Down Expand Up @@ -1292,7 +1291,7 @@ func (resp *Response) ReadLimitBody(r *bufio.Reader, maxBodySize int) error {
if !resp.mustSkipBody() {
err = resp.ReadBody(r, maxBodySize)
if err != nil {
if errors.Is(err, syscall.ECONNRESET) {
if isConnectionReset(err) {
return nil
}
return err
Expand All @@ -1302,7 +1301,7 @@ func (resp *Response) ReadLimitBody(r *bufio.Reader, maxBodySize int) error {
if resp.Header.ContentLength() == -1 {
err = resp.Header.ReadTrailer(r)
if err != nil && err != io.EOF {
if errors.Is(err, syscall.ECONNRESET) {
if isConnectionReset(err) {
return nil
}
return err
Expand Down
13 changes: 13 additions & 0 deletions tcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build !windows
// +build !windows

package fasthttp

import (
"errors"
"syscall"
)

func isConnectionReset(err error) bool {
return errors.Is(err, syscall.ECONNRESET)
}
13 changes: 13 additions & 0 deletions tcp_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build windows
// +build windows

package fasthttp

import (
"errors"
"syscall"
)

func isConnectionReset(err error) bool {
return errors.Is(err, syscall.WSAECONNRESET)
}

0 comments on commit 4e9614f

Please sign in to comment.