Previous topic

Server applications

Next topic

C++ Client Library

This Page

Deployment wide requests

Sometimes, it’s necessary to run code on all nodes of a server application, then aggregate the results. For instance :

  • Return the total number of running gamesessions, or any realtime aggregated metric for all gamesessions

  • Return a list of parties created by friends, and open for joining.

  • Terminate all game sessions.

  • Run complex processing distributed between several scenes.

  • etc..

Stormancer server applications support deployment wide requests. These Server to server requests are automatically forwarded to each host running the calling application. Only Server applications can send deployment wide calls (External peers - dedicated servers and clients cannot).

In order to enable client peers to perform such an operation, you should create a client to server operation which will in turn perform the deployment wide call.

Sending a deployment wide request:

IHost host = DependencyResolver.Resolve<IHost>(); //an IHost instance can be obtained from IoC. For the sample, we use the Resolve<T>() method to obtain the instance. In real case, constructor injection should be preferred.

string args = "hello";
//Calls the "myOperation" deployment wide operation and wait for responses.
IAsyncEnumerable<Result<string>> results = await host.Call<string,string>("helloworldOperation",args);

//The method returns an IAsyncEnumerable instance that streams results as soon as they are available.
await foreach(var result in results)
{
    if(result.Success)
    {
        //Each entry represents the response of an host running the current application deployment.
        // Remark: The host sending the request receives it too.
        logger.Log(LogLevel.Info,"deploymentCall",$"Received : {result.Value} from {result.HostId}",new {});
    }
    else
    {
                    logger.Log(LogLevel.Error,"deploymentCall",$"Received error : {result.Error} from {result.HostId}",new {});
    }
}

To handle deployment wide requests, hosts must call IHost.AddRequestHandler with the operation id as first parameter and an handler function as the second. All hosts running an application should handle a given operation id. While it is not strictly mandatory, it is therefore recommanded to call AddRequestHandler during the HostStarting event.

Handling a deployment wide request:

class DeploymentRequestsSamplePlugin : IHostPlugin
{
    public void Build(HostPluginBuildContext ctx)
    {
        ctx.HostStarting += (IHost host)=>{

            //Add a request handler
            host.AddRequestHandler<string,string>("helloworldOperation",(string arg)=>{
                return Task.FromResult(arg + " world!");
            });
        };
    }
}