Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Fix stopping TCP piers
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Oct 12, 2023
1 parent d208af1 commit 0af6c91
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
9 changes: 1 addition & 8 deletions ships/http.go
Expand Up @@ -226,12 +226,5 @@ func (pier *HTTPPier) Abolish() {

// Do not close the listener, as it is shared.
// Instead, remove the HTTP handler and the shared server will shutdown itself when needed.

// Default to root path.
path := pier.transport.Path
if path == "" {
path = "/"
}

_ = removeHTTPHandler(pier.transport.Port, path)
_ = removeHTTPHandler(pier.transport.Port, pier.transport.Path)
}
32 changes: 20 additions & 12 deletions ships/tcp.go
Expand Up @@ -19,6 +19,9 @@ type TCPShip struct {
// TCPPier is a pier that uses TCP.
type TCPPier struct {
PierBase

ctx context.Context
cancelCtx context.CancelFunc
}

func init() {
Expand Down Expand Up @@ -78,12 +81,15 @@ func establishTCPPier(transport *hub.Transport, dockingRequests chan Ship) (Pier
}

// Create new pier.
pierCtx, cancelCtx := context.WithCancel(module.Ctx)
pier := &TCPPier{
PierBase: PierBase{
transport: transport,
listeners: listeners,
dockingRequests: dockingRequests,
},
ctx: pierCtx,
cancelCtx: cancelCtx,
}
pier.initBase()

Expand All @@ -98,20 +104,16 @@ func establishTCPPier(transport *hub.Transport, dockingRequests chan Ship) (Pier
return pier, nil
}

func (pier *TCPPier) dockingWorker(ctx context.Context, listener net.Listener) error {
func (pier *TCPPier) dockingWorker(_ context.Context, listener net.Listener) error {
for {
// Block until something happens.
conn, err := listener.Accept()

// Check if we are done.
select {
case <-ctx.Done():
return nil
default:
}

// Check for error.
if err != nil {
// Check for errors.
switch {
case pier.ctx.Err() != nil:
return pier.ctx.Err()
case err != nil:
return err
}

Expand All @@ -130,8 +132,14 @@ func (pier *TCPPier) dockingWorker(ctx context.Context, listener net.Listener) e
// Submit new docking request.
select {
case pier.dockingRequests <- ship:
case <-ctx.Done():
return nil
case <-pier.ctx.Done():
return pier.ctx.Err()
}
}
}

// Abolish closes the underlying listener and cleans up any related resources.
func (pier *TCPPier) Abolish() {
pier.cancelCtx()
pier.PierBase.Abolish()
}

0 comments on commit 0af6c91

Please sign in to comment.