Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark EOF errors coming from http request as downstream #1192

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion experimental/status/status_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -142,7 +143,8 @@ func IsDownstreamHTTPError(err error) bool {
return IsDownstreamError(err) ||
isConnectionResetOrRefusedError(err) ||
isDNSNotFoundError(err) ||
isTLSCertificateVerificationError(err)
isTLSCertificateVerificationError(err) ||
isHTTPEOFError(err)
}

// InCancelledError returns true if err is context.Canceled or is gRPC status Canceled.
Expand Down Expand Up @@ -202,6 +204,20 @@ func isTLSCertificateVerificationError(err error) bool {
return false
}

// isHTTPEOFError returns true if the error is an EOF error inside of url.Error or net.OpError, indicating the connection was closed prematurely by server
func isHTTPEOFError(err error) bool {
var netErr *net.OpError
if errors.As(err, &netErr) {
return errors.Is(netErr.Err, io.EOF)
}

var urlErr *url.Error
if errors.As(err, &urlErr) {
return errors.Is(urlErr.Err, io.EOF)
}
return false
}

type sourceCtxKey struct{}

// SourceFromContext returns the source stored in the context.
Expand Down
31 changes: 31 additions & 0 deletions experimental/status/status_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"io"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -248,6 +249,36 @@ func TestIsDownstreamHTTPError(t *testing.T) {
err: x509.UnknownAuthorityError{},
expected: true,
},
{
name: "nil error",
err: nil,
expected: false,
},
{
name: "io.EOF error",
err: io.EOF,
expected: false,
},
{
name: "url io.EOF error",
err: &url.Error{Op: "Get", URL: "https://example.com", Err: io.EOF},
expected: true,
},
{
name: "net op io.EOF error",
err: &net.OpError{Err: io.EOF},
expected: true,
},
{
name: "wrapped url io.EOF error",
err: fmt.Errorf("wrapped: %w", &url.Error{Op: "Get", URL: "https://example.com", Err: io.EOF}),
expected: true,
},
{
name: "joined error with io.EOF",
err: errors.Join(io.EOF, &url.Error{Op: "Get", URL: "https://example.com", Err: io.EOF}),
expected: true,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Loading