The Throttle mediator can be used for rate limiting as well as concurrency based limiting. A WS-Policy dictates the throttling configuration and may be specified inline or loaded from the registry. Please refer to the samples document for sample throttling policies. The Throttle mediator could be added in the request path for rate limiting and concurrent access limitation. When using for concurrent access limitation, the same throttle mediator 'id' must be triggered on the response flow so that completed responses are deducted from the available limit. (i.e. two instances of the throttle mediator with the same 'id' attribute in the request and response flows). The 'onReject' and 'onAccept' sequence references or inline sequences define how accepted and rejected messages are to be handled.
<throttle [onReject="string"] [onAccept="string"] id="string"> (<policy key="string"/> | <policy>..</policy>) <onReject>..</onReject>? <onAccept>..</onAccept>? </throttle>
<in> <throttle id="A"> <policy> <!-- define throttle policy --> <wsp:Policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:throttle="http://www.wso2.org/products/wso2commons/throttle"> <throttle:ThrottleAssertion> <throttle:MaximumConcurrentAccess>10</throttle:MaximumConcurrentAccess> </throttle:ThrottleAssertion> </wsp:Policy> </policy> <onAccept> <log level="custom"> <property name="text" value="**Access Accept**"/> </log> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </onAccept> <onReject> <log level="custom"> <property name="text" value="**Access Denied**"/> </log> <makefault> <code value="tns:Receiver" xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/> <reason value="**Access Denied**"/> </makefault> <property name="RESPONSE" value="true"/> <header name="To" action="remove"/> <send/> <drop/> </onReject> </throttle> </in>
Above example specifies a throttle mediator inside the in mediator. Therefore, all request messages directed to the main sequence will be subjected to throttling. Throttle mediator has 'policy', 'onAccept' and 'onReject' tags at top level. The 'policy' tag specifies the throttling policy for throttling messages. This sample policy only contains a component called "MaximumConcurrentAccess" .This indicates the maximum number of concurrent requests that can pass through Synapse on a single unit of time.