| 153 | } |
| 154 | |
| 155 | int MmapLeakyBucket::Apply(int iCount) { |
| 156 | if (NULL == m_poLock || NULL == m_ptBucket) |
| 157 | return -1; |
| 158 | |
| 159 | m_poLock->WriteLockW(0); |
| 160 | |
| 161 | unsigned int tLatestRefillTime = (unsigned int) time(NULL); |
| 162 | tLatestRefillTime = tLatestRefillTime - tLatestRefillTime % (m_tConfig.tRefillPeriod); |
| 163 | |
| 164 | if (m_ptBucket->tLatestRefillTime != tLatestRefillTime) { |
| 165 | m_ptBucket->tLatestRefillTime = tLatestRefillTime; |
| 166 | m_ptBucket->iTokenCount = m_tConfig.iBucketSize; |
| 167 | } |
| 168 | |
| 169 | int ret = -1; |
| 170 | if (m_ptBucket->iTokenCount >= iCount) { |
| 171 | m_ptBucket->iTokenCount -= iCount; |
| 172 | ret = 0; |
| 173 | } |
| 174 | |
| 175 | m_poLock->Unlock(0); |
| 176 | |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | void MmapLeakyBucket::SetConfig(Config_t &config) { |
| 181 | if (NULL == m_poLock) |
no test coverage detected