OnixS C++ FIX Engine  4.12.0
API Documentation
Message Throttling

Many venues have limits of sending messages per time unit (throttling). If an application sends messages too often, then they can be rejected. To not exceeds the limits, there is the OnixS::FIX::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 the 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::FIX::Session::throttlingLimit method is used to set throttling limit parameters:

using namespace OnixS::FIX;
const int MessagesPerSecondLimit = 10;
const int ThrottlingIntervalMillisecond = 1000;
const std::string AcceptorHost = "localhost";
const int AcceptorPort = 5000;
Message order(FIX42::Values::MsgType::Order_Single, ProtocolVersion::FIX_42);
Session session("Sender", "Target", ProtocolVersion::FIX_42, &listener);
// Set throttling limit parameters.
session.throttlingLimit(MessagesPerSecondLimit, ThrottlingIntervalMillisecond);
session.logonAsInitiator(AcceptorHost, AcceptorPort);
// 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 per time unit exceeds the throttling limit,
// the 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::FIX::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. The resetWhenDelay flag indicates if the calculation of messages per throttling interval should be reset and started again when a delay is returned. 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 per time unit 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);
}

Additionally, the OnixS::FIX::Throttler class can simplify implementing the throttling (rate limiting) in specific scenarios. This class has similar OnixS::FIX::Throttler::throttle / OnixS::FIX::Throttler::tryThrottle methods that can be used similarly. However, unlike the session's functionality, different throttler objects with different throttling limit parameters can be created and used independently of a session. This can help to implement the throttling on the acceptor's side or perform the different throttling for different message types:

class AcceptorListener : public ISessionListener
{
Throttler orderSingleThrottler_;
Throttler orderCancelRequestThrottler_;
public:
// Create throttler objects and set throttling limit parameters.
AcceptorListener():orderSingleThrottler_(OrderSinglePerSecondLimit, ThrottlingInterval),
orderCancelRequestThrottler_(OrderCancelRequestPerSecondLimit, ThrottlingInterval)
{}
void onInboundApplicationMsg(Message & message, Session * sn) ONIXS_FIXENGINE_OVERRIDE
{
// This method must be called before each action that should be throttled.
if(message.type() == FIX42::Values::MsgType::Order_Single && orderSingleThrottler_.tryThrottle())
sn->sendReject(message.seqNum(), "The OrderSingle message's rate exceeds the limit.");
// This method must be called before each action that should be throttled.
if(message.type() == FIX42::Values::MsgType::Order_Cancel_Request && orderCancelRequestThrottler_.tryThrottle())
sn->sendReject(message.seqNum(), "The OrderCancelRequest message's rate exceeds the limit.");
}
};
AcceptorListener acceptorListener;
Session acceptor("Target", "Sender", ProtocolVersion::FIX_42, &acceptorListener);

See Also