Handler implementation

Use this section as a guide for developing new rpc methods inside Traffic Server and how to expose them through the JSONRPC 2.0 endpoint. Before we start, it is worth mentioning some of the architecture of the current implementation. The whole RPC mechanism is divided in few components.

Json RPC manager

This class is the entrance point for both server calls and registered functions.


Dispatcher class

  • Class that keeps track of all the registered methods and notifications that can be invoked by the RPC server. This class holds two hash tables containing methods and notifications which use the method name as a key.

  • This class internally consumes RPCRequestInfo objects and performs the invocation of the respective calls.

  • This class handlers the responses from the registered callbacks and it fills the appropriated RPCResponseInfo which then is passed back to the JsonRPCManager class.

JsonRPCManager class

  • Singleton class that handles the JSONRPC handler registration and JSONRPC handler invocation.

  • This class is the main entrance point for the RPC server through the handle_call(std::string_view req) function.

  • This class is the main entrance point for the handler to be able to register in the RPC logic. add_notification_handler and remove_notification_handler.

Implementing new handlers

There a a few basic concepts that needs to be known before implementing a new handler, this is an easy process and the complexity depends on the nature of the handler that you want to implement. Dealing with incoming and outgoing parameters is up to the developer, we will touch on some ways to deal with this through this guide.


As requirement from the JsonRPCManager in order to be able to register inside the RPC management a function should implement the following signature:


ts::Rv<YAML::Node> your_rpc_handler_function_name(std::string_view const &id, YAML::Node const &params);


void your_rpc_handler_function_name(YAML::Node const &params);
  • Incoming method request’s id will be passed to the handler, this is read only value as the server is expected to respond with the same value.

  • YAML::Node params is expected to be a Sequence or a Map, as per protocol this cannot be a single value, so do not expect things like: param=123 or param=SomeString.

  • The params can be empty and contains no data at all.

It is important to know that method handlers are expected to respond to the requests, while notifications` will not respond with any data nor error. You can find more information in JSONRPC 2.0 Protocol or directly in the protocol specs JSONRPC.


If there is no explicit response from the method, the protocol implementation will respond with success_response unless an error was specified.

Registration and Handling


Handler registration should be done by using the JsonRPCManager singleton object. Note that there are a set of convenient helper functions that can be used to achieve registration through the singleton object.

namespace rpc {
    // this set of functions will call the singleton object and perform the same as by using the singleton directly.
// Handler implementation
my_handler_impl(std::string_view const &id, YAML::Node const &params)
    using namespace rpc::handlers::errors;
    return make_errata(Codes::SERVER, "Something happened in the server");

The actual registration:

#include "rpc/jsonrpc/JsonRPC.h"
rpc::add_method_handler("my_handler_impl", &my_handler_impl);

This API also accepts a RPCRegistryInfo pointer which will provide a context data for the particular handler, for instance it will display the provider’s name when the service descriptor gets called. There is a global object created for this purpose which can be used As a generic registry context object, core_ats_rpc_service_provider_handle is defined in the JsonRPC.h header. Please check get_service_descriptor for more information.

Notification example:

As mentioned before, notifications do not need to respond, as they are “fire and forget” calls, no id will be provided as part of the api.

my_notification_handler(YAML::Node const &params)
    // do something
    // all errors will be ignored by the server.

Registration for notifications uses a different API:

#include "rpc/jsonrpc/JsonRPC.h"
rpc::add_notification_handler("my_notification_handler", &my_notification_handler);

The registration API allows the client to pass a RPCRegistryInfo which provides extra information for a particular handler. Non plugins handlers should use the default provided Registry Info, located in the JsonRPC.h header file.

Error Handling


There are several ways to deal with internal handler errors. Errors are expected to be sent back to the client if the API was expressed that way and if the request was a method. We have defined some generic errors that can be used to respond depending on the nature of the registered handler, please check API Handler error codes for more info.

We recommend some ways to deal with this:

  1. Using the Errata from ts::Rv<YAML::Node>

This can be set in case you would like to let the server to respond with an JSONRPC 2.0 error, ExecutionError will be used to catch all the errors that are fired from within the function call, either by setting the proper errata or by throwing an exception. Please check the rpc-error-code and in particular ExecutionError = 9. Also check API Handler error codes


Errors have preference over any other response, so if you set both, the errata and the YAML::Node response, then the former will be set in the response.

  1. Defining a custom error object and make it part of the response object.

  • This is up to the developer and the errors can be part of the response YAML::Node.

  • The JSONRPC Dispatcher will read that there is no error returned from the call and use the result to build the response. If this is what you are willing to respond to, then make sure that the error is not set in the ts::Rv.

  1. Exception.

    As long as the exception inherit from std::exception it will be handled by the jsonrpc manager, this error will be handled as like using the Errata object, this kind of errors will be part of the ExecutionError.

    The following example will generate this JSON response:

    foo(std::string_view const &id, YAML::Node const &params)


  • Respond with an error, no result field will be set in the response.

    respond_with_an_error(std::string_view const &id, YAML::Node const &params)
        using namespace rpc::handlers::errors;
        return make_errata(Codes::SERVER, "Something happened in the server");

    Server’s response.

            "message":"Error during execution",
                    "message":"Something happened in the server"


    make_errata hides some internal details when creating an errata.

  • Response with custom handler error. In this case, make sure that the API definition and documentation reflects this as so far we do not have json schemas to enforce any of this on the client side.

      respond_with_my_own_error(std::string_view const &id, YAML::Node const &params)
          YAML::Node resp;
          resp["HandlerErrorDescription"] = "I can set up my own error in the result field.";
          return resp;
    The "error" is part of the ``result``, in this case this could be used as any other field, the example would be the same.
          "HandlerErrorDescription":"I can set up my own error in the result field."

We have selected the ts::Rv<YAML::Node> as a message interface as this can hold the actual response/error.

Unit test

All new methods exposed through the RPC server can be tested using the jsonrpc autest extension.


This extension provides the ability to interact with the JSONRPC interface by using traffic_ctl as a client. As a helper for all new autest that needs to write and read jsonrpc message, there is also a new module jsonrpc.py which provides a nice and easy interface to write methods and notifications. This extension also provides the facility to write custom jsonrpc validations. Please check some of the following examples:

  1. Write custom jsonrpc message

    The newly added jsonrpc method was named 'foo_bar' and is expected to accept a list of fqdn.
    tr = Test.AddTestRun("Test JSONRPC foo_bar()")
    The following call to the Request object will generate this:
        "id": "850d32a8-d5a7-11eb-bebc-fa163e6d2ec5",
        "jsonrpc": "2.0",
        "method": "foo_bar",
        "params": {
            "fqdn": ["yahoo.com", "trafficserver.org"]
    req = Request.foo_bar(fqdn=["yahoo.com", "trafficserver.org"])
    tr.AddJsonRPCClientRequest(ts, req)
  2. Custom response validation

    tr = Test.AddTestRun("Test update_host_status")
    Params = [
        {'name': 'yahoo', 'status': 'up'}
    tr.AddJsonRPCClientRequest(ts, Request.update_host_status(hosts=Params))
    def check_no_error_on_response(resp: Response):
        # we only check if it's an error.
        if resp.is_error():
            return (False, resp.error_as_str())
        return (True, "All good")
    tr.Processes.Default.Streams.stdout = Testers.CustomJSONRPCResponse(check_no_error_on_response)


A tester class that will let you write your own response validation by dealing with the jsonrpc.Response object, please check the CustomJSONRPCResponse tester for more information.


This function will generate a json response as an output, internally it ses traffic_ctl file --format json as client. The output can be used and compared with a gold file. This also provides schema validation for the entire JSONRPC protocol as well as the param field against a specific schema file. You can specify schema_file_name with a valid json schema file to validate the entire JSONRPC 2.0 request(except the content of the params field). You can also set params_field_schema_file_name with a valid json schema file to validate only the params field.


The following, beside sending the request, will perform a JSONRPC 2.0 schema validation as well as the param field.

schema_file_name = 'schemas/jsonrpc20_request_schema.json'
params_schema_file_name = 'schemas/join_strings_request_params_schema.json'


A tester class to perform a schema validation of the entire JSONRPC 2.0 request as well as the result param. You can specify schema_file_name with a valid json schema file to validate the entire JSONRPC 2.0 response(except the content of the result field). Also you can set result_field_schema_file_name with a valid json schema file to validate only the result field.


The following will add a Tester for a JSONRPC 2.0 schema validation as well as the result field.

tr = Test.AddTestRun('test valid response schema')
schema_file_name = 'schemas/jsonrpc20_response_schema.json'
result_schema_file_name = 'schemas/join_strings_request_result_schema.json'

# Add a tester.
tr.Processes.Default.Streams.stdout = Testers.JSONRPCResponseSchemaValidator(

Important Notes

  • You can refer to YAML for more info in how code/decode yaml content.

  • Remember to update API if you are adding a new handler.

  • If a new handler needs to be exposed through traffic_ctl please refer to traffic_ctl for a general idea and to Traffic Control Development Guide for how to implement a new command.

  • To interact directly with the JSONRPC 2.0 node, please check JSONRPC Endpoint

Configuration JSONRPC 2.0 Protocol Traffic Control Development Guide