2013-04-15 16:50:35 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "EventTokenBucket.h"
|
|
|
|
|
2013-09-22 07:04:57 +04:00
|
|
|
#include "nsICancelable.h"
|
2016-01-09 04:20:50 +03:00
|
|
|
#include "nsIIOService.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsNetCID.h"
|
2016-01-09 04:20:50 +03:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-04-15 16:50:35 +04:00
|
|
|
#include "nsSocketTransportService2.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#endif
|
2013-04-15 16:50:35 +04:00
|
|
|
|
2013-04-15 17:40:53 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#include <mmsystem.h>
|
|
|
|
#endif
|
|
|
|
|
2013-04-15 16:50:35 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
////////////////////////////////////////////
|
|
|
|
// EventTokenBucketCancelable
|
|
|
|
////////////////////////////////////////////
|
|
|
|
|
|
|
|
class TokenBucketCancelable : public nsICancelable
|
|
|
|
{
|
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-04-15 16:50:35 +04:00
|
|
|
NS_DECL_NSICANCELABLE
|
|
|
|
|
2014-08-05 17:20:50 +04:00
|
|
|
explicit TokenBucketCancelable(class ATokenBucketEvent *event);
|
2013-04-15 16:50:35 +04:00
|
|
|
void Fire();
|
|
|
|
|
|
|
|
private:
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~TokenBucketCancelable() {}
|
|
|
|
|
2013-04-15 16:50:35 +04:00
|
|
|
friend class EventTokenBucket;
|
|
|
|
ATokenBucketEvent *mEvent;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(TokenBucketCancelable, nsICancelable)
|
2013-04-15 16:50:35 +04:00
|
|
|
|
|
|
|
TokenBucketCancelable::TokenBucketCancelable(ATokenBucketEvent *event)
|
|
|
|
: mEvent(event)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TokenBucketCancelable::Cancel(nsresult reason)
|
|
|
|
{
|
2017-04-27 21:34:42 +03:00
|
|
|
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
2013-04-15 16:50:35 +04:00
|
|
|
mEvent = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TokenBucketCancelable::Fire()
|
|
|
|
{
|
|
|
|
if (!mEvent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ATokenBucketEvent *event = mEvent;
|
|
|
|
mEvent = nullptr;
|
|
|
|
event->OnTokenBucketAdmitted();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////
|
|
|
|
// EventTokenBucket
|
|
|
|
////////////////////////////////////////////
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_IMPL_ISUPPORTS(EventTokenBucket, nsITimerCallback, nsINamed)
|
2013-04-15 16:50:35 +04:00
|
|
|
|
|
|
|
// by default 1hz with no burst
|
|
|
|
EventTokenBucket::EventTokenBucket(uint32_t eventsPerSecond,
|
|
|
|
uint32_t burstSize)
|
|
|
|
: mUnitCost(kUsecPerSec)
|
|
|
|
, mMaxCredit(kUsecPerSec)
|
|
|
|
, mCredit(kUsecPerSec)
|
|
|
|
, mPaused(false)
|
|
|
|
, mStopped(false)
|
|
|
|
, mTimerArmed(false)
|
2013-04-15 17:40:53 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
, mFineGrainTimerInUse(false)
|
|
|
|
, mFineGrainResetTimerArmed(false)
|
|
|
|
#endif
|
2013-04-15 16:50:35 +04:00
|
|
|
{
|
|
|
|
mLastUpdate = TimeStamp::Now();
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIEventTarget> sts;
|
|
|
|
nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
sts = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2017-10-16 09:12:02 +03:00
|
|
|
mTimer = NS_NewTimer(sts);
|
2013-04-15 16:50:35 +04:00
|
|
|
SetRate(eventsPerSecond, burstSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
EventTokenBucket::~EventTokenBucket()
|
|
|
|
{
|
2017-07-26 23:03:57 +03:00
|
|
|
SOCKET_LOG(("EventTokenBucket::dtor %p events=%zu\n",
|
2013-04-15 16:50:35 +04:00
|
|
|
this, mEvents.GetSize()));
|
|
|
|
|
2016-01-21 20:40:15 +03:00
|
|
|
CleanupTimers();
|
|
|
|
|
|
|
|
// Complete any queued events to prevent hangs
|
|
|
|
while (mEvents.GetSize()) {
|
|
|
|
RefPtr<TokenBucketCancelable> cancelable =
|
|
|
|
dont_AddRef(static_cast<TokenBucketCancelable *>(mEvents.PopFront()));
|
|
|
|
cancelable->Fire();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventTokenBucket::CleanupTimers()
|
|
|
|
{
|
|
|
|
if (mTimer && mTimerArmed) {
|
2013-04-15 16:50:35 +04:00
|
|
|
mTimer->Cancel();
|
2016-01-21 20:40:15 +03:00
|
|
|
}
|
|
|
|
mTimer = nullptr;
|
|
|
|
mTimerArmed = false;
|
2013-04-15 16:50:35 +04:00
|
|
|
|
2013-04-15 17:40:53 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
NormalTimers();
|
2016-01-21 20:40:15 +03:00
|
|
|
if (mFineGrainResetTimer && mFineGrainResetTimerArmed) {
|
2013-04-15 17:40:53 +04:00
|
|
|
mFineGrainResetTimer->Cancel();
|
|
|
|
}
|
2016-01-21 20:40:15 +03:00
|
|
|
mFineGrainResetTimer = nullptr;
|
|
|
|
mFineGrainResetTimerArmed = false;
|
2013-04-15 17:40:53 +04:00
|
|
|
#endif
|
2013-04-15 16:50:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventTokenBucket::SetRate(uint32_t eventsPerSecond,
|
|
|
|
uint32_t burstSize)
|
|
|
|
{
|
|
|
|
SOCKET_LOG(("EventTokenBucket::SetRate %p %u %u\n",
|
|
|
|
this, eventsPerSecond, burstSize));
|
|
|
|
|
|
|
|
if (eventsPerSecond > kMaxHz) {
|
|
|
|
eventsPerSecond = kMaxHz;
|
|
|
|
SOCKET_LOG((" eventsPerSecond out of range\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!eventsPerSecond) {
|
|
|
|
eventsPerSecond = 1;
|
|
|
|
SOCKET_LOG((" eventsPerSecond out of range\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
mUnitCost = kUsecPerSec / eventsPerSecond;
|
|
|
|
mMaxCredit = mUnitCost * burstSize;
|
|
|
|
if (mMaxCredit > kUsecPerSec * 60 * 15) {
|
|
|
|
SOCKET_LOG((" burstSize out of range\n"));
|
|
|
|
mMaxCredit = kUsecPerSec * 60 * 15;
|
|
|
|
}
|
|
|
|
mCredit = mMaxCredit;
|
|
|
|
mLastUpdate = TimeStamp::Now();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventTokenBucket::ClearCredits()
|
|
|
|
{
|
2017-04-27 21:34:42 +03:00
|
|
|
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
2013-04-15 16:50:35 +04:00
|
|
|
SOCKET_LOG(("EventTokenBucket::ClearCredits %p\n", this));
|
|
|
|
mCredit = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
EventTokenBucket::BurstEventsAvailable()
|
|
|
|
{
|
2017-04-27 21:34:42 +03:00
|
|
|
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
2013-04-15 16:50:35 +04:00
|
|
|
return static_cast<uint32_t>(mCredit / mUnitCost);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
EventTokenBucket::QueuedEvents()
|
|
|
|
{
|
2017-04-27 21:34:42 +03:00
|
|
|
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
2013-04-15 16:50:35 +04:00
|
|
|
return mEvents.GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventTokenBucket::Pause()
|
|
|
|
{
|
2017-04-27 21:34:42 +03:00
|
|
|
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
2013-04-15 16:50:35 +04:00
|
|
|
SOCKET_LOG(("EventTokenBucket::Pause %p\n", this));
|
|
|
|
if (mPaused || mStopped)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mPaused = true;
|
|
|
|
if (mTimerArmed) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimerArmed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventTokenBucket::UnPause()
|
|
|
|
{
|
2017-04-27 21:34:42 +03:00
|
|
|
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
2013-04-15 16:50:35 +04:00
|
|
|
SOCKET_LOG(("EventTokenBucket::UnPause %p\n", this));
|
|
|
|
if (!mPaused || mStopped)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mPaused = false;
|
|
|
|
DispatchEvents();
|
|
|
|
UpdateTimer();
|
|
|
|
}
|
|
|
|
|
2015-12-23 18:47:13 +03:00
|
|
|
void
|
|
|
|
EventTokenBucket::Stop()
|
|
|
|
{
|
2017-04-27 21:34:42 +03:00
|
|
|
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
2015-12-23 18:47:13 +03:00
|
|
|
SOCKET_LOG(("EventTokenBucket::Stop %p armed=%d\n", this, mTimerArmed));
|
|
|
|
mStopped = true;
|
2016-01-21 20:40:15 +03:00
|
|
|
CleanupTimers();
|
|
|
|
|
|
|
|
// Complete any queued events to prevent hangs
|
|
|
|
while (mEvents.GetSize()) {
|
|
|
|
RefPtr<TokenBucketCancelable> cancelable =
|
|
|
|
dont_AddRef(static_cast<TokenBucketCancelable *>(mEvents.PopFront()));
|
|
|
|
cancelable->Fire();
|
2015-12-23 18:47:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 16:50:35 +04:00
|
|
|
nsresult
|
|
|
|
EventTokenBucket::SubmitEvent(ATokenBucketEvent *event, nsICancelable **cancelable)
|
|
|
|
{
|
2017-04-27 21:34:42 +03:00
|
|
|
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
2013-04-15 16:50:35 +04:00
|
|
|
SOCKET_LOG(("EventTokenBucket::SubmitEvent %p\n", this));
|
|
|
|
|
|
|
|
if (mStopped || !mTimer)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
UpdateCredits();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TokenBucketCancelable> cancelEvent = new TokenBucketCancelable(event);
|
2013-04-15 16:50:35 +04:00
|
|
|
// When this function exits the cancelEvent needs 2 references, one for the
|
|
|
|
// mEvents queue and one for the caller of SubmitEvent()
|
|
|
|
|
|
|
|
NS_ADDREF(*cancelable = cancelEvent.get());
|
|
|
|
|
|
|
|
if (mPaused || !TryImmediateDispatch(cancelEvent.get())) {
|
|
|
|
// queue it
|
|
|
|
SOCKET_LOG((" queued\n"));
|
2014-03-15 23:00:15 +04:00
|
|
|
mEvents.Push(cancelEvent.forget().take());
|
2013-04-15 16:50:35 +04:00
|
|
|
UpdateTimer();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SOCKET_LOG((" dispatched synchronously\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
EventTokenBucket::TryImmediateDispatch(TokenBucketCancelable *cancelable)
|
|
|
|
{
|
|
|
|
if (mCredit < mUnitCost)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mCredit -= mUnitCost;
|
|
|
|
cancelable->Fire();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventTokenBucket::DispatchEvents()
|
|
|
|
{
|
2017-04-27 21:34:42 +03:00
|
|
|
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
2013-04-15 16:50:35 +04:00
|
|
|
SOCKET_LOG(("EventTokenBucket::DispatchEvents %p %d\n", this, mPaused));
|
|
|
|
if (mPaused || mStopped)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (mEvents.GetSize() && mUnitCost <= mCredit) {
|
2017-07-06 15:00:35 +03:00
|
|
|
RefPtr<TokenBucketCancelable> cancelable =
|
2013-04-15 16:50:35 +04:00
|
|
|
dont_AddRef(static_cast<TokenBucketCancelable *>(mEvents.PopFront()));
|
|
|
|
if (cancelable->mEvent) {
|
|
|
|
SOCKET_LOG(("EventTokenBucket::DispachEvents [%p] "
|
2016-12-16 06:16:31 +03:00
|
|
|
"Dispatching queue token bucket event cost=%" PRIu64 " credit=%" PRIu64 "\n",
|
2013-04-15 16:50:35 +04:00
|
|
|
this, mUnitCost, mCredit));
|
|
|
|
mCredit -= mUnitCost;
|
|
|
|
cancelable->Fire();
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2013-04-15 17:40:53 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
if (!mEvents.GetSize())
|
|
|
|
WantNormalTimers();
|
|
|
|
#endif
|
2013-04-15 16:50:35 +04:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2013-04-15 16:50:35 +04:00
|
|
|
void
|
|
|
|
EventTokenBucket::UpdateTimer()
|
|
|
|
{
|
2017-04-27 21:34:42 +03:00
|
|
|
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
2013-04-15 16:50:35 +04:00
|
|
|
if (mTimerArmed || mPaused || mStopped || !mEvents.GetSize() || !mTimer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mCredit >= mUnitCost)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// determine the time needed to wait to accumulate enough credits to admit
|
|
|
|
// one more event and set the timer for that point. Always round it
|
|
|
|
// up because firing early doesn't help.
|
|
|
|
//
|
|
|
|
uint64_t deficit = mUnitCost - mCredit;
|
|
|
|
uint64_t msecWait = (deficit + (kUsecPerMsec - 1)) / kUsecPerMsec;
|
|
|
|
|
|
|
|
if (msecWait < 4) // minimum wait
|
|
|
|
msecWait = 4;
|
|
|
|
else if (msecWait > 60000) // maximum wait
|
|
|
|
msecWait = 60000;
|
|
|
|
|
2013-04-15 17:40:53 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
FineGrainTimers();
|
|
|
|
#endif
|
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("EventTokenBucket::UpdateTimer %p for %" PRIu64 "ms\n",
|
2013-04-15 16:50:35 +04:00
|
|
|
this, msecWait));
|
|
|
|
nsresult rv = mTimer->InitWithCallback(this, static_cast<uint32_t>(msecWait),
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
mTimerArmed = NS_SUCCEEDED(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
EventTokenBucket::Notify(nsITimer *timer)
|
|
|
|
{
|
2017-04-27 21:34:42 +03:00
|
|
|
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
2013-04-15 17:40:53 +04:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (timer == mFineGrainResetTimer) {
|
|
|
|
FineGrainResetTimerNotify();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-15 16:50:35 +04:00
|
|
|
SOCKET_LOG(("EventTokenBucket::Notify() %p\n", this));
|
|
|
|
mTimerArmed = false;
|
|
|
|
if (mStopped)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
UpdateCredits();
|
|
|
|
DispatchEvents();
|
|
|
|
UpdateTimer();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
EventTokenBucket::GetName(nsACString& aName)
|
|
|
|
{
|
|
|
|
aName.AssignLiteral("EventTokenBucket");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-15 16:50:35 +04:00
|
|
|
void
|
|
|
|
EventTokenBucket::UpdateCredits()
|
|
|
|
{
|
2017-04-27 21:34:42 +03:00
|
|
|
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
|
2013-04-15 16:50:35 +04:00
|
|
|
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
TimeDuration elapsed = now - mLastUpdate;
|
|
|
|
mLastUpdate = now;
|
|
|
|
|
|
|
|
mCredit += static_cast<uint64_t>(elapsed.ToMicroseconds());
|
|
|
|
if (mCredit > mMaxCredit)
|
|
|
|
mCredit = mMaxCredit;
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("EventTokenBucket::UpdateCredits %p to %" PRIu64 " (%" PRIu64 " each.. %3.2f)\n",
|
2013-04-15 16:50:35 +04:00
|
|
|
this, mCredit, mUnitCost, (double)mCredit / mUnitCost));
|
|
|
|
}
|
|
|
|
|
2013-04-15 17:40:53 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
void
|
|
|
|
EventTokenBucket::FineGrainTimers()
|
|
|
|
{
|
|
|
|
SOCKET_LOG(("EventTokenBucket::FineGrainTimers %p mFineGrainTimerInUse=%d\n",
|
|
|
|
this, mFineGrainTimerInUse));
|
|
|
|
|
|
|
|
mLastFineGrainTimerUse = TimeStamp::Now();
|
|
|
|
|
|
|
|
if (mFineGrainTimerInUse)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mUnitCost > kCostFineGrainThreshold)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SOCKET_LOG(("EventTokenBucket::FineGrainTimers %p timeBeginPeriod()\n",
|
|
|
|
this));
|
|
|
|
|
|
|
|
mFineGrainTimerInUse = true;
|
|
|
|
timeBeginPeriod(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventTokenBucket::NormalTimers()
|
|
|
|
{
|
|
|
|
if (!mFineGrainTimerInUse)
|
|
|
|
return;
|
|
|
|
mFineGrainTimerInUse = false;
|
|
|
|
|
|
|
|
SOCKET_LOG(("EventTokenBucket::NormalTimers %p timeEndPeriod()\n", this));
|
|
|
|
timeEndPeriod(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventTokenBucket::WantNormalTimers()
|
|
|
|
{
|
|
|
|
if (!mFineGrainTimerInUse)
|
|
|
|
return;
|
|
|
|
if (mFineGrainResetTimerArmed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
TimeDuration elapsed(TimeStamp::Now() - mLastFineGrainTimerUse);
|
|
|
|
static const TimeDuration fiveSeconds = TimeDuration::FromSeconds(5);
|
|
|
|
|
|
|
|
if (elapsed >= fiveSeconds) {
|
|
|
|
NormalTimers();
|
|
|
|
return;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2013-04-15 17:40:53 +04:00
|
|
|
if (!mFineGrainResetTimer)
|
2017-10-16 09:12:02 +03:00
|
|
|
mFineGrainResetTimer = NS_NewTimer();
|
2013-04-15 17:40:53 +04:00
|
|
|
|
|
|
|
// if we can't delay the reset, just do it now
|
|
|
|
if (!mFineGrainResetTimer) {
|
|
|
|
NormalTimers();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pad the callback out 100ms to avoid having to round trip this again if the
|
|
|
|
// timer calls back just a tad early.
|
|
|
|
SOCKET_LOG(("EventTokenBucket::WantNormalTimers %p "
|
|
|
|
"Will reset timer granularity after delay", this));
|
|
|
|
|
|
|
|
mFineGrainResetTimer->InitWithCallback(
|
|
|
|
this,
|
|
|
|
static_cast<uint32_t>((fiveSeconds - elapsed).ToMilliseconds()) + 100,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
mFineGrainResetTimerArmed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventTokenBucket::FineGrainResetTimerNotify()
|
|
|
|
{
|
|
|
|
SOCKET_LOG(("EventTokenBucket::FineGrainResetTimerNotify() events = %d\n",
|
|
|
|
this, mEvents.GetSize()));
|
|
|
|
mFineGrainResetTimerArmed = false;
|
|
|
|
|
|
|
|
// If we are currently processing events then wait for the queue to drain
|
|
|
|
// before trying to reset back to normal timers again
|
|
|
|
if (!mEvents.GetSize())
|
|
|
|
WantNormalTimers();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|