Capacity limiting
Introduction
You can configure worker connection rate and capacity limit at launch, or while a deployment is running using the Deployment APIs. This functionality requires that clients connect using the alpha version of the Worker.Locator class (C#/C++).
This scenario demonstrates how to configure capacity limits for a running deployment. (It doesn’t cover rate limit configuration, as this is very similar.) If you’re looking for how to configure capacity and rate limits on launch, refer to the documentation on worker connection limits.
Find out how to download the Platform SDK from the setup guide.
Example: applying a dynamic capacity limit to a running cloud deployment
1. Instantiate clients
Instantiate clients of the DeploymentServiceClient
types.
Use the Create
method of the DeploymentServiceClient
.
private readonly DeploymentServiceClient _deploymentServiceClient = DeploymentServiceClient.Create();
By default, this uses your refresh token saved locally by the SpatialOS CLI. You can find your token at:
- Windows:
C:\Users\<username>\AppData\Local\.improbable\oauth2\oauth2_refresh_token\
- Mac:
~/.improbable/oauth2/oauth2_refresh_token
If you want to provide a refresh token, you can instantiate a PlatformRefreshTokenCredential
object and supply it to create
:
var credentialsWithProvidedToken = new PlatformRefreshTokenCredential("my_refresh_token");
private readonly DeploymentServiceClient _deploymentServiceClient = DeploymentServiceClient.Create(credentials: credentialsWithProvidedToken);
2. Choose a running deployment to apply the capacity limit to
You can only dynamically change rate and capacity limits for running deployments. Choose the first running deployment:
var suitableDeployment = _deploymentServiceClient.ListDeployments(new ListDeploymentsRequest
{
ProjectName = ProjectName,
DeploymentName = DeploymentName
}).First(d => d.Status == Deployment.Types.Status.Running));
3. Set the capacity limit for the worker type “my_worker_type” to 2
You apply capacity limits per worker type, so you must specify a valid worker type when setting the capacity limit.
suitableDeployment.WorkerConnectionCapacities.Clear();
suitableDeployment.WorkerConnectionCapacities.Add(new WorkerCapacity
{
WorkerType = "my_worker_type",
MaxCapacity = 2
});
_deploymentServiceClient.UpdateDeployment(new UpdateDeploymentRequest {Deployment = suitableDeployment});
To modify an existing capacity limit, you can modify the value of that object directly.
suitableDeployment.WorkerConnectionCapacities
.First(c => c.WorkerType == "type_to_change")
.MaxCapacity = 2;
_deploymentServiceClient.UpdateDeployment(new UpdateDeploymentRequest {Deployment = suitableDeployment});
4. Clear all existing capacity limits for a deployment
To clear all capacity limits, pass in an empty array for the capacity field.
suitableDeployment.WorkerConnectionCapacities.Clear();
_deploymentServiceClient.UpdateDeployment(new UpdateDeploymentRequest {Deployment = suitableDeployment});