Read / write to network with context

The net package has a DialContext, but it doesn’t appear to have corresponding Read and Write functions that take Contexts. What’s the best practice for doing network reads and writes while honoring Context deadlines and cancellation?

The documentation indicates that the context passed into DialContext is solely used for the dialing phase:

Once successfully connected, any expiration of the context will not affect the connection.

For the connection itself, one possible option is to handle the context one layer above the net API and turn timeouts and cancellations into calls to the connection methods that control connection lifetime (Set(Read/Write)Deadline, KeepAlive, Linger, Close).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.