Add thread_local_middleware, document thread-locals