How Go's net.DialContext() stops things when the context is cancelled
https://utcc.utoronto.ca/~cks/space/blog/programming/GoDialCancellationHow [utcc.utoronto.ca]
2020-01-17 02:04
When I started looking into the relevant standard library code I expected to find that things like net.Dialer.DialContext() had special hooks into the runtime’s network poller (netpoller) to do this. This turns out to not be the case; instead dialing uses an interesting and elegant approach that’s open to everyone doing network IO.
In order to abort an outstanding dial operation if the context is cancelled, the net package simply sets an expired (write) deadline.