每秒节流消息

时间:2014-06-26 07:30:35

标签: c# throttling

我试图限制发送给SMPP SMC(服务器)的消息量。我需要限制它们,以便SMC不会限制和丢弃我的消息。

这就是我的尝试:

double _client.SendSpeedLimit = 20;
if (Math.Abs(_client.SendSpeedLimit) > 1E-6 && _lastSubmit != DateTime.MinValue)
{
    check submit speed and wait for next time
    double elapsed = (DateTime.Now - _lastSubmit).TotalMilliseconds;
    if (elapsed < 1000f / _client.SendSpeedLimit)
    {
        int interval = Convert.ToInt32((1000f / _client.SendSpeedLimit) - elapsed);
        Thread.Sleep(interval);         
    }
}

和...

int left = (int)(start.AddSeconds(1.0 / (double)_client.SendSpeedLimit) - DateTime.UtcNow).TotalMilliseconds;
if (left > 0)
{
    MobileDAL.InsertError(Settings.Default.MobileConnectionString, "Throttled: " + _clientFriendlyName, "", _AccountID);
    Console.WriteLine("Throttled: " + _clientFriendlyName, "", _AccountID);
    while (left > 0)
    {
        Thread.Sleep(10);
        left = left - 10;
    }
    start = DateTime.UtcNow;
}

但他们似乎没有按预期工作,因为我仍然受到服务提供商(SMC)的限制。 代码有问题吗?有人可以提供改善点吗?

0 个答案:

没有答案