Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-5240

Go: do connectivity check on TSimpleServer

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 0.13.0
    • 0.14.0
    • Go - Compiler, Go - Library
    • None

    Description

      In go's stdlib http package, the http server will cancel the context passed into the request handler when the client closed the connection (link):

      For incoming server requests, the context is canceled when the client's connection closes, the request is canceled (with HTTP/2), or when the ServeHTTP method returns.

      This is useful for the handler implementation to be able to abandon the current request to free up resources that's no longer needed.

      Now with socket connectivity check added (THRIFT-5214), it's doable in thrift go library as well.

      Looking at the current code, it needs to be done in compiler generated TProcessorFunction implementations for each endpoints. What that function does now is basically:

      1. read the request
      2. call the handler with the read request
      3. write the response handler returned

      so in order to do that, we can just do the following between step 1 and 2:

      1. create a sub-context with cancel
      2. start a goroutine between step 1 and 2, with a ticker (the interval should be a fixed, small value, maybe 1ms?), do connectivity check on every tick, cancel the context if the connectivity check failed
      3. pass that sub-context to the handler, and also cancel the goroutine after the handler returns

      one thing to note is that it's not safe to read from the same TTransport concurrently, but since we don't do any reading after step 1, the goroutine doing connectivity check would be the only one reading, so a lock is not needed.

      the only downside of this approach is that it will create an extra goroutine for each request in processing. but because of goroutines' are lightweight in nature, this should be fine.

      Attachments

        Activity

          People

            fishywang Yuxuan Wang
            fishywang Yuxuan Wang
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 3h 40m
                3h 40m