Home

Spreading the context

When the code is executed, asynchronous control flow is built. Sometimes we want to spread some context at the tree. For example, we want to have to access information about a user or tracing by default.

Consider the following situation:
A user had come in our service. We know something about him. For example, we know his id.
Control flow looks like this:

We want to have to access information about the user at every level of asynchronous control flow. But at the same time, we wouldn't need to throw this data explicitly.
For example, we want to say: "UserId is xxx now" in Handle() and get UserId in MethodN() :

Context is more user-friendly wrapper above the AsyncLocal.
The main value of Vostok-Context is integration with other Vostok's libraries. For example, if you use Vostok.Context + ClusterClient , you can automatically spread the context across the network. (Not just in a current asynchronous stream.)

By way of illustration:
A user has come in our service, and we know his id. But calls go through another service unlike the example above. It looks like this:

We want to have access to information on the user at every level of asynchronous control flow in the Service2.

You can spread the context across the network with Vostok.Context + ClusterClient.
The function of the Context is serialization and deserialization of disseminated data. Use model, connecting ClusterClient and Context – Vostok.Clusterclient.Context to send context over the network.