Skip to content

Commit

Permalink
Revert "http_(proxy,server): remove Close()"
Browse files Browse the repository at this point in the history
This reverts commit 05a1e14.

See discussion #967 (comment)
  • Loading branch information
mmatczuk committed Nov 28, 2024
1 parent 05a1e14 commit 0686629
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions command/pac/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (c *command) runE(cmd *cobra.Command, _ []string) (cmdErr error) {
if err != nil {
return err
}
defer s.Close()

return runctx.NewGroup(s.Run).Run()
}
Expand Down
2 changes: 2 additions & 0 deletions command/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func (c *command) runE(cmd *cobra.Command, _ []string) (cmdErr error) {
if err != nil {
return err
}
defer p.Close()
g.Add(p.Run)

if ca := p.MITMCACert(); ca != nil {
Expand Down Expand Up @@ -272,6 +273,7 @@ func (c *command) runE(cmd *cobra.Command, _ []string) (cmdErr error) {
if err != nil {
return err
}
defer a.Close()
g.Add(a.Run)
}
}
Expand Down
1 change: 1 addition & 0 deletions command/test/httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (c *command) runE(cmd *cobra.Command, _ []string) (cmdErr error) {
if err != nil {
return err
}
defer s.Close()
g.Add(s.Run)

g.Add(func(ctx context.Context) error {
Expand Down
6 changes: 2 additions & 4 deletions http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"net"
"net/http"
"net/url"
"runtime"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -189,7 +188,6 @@ func NewHTTPProxy(cfg *HTTPProxyConfig, pr PACResolver, cm *CredentialsMatcher,
return nil, err
}
hp.listeners = ll
runtime.SetFinalizer(hp, (*HTTPProxy).closeListeners)

for _, l := range hp.listeners {
hp.log.Infof("PROXY server listen address=%s protocol=%s", l.Addr(), hp.config.Protocol)
Expand Down Expand Up @@ -572,7 +570,7 @@ func (hp *HTTPProxy) run(ctx context.Context) error {
ctxErr := ctx.Err()

// Close listeners first to prevent new connections.
if err := hp.closeListeners(); err != nil {
if err := hp.Close(); err != nil {
hp.log.Debugf("failed to close listeners error=%s", err)
}

Expand Down Expand Up @@ -650,7 +648,7 @@ func (hp *HTTPProxy) Addr() (addrs []string, ok bool) {
return
}

func (hp *HTTPProxy) closeListeners() error {
func (hp *HTTPProxy) Close() error {
var err error
for _, l := range hp.listeners {
if e := l.Close(); e != nil {
Expand Down
2 changes: 2 additions & 0 deletions http_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestAbortIf(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer p.Close()

check := func(t *testing.T, rt http.RoundTripper) {
t.Helper()
Expand Down Expand Up @@ -94,6 +95,7 @@ func TestNopDialer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer p.Close()

req := &http.Request{
Method: http.MethodGet,
Expand Down
4 changes: 1 addition & 3 deletions http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"net"
"net/http"
"net/url"
"runtime"
"sync"
"time"

Expand Down Expand Up @@ -147,7 +146,6 @@ func NewHTTPServer(cfg *HTTPServerConfig, h http.Handler, log log.Logger) (*HTTP
return nil, err
}
hs.listener = l
runtime.SetFinalizer(hs, (*HTTPServer).closeListener)

hs.log.Infof("HTTP server listen address=%s protocol=%s", l.Addr(), hs.config.Protocol)

Expand Down Expand Up @@ -260,6 +258,6 @@ func (hs *HTTPServer) Addr() string {
return hs.listener.Addr().String()
}

func (hs *HTTPServer) closeListener() error {
func (hs *HTTPServer) Close() error {
return hs.listener.Close()
}

0 comments on commit 0686629

Please sign in to comment.