mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-01-13 03:25:21 +00:00
Improvements to how link shutdowns are handled
This commit is contained in:
parent
2454970e4d
commit
d3b4de46ea
@ -131,8 +131,8 @@ func (l *links) _updateAverages() {
|
|||||||
|
|
||||||
func (l *links) shutdown() {
|
func (l *links) shutdown() {
|
||||||
phony.Block(l, func() {
|
phony.Block(l, func() {
|
||||||
for listener := range l._listeners {
|
for _, cancel := range l._listeners {
|
||||||
_ = listener.listener.Close()
|
cancel()
|
||||||
}
|
}
|
||||||
for _, link := range l._links {
|
for _, link := range l._links {
|
||||||
if link._conn != nil {
|
if link._conn != nil {
|
||||||
@ -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) {
|
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
|
var protocol linkProtocol
|
||||||
switch strings.ToLower(u.Scheme) {
|
switch strings.ToLower(u.Scheme) {
|
||||||
case "tcp":
|
case "tcp":
|
||||||
@ -445,21 +445,25 @@ func (l *links) listen(u *url.URL, sintf string, local bool) (*Listener, error)
|
|||||||
case "wss":
|
case "wss":
|
||||||
protocol = l.wss
|
protocol = l.wss
|
||||||
default:
|
default:
|
||||||
cancel()
|
ctxcancel()
|
||||||
return nil, ErrLinkUnrecognisedSchema
|
return nil, ErrLinkUnrecognisedSchema
|
||||||
}
|
}
|
||||||
listener, err := protocol.listen(ctx, u, sintf)
|
listener, err := protocol.listen(ctx, u, sintf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
ctxcancel()
|
||||||
return nil, err
|
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{
|
li := &Listener{
|
||||||
listener: listener,
|
listener: listener,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
Cancel: func() {
|
Cancel: cancel,
|
||||||
cancel()
|
|
||||||
_ = listener.Close()
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var options linkOptions
|
var options linkOptions
|
||||||
@ -482,10 +486,11 @@ func (l *links) listen(u *url.URL, sintf string, local bool) (*Listener, error)
|
|||||||
})
|
})
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
l.core.log.Infof("%s listener started on %s", strings.ToUpper(u.Scheme), li.listener.Addr())
|
l.core.log.Infof("%s listener started on %s", strings.ToUpper(u.Scheme), addr)
|
||||||
defer l.core.log.Infof("%s listener stopped on %s", strings.ToUpper(u.Scheme), li.listener.Addr())
|
|
||||||
defer phony.Block(l, func() {
|
defer phony.Block(l, func() {
|
||||||
|
cancel()
|
||||||
delete(l._listeners, li)
|
delete(l._listeners, li)
|
||||||
|
l.core.log.Infof("%s listener stopped on %s", strings.ToUpper(u.Scheme), addr)
|
||||||
})
|
})
|
||||||
for {
|
for {
|
||||||
conn, err := li.listener.Accept()
|
conn, err := li.listener.Accept()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user