Description
Use Case
While writing a tor transport I noticed that some transports runs some node beyond the listener (mainly overlay networks).
For example my tor transport runs a tor node and I need to close it at one point. I can't rely on the listeners to close it because it's not because a node stop listening on an address it will not then dial someone.
Additions
I propose to add this in go-libp2p-core/transport
:
type ClosableTransport interface {
Transport
// Close is called when the libp2p node doesn't need the transport anymore (the is node closing).
// After that the transport can safely assume no more Listen() or Dial() are gonna be done.
// This is usefull for transport running overlay network.
Close() error
}
Then in host.Close()
the host would use reflection to terminate the transport when closing (after closing all listeners) :
// Not actual working code, just an idea
var wg sync.WaitGroup
for _, v := range h.transports {
ct, ok := v.(tpt.ClosableTransport)
if ok {
wg.Add(1)
go func (t tpt.ClosableTransport){ // Needed because range is not thread safe
defer wg.Done()
t.Close()
}(ct)
}
}
wg.Wait()
(Also I can't rely on the garbage collector to do this work because I have some CGO code (or a system fork) and the garbage collector doesn't deal with that.)
In the Future
If in the future a method to unhook transport from a libp2p is added the transport will be closable that way.