Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ray project Ray Serve RequestContext

From Leeroopedia
Knowledge Sources
Domains Model_Serving, Distributed_Computing
Last Updated 2026-02-13 16:00 GMT

Overview

RequestContext provides thread-local storage for per-request metadata accessible within Ray Serve deployment code during request processing.

Description

RequestContext uses a ThreadLocal variable to store request-scoped context information including route, request ID, application name, and multiplexed model ID. A default empty context is used as the initial value for each thread. Static methods set(), get(), and clean() manage the context lifecycle, ensuring each concurrent request thread has isolated context data without requiring explicit parameter passing.

Usage

Use RequestContext within deployment handler code to access metadata about the current request, such as the route path, request ID for tracing, application name, or multiplexed model ID. The context is automatically set by the Serve framework before invoking the handler and should be cleaned up after processing completes.

Code Reference

Source Location

  • Repository: Ray
  • File: java/serve/src/main/java/io/ray/serve/context/RequestContext.java

Signature

public class RequestContext {

    public static void set(
        String route, String requestId, String appName, String multiplexedModelId)

    public static RequestContext get()

    public static void clean()
}

Import

import io.ray.serve.context.RequestContext;

I/O Contract

Fields

Field Type Description
route String The HTTP route path for the current request
requestId String Unique identifier for the current request
appName String Name of the Ray Serve application handling the request
multiplexedModelId String Model ID when using multiplexed model serving

Static Methods

Method Return Type Description
set(route, requestId, appName, multiplexedModelId) void Sets the thread-local request context; null values fall back to defaults
get() RequestContext Returns the current thread's request context
clean() void Removes the thread-local context, resetting to the default

Instance Methods

Method Return Type Description
getRoute() String Returns the route path
getRequestId() String Returns the request ID
getAppName() String Returns the application name
getMultiplexedModelId() String Returns the multiplexed model ID

Usage Examples

// Inside a deployment handler method
public Object handleRequest(Object request) {
    // Access the current request context
    RequestContext ctx = RequestContext.get();

    String route = ctx.getRoute();
    String requestId = ctx.getRequestId();
    String appName = ctx.getAppName();

    logger.info("Processing request {} on route {} for app {}",
        requestId, route, appName);

    try {
        // Process the request
        return doWork(request);
    } finally {
        // Context is typically cleaned up by the framework
        RequestContext.clean();
    }
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment