Skip to content

Toro Cloud Dev Center


examples package: Throttling and monetization

The examples package contains services that demonstrate how Martini's API throttling and monetization features work. To make things easier, the package also contains a service called throttlingAndMonetization.config.StartMonetizationConfig which will create the necessary users, groups, and rules so you can test the functionality and rules without having to configure it yourself.

Related articles

Please see the following articles for more information:

Try it!

In the Navigator, expand the examples package and navigate to the code folder, then expand the throttlingAndMonetization.config package. This package contains the services that will set up the users, groups, and rules. It also contains a service that will delete the data if you no longer need it.

Run the throttlingAndMonetization.config.StartMonetizationConfig service to create the data necessary to test the rules. Once this is done, you can proceed to testing the rules, as shown below.

To do the test, invoke the service at throttlingAndMonetization.InvokeMonetizedApi. This service will invoke a REST endpoint in Martini at /api/monetization/hello/{name} until it's throttled. The service can be invoked with one of two users, who have their own limits, listed below:

User name Password Monitor Rule Invoke limit per minute, hour, day, week, month
Demo DEFAULT Rule 1 3, 7, 12, 14, 20
DemoII DEFAULT Rule 2 5, 10, 18, 24, 30

The services relevant for demonstrating this example are found under the throttlingAndMonetization package, as seen below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
examples
├── ...
└── code
    └── throttlingAndMonetization
        ├── InvokeMonetizedApi.gloop
        ├── config
        │   ├── ShutdownMonetizationConfig.gloop
        │   └── StartMonetizationConfig.gloop
        ├── controller
        │   └── Monetization.api
        ├── helper
        │   ├── RuleHelper.gloop
        │   └── UserHelper.gloop
        └── httpClient
            └── InvokeEndpoint.gloop

Explanation

A service has been configured to create the users, groups, and monitor rules for this example. Once Martini has been configured, simply trigger Martini's throttling mechanism by invoking the REST service at /api/monetization/hello/{name} more times than what is allowed (or invoke throttlingAndMonetization .InvokeMonetizedApi.gloop).

If no invoke limits have been reached, then the service will run as normal and will return a 200 OK status. Once a limit has been reached, Martini will return a 429 Too Many Requests status, and a response similar to the following:

1
2
3
4
5
{
    "result": "ERROR",
    "apiErrorCode": -1,
    "message": "You have already exceeded the '5' requests allowable 'minutely'."
}