115 || in_array($entityType, static::$limitedEntityTypes,
true)
116 || !in_array($method, static::$ignoreMethod,
true)
119 $key = static::getKey($entityType, $entity, $method);
120 if (!(static::$timeRegistered[$key] ??
null))
122 static::$timeRegistered[$key] = [
123 'entityType' => $entityType,
131 static::$timeRegistered[$key][
'timeStart'][] = microtime(
true);
146 && in_array($entityType, static::$limitedEntityTypes,
true)
147 && !in_array($method, static::$ignoreMethod,
true)
150 $key = static::getKey($entityType, $entity, $method);
151 if (static::$timeRegistered[$key])
153 static::$timeRegistered[$key][
'timeFinish'][] = microtime(
true);
156 if (!static::$isFinaliseInit)
158 static::$isFinaliseInit =
true;
172 public static function is($entityType, $entity, $method): bool
176 || !in_array($entityType, static::$limitedEntityTypes,
true)
177 || in_array($method, static::$ignoreMethod,
true)
183 $totalTime = static::getRestTime($entityType, $entity, $method);
184 if ($totalTime > static::getLimitTime())
186 if (Loader::includeModule(
'bitrix24') && function_exists(
'saveRestStat'))
188 saveRestStat(static::getDomain(), $entityType, $entity, $method, $totalTime);
197 private static function getKey($entityType, $entity, $method, $bucketNum =
null): string
215 public static function getResetTime($entityType, $entity, $method): ?int
219 if (static::isActive())
221 $resource = static::getConnectionResource();
224 $numBucket = static::getNumBucket();
225 $key = static::getKey($entityType, $entity, $method);
226 $keyExpire = static::CACHE_EXPIRE_TIME_PREFIX .
'|' . $key;
227 for ($i = static::$bucketCount - 1; $i >= 0; $i--)
229 $time = (float)$resource->get($key . ($numBucket - $i));
230 if ($time > 0 && $resource->exists($keyExpire . ($numBucket - $i)))
232 $result = (int)$resource->get($keyExpire . ($numBucket - $i));
238 if (!empty(static::$timeRegistered))
240 $item = reset(static::$timeRegistered);
241 if (!empty($item[
'timeStart']))
243 $firstTimeStart = reset($item[
'timeStart']);
244 $result = $firstTimeStart + static::$bucketCount * static::$bucketSize;
272 public static function getRestTime($entityType, $entity, $method): float
275 if (static::isActive())
277 $numBucket = static::getNumBucket();
279 $key = static::getKey($entityType, $entity, $method);
280 $resource = static::getConnectionResource();
283 for ($i = 0; $i < static::$bucketCount; $i++)
285 $result[] = (float)$resource->get($key . ($numBucket - $i));
288 if (!empty(static::$timeRegistered[$key][
'timeStart']))
290 foreach (static::$timeRegistered[$key][
'timeStart'] as $k => $timeStart)
292 if (static::$timeRegistered[$key][
'timeFinish'][$k] ??
null)
294 $time = static::$timeRegistered[$key][
'timeFinish'][$k] - $timeStart;
295 if ($time > static::$minimalFixTime)
304 return array_sum($result);
314 if (static::$timeRegistered && static::isActive())
316 $resource = static::getConnectionResource();
319 foreach (static::$timeRegistered as $item)
322 $firstTime = reset($item[
'timeStart']);
323 foreach ($item[
'timeStart'] as $k => $timeStart)
325 if ($item[
'timeFinish'][$k])
327 $time += $item[
'timeFinish'][$k] - $timeStart;
331 if ($time > static::$minimalFixTime)
333 $key = static::getKey($item[
'entityType'], $item[
'entity'], $item[
'method'], static::getNumBucket());
334 if ($resource->exists($key))
336 $resource->incrByFloat($key, $time);
340 $expireAt = $firstTime + static::$bucketCount * static::$bucketSize;
341 $resource->incrByFloat($key, $time);
342 $resource->expire($key, $expireAt);
344 $keyExpire = static::CACHE_EXPIRE_TIME_PREFIX .
'|' . $key;
345 $resource->incrByFloat($keyExpire, $expireAt);
346 $resource->expire($keyExpire, $expireAt);
350 static::$timeRegistered = [];