Skip to content

Commit

Permalink
http_(proxy,server): remove Close() from API
Browse files Browse the repository at this point in the history
Listeners are created in constructor and closed in finalizer, specified in constructor.
This is the 2nd time they are closed, the first close is issued on Shutdown().
  • Loading branch information
mmatczuk committed Nov 28, 2024
1 parent dbf5077 commit d79700d
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 9 deletions.
1 change: 0 additions & 1 deletion command/pac/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ 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: 0 additions & 2 deletions command/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ 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 @@ -273,7 +272,6 @@ 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: 0 additions & 1 deletion command/test/httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ 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: 4 additions & 2 deletions http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net"
"net/http"
"net/url"
"runtime"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -188,6 +189,7 @@ 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 @@ -570,7 +572,7 @@ func (hp *HTTPProxy) run(ctx context.Context) error {
ctxErr := ctx.Err()

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

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

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

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

Expand Down Expand Up @@ -146,6 +147,7 @@ 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 @@ -258,6 +260,6 @@ func (hs *HTTPServer) Addr() string {
return hs.listener.Addr().String()
}

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

0 comments on commit d79700d

Please sign in to comment.