OnixS C++ B3 BOE Binary Order Entry  1.2.0
API Documentation
Message Throttling

The B3 has limits on sending messages per time unit (throttling). If an application sends messages too often, they can be rejected, and finally, the session can be terminated. To not exceeds the limits, there is the OnixS::B3::BOE::Session::throttle method. This method performs the throttling of a session. It must be called before each send function call. If the count of messages per time unit exceeds a throttling limit, the function will be blocked until the given time interval is passed. The waiting is performed in the busy-wait (spin) mode to exclude the context switching overhead. The OnixS::B3::BOE::Session::throttlingLimit method is used to set throttling limit parameters:

using namespace OnixS::B3::BOE;
SessionSettings settings;
Session session(settings, &sessionListener);
const int MessagesPerSecondLimit = 10;
const int ThrottlingIntervalMillisecond = 1000;
// Set throttling limit parameters.
session.throttlingLimit(MessagesPerSecondLimit, ThrottlingIntervalMillisecond);
session.connect(CounterpartyHost, CounterpartyPort);
// Emulate some sending flow of an application.
for(size_t counter = 0; counter < 100; ++counter)
{
// This method must be called before each send function call.
// If the count of messages (10 in our case) per time unit (1 sec in our case) exceeds the throttling limit,
// this function will be blocked until the given time interval is passed.
session.throttle();
// A regular send call, which should be throttled.
session.send(order);
}

Also, there is the OnixS::B3::BOE::Session::tryThrottle method. It checks the throttling of a session, but it does not block the execution. If the count of messages per time unit exceeds the throttling limit, the function returns the delay (in milliseconds) until the sending becomes possible. Otherwise, it returns 0. This method can be helpful when one needs to detect that the message's rate exceeds the limit and react in some specific way:

// Emulate some sending flow of an application.
for(size_t counter = 0; counter < 100; ++counter)
{
// This method must be called before each send function call.
// If the count of messages (10 in our case) per time unit (1 sec in our case) exceeds the throttling limit,
// the function returns the delay (in milliseconds) until the sending becomes possible. Otherwise, it returns 0.
if(size_t delay = session.tryThrottle())
{
std::cout << "The message's rate exceeds the limit. " << delay << " milliseconds should be passed until the sending becomes possible." << std::endl;
// Wait for the 'delay' milliseconds or do some another specific actions if necessary.
}
// A regular send call, which should be throttled.
session.send(order);
}