wikimedia/request-timeout 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wikimedia/request-timeout

最新稳定版本:v3.0.0

Composer 安装命令:

composer require wikimedia/request-timeout

包简介

Request timeout library for Excimer with plain PHP fallback

README 文档

README

Excimer is an extension for PHP which provides flexible userspace timers.

This library provides a global request timeout concept, ideally using Excimer, but with a fallback to set_time_limit() if Excimer is not available.

Typical usage:

use Wikimedia\RequestTimeout\RequestTimeout;

RequestTimeout::singleton()->setWallTimeLimit( 20 );

This sets a timer for 20 seconds of wall clock time. When it expires, a TimeoutException will be thrown.

It is possible to query the amount of time remaining:

if ( RequestTimeout::singleton()->getWallTimeRemaining() > 5 ) {
	do_slow_thing();
} else {
	do_fast_thing();
}

This works in the fallback mode.

The library provides a critical section concept. If a critical section is active, timeouts will be queued, rather than immediately thrown. The timeout exception will be thrown once no critical section is open.

$csp = RequestTimeout::singleton()->createCriticalSectionProvider( 5 );
$csp->enter( __METHOD__ );
try {
	do_something();
} finally {
	$csp->exit( __METHOD__ );
}

It is important to always exit a critical section. If the code in the critical section can throw an error, try/finally can be used to ensure that the critical section is exited. Alternatively we provide a scope variable model:

function foo() {
	$scope = $csp->scopedEnter( __METHOD__ );
	do_something();
}

The critical section exits when the scope object is destroyed. However, with this method, it is important to not terminate the request during the critical section, for example by calling exit(), or by keeping the scope object in global variable after the function returns. The library may throw an exception from a destructor during request shutdown, which causes a PHP fatal error.

Critical sections are not functional in the fallback mode.

统计信息

  • 总下载量: 399.15k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 19
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-06