Skip to content

Commit

Permalink
Improvements to how link shutdowns are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
neilalexander committed Nov 23, 2024
1 parent 2454970 commit d3b4de4
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/core/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func (l *links) _updateAverages() {

func (l *links) shutdown() {
phony.Block(l, func() {
for listener := range l._listeners {
_ = listener.listener.Close()
for _, cancel := range l._listeners {
cancel()
}
for _, link := range l._links {
if link._conn != nil {
Expand Down Expand Up @@ -429,7 +429,7 @@ func (l *links) remove(u *url.URL, sintf string, _ linkType) error {
}

func (l *links) listen(u *url.URL, sintf string, local bool) (*Listener, error) {
ctx, cancel := context.WithCancel(l.core.ctx)
ctx, ctxcancel := context.WithCancel(l.core.ctx)
var protocol linkProtocol
switch strings.ToLower(u.Scheme) {
case "tcp":
Expand All @@ -445,21 +445,25 @@ func (l *links) listen(u *url.URL, sintf string, local bool) (*Listener, error)
case "wss":
protocol = l.wss
default:
cancel()
ctxcancel()
return nil, ErrLinkUnrecognisedSchema
}
listener, err := protocol.listen(ctx, u, sintf)
if err != nil {
cancel()
ctxcancel()
return nil, err
}
addr := listener.Addr()
cancel := func() {
ctxcancel()
if err := listener.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
l.core.log.Warnf("Error closing %s listener %s: %s", strings.ToUpper(u.Scheme), addr, err)
}
}
li := &Listener{
listener: listener,
ctx: ctx,
Cancel: func() {
cancel()
_ = listener.Close()
},
Cancel: cancel,
}

var options linkOptions
Expand All @@ -482,10 +486,11 @@ func (l *links) listen(u *url.URL, sintf string, local bool) (*Listener, error)
})

go func() {
l.core.log.Infof("%s listener started on %s", strings.ToUpper(u.Scheme), li.listener.Addr())
defer l.core.log.Infof("%s listener stopped on %s", strings.ToUpper(u.Scheme), li.listener.Addr())
l.core.log.Infof("%s listener started on %s", strings.ToUpper(u.Scheme), addr)
defer phony.Block(l, func() {
cancel()
delete(l._listeners, li)
l.core.log.Infof("%s listener stopped on %s", strings.ToUpper(u.Scheme), addr)
})
for {
conn, err := li.listener.Accept()
Expand Down

0 comments on commit d3b4de4

Please sign in to comment.