Skip to content

Commit

Permalink
extract function isBrokenConnection()
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Sep 3, 2024
1 parent 1bf0776 commit ef687bc
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,11 @@ type APIServer struct {
consoleConfig *csconfig.ConsoleConfig
}

func recoverFromPanic(c *gin.Context) {
err := recover()
if err == nil {
return
}

// Check for a broken connection, as it is not really a
// condition that warrants a panic stack trace.
brokenPipe := false

func isBrokenConnection(err any) bool {

Check warning on line 49 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L49

Added line #L49 was not covered by tests
if ne, ok := err.(*net.OpError); ok {
if se, ok := ne.Err.(*os.SyscallError); ok {
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
brokenPipe = true
return true

Check warning on line 53 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L53

Added line #L53 was not covered by tests
}
}
}
Expand All @@ -79,11 +70,22 @@ func recoverFromPanic(c *gin.Context) {
errors.Is(strErr, errClosedBody) ||
errors.Is(strErr, errHandlerComplete) ||
errors.Is(strErr, errStreamClosed) {
brokenPipe = true
return true

Check warning on line 73 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L73

Added line #L73 was not covered by tests
}
}

if brokenPipe {
return false

Check warning on line 77 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L77

Added line #L77 was not covered by tests
}

func recoverFromPanic(c *gin.Context) {
err := recover()
if err == nil {
return
}

// Check for a broken connection, as it is not really a
// condition that warrants a panic stack trace.
if isBrokenConnection(err) {

Check warning on line 88 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L88

Added line #L88 was not covered by tests
log.Warningf("client %s disconnected: %s", c.ClientIP(), err)
c.Abort()
} else {
Expand Down

0 comments on commit ef687bc

Please sign in to comment.