jolshan commented on code in PR #13391: URL: https://github.com/apache/kafka/pull/13391#discussion_r1161846085
########## core/src/main/scala/kafka/server/KafkaRequestHandler.scala: ########## @@ -35,6 +36,43 @@ trait ApiRequestHandler { def handle(request: RequestChannel.Request, requestLocal: RequestLocal): Unit } +object KafkaRequestHandler { + // Support for scheduling callbacks on a request thread. + private val threadRequestChannel = new ThreadLocal[RequestChannel] + private val currentRequest = new ThreadLocal[RequestChannel.Request] + + // For testing + private var bypassThreadCheck = false + def setBypassThreadCheck(bypassCheck: Boolean): Unit = { + bypassThreadCheck = bypassCheck + } + + def currentRequestOnThread(): RequestChannel.Request = { + currentRequest.get() + } + + /** + * Wrap callback to schedule it on a request thread. + * NOTE: this function must be called on a request thread. + * @param fun Callback function to execute + * @return Wrapped callback that would execute `fun` on a request thread + */ + def wrap[T](fun: T => Unit)(request: RequestChannel.Request): T => Unit = { Review Comment: My original concern was that if we just used the thread local, we would access it when the inner method is called. I guess I can just save a local variable when wrap is called and pass that value into the inner method. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org