Skip to content

Commit

Permalink
[xnet/xhttp] fix goroutine leak (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kucherenkovova authored Jun 13, 2024
1 parent 81a8cab commit a9c2e24
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion xnet/xhttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ Extensions to the standard Go library's `net/http` package. `ListenAndServe` alt

### Install
```
go get github.com/kucherenkovova/gopypaste/xnet/[email protected].1
go get github.com/kucherenkovova/gopypaste/xnet/[email protected].2
```
10 changes: 8 additions & 2 deletions xnet/xhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ func ListenAndServeContext(ctx context.Context, addr string, handler http.Handle
defer cancel()

server := &http.Server{Addr: addr, Handler: handler}
done := make(chan struct{})

go func() {
<-ctx.Done()
_ = server.Shutdown(context.Background())
select {
case <-ctx.Done():
_ = server.Shutdown(context.Background())
case <-done:
return
}
}()

if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
close(done)
return err
}

Expand Down

0 comments on commit a9c2e24

Please sign in to comment.