• Kirill Smelkov's avatar
    xnet: Adjust Networker.Listen to return listener that can handle cancellation in Accept · 3354b401
    Kirill Smelkov authored
    We already handle cancellation in Dial, but Accepting was out of luck
    until now. This makes it more difficult for clients to implement and
    wrap acceptors where they need to handle cancellations. This also makes
    it possible for a test or program to get stuck in Accept loop if it is
    not careful enough to manually handle ctx cancel around Accept calls.
    
    -> Fix it in one place - here, in xnet - so that users are offloaded
    from all this and can just call Accept(ctx) and rely on underlying
    implementation to handle ctx cancel.
    
    This patch:
    
    - introduces xnet.Listener interface, which is like net.Listener, but
      Accept goes with ctx argument.
    - changes Networker.Listen signature to return xnet.Listener instead of
      net.Listener. While we are here - changing it - also add ctx argument
      to Listen call itself.
    - Adds listenerCtx - which, given net.Listener, provides xnet.Listener
      by wrapping some logic around original.
    - Adapts NetPlain, NetTLS and NetTrace to provide updated interface.
    
    We'll fix up everything in other packages to match/use updated interface
    in the next patch.
    3354b401
trace.go 4.14 KB