mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-12 11:19:56 +00:00
feat: 更新请求上下文处理
This commit is contained in:
parent
734b5f9534
commit
625648c908
@ -19,8 +19,7 @@ class WebApi
|
|||||||
*/
|
*/
|
||||||
public function handle($request, Closure $next)
|
public function handle($request, Closure $next)
|
||||||
{
|
{
|
||||||
// 为每个请求生成唯一ID
|
// 记录请求信息
|
||||||
$request->requestId = RequestContext::generateRequestId();
|
|
||||||
RequestContext::set('start_time', microtime(true));
|
RequestContext::set('start_time', microtime(true));
|
||||||
RequestContext::set('header_language', $request->header('language'));
|
RequestContext::set('header_language', $request->header('language'));
|
||||||
|
|
||||||
|
|||||||
@ -11,6 +11,8 @@ use Swoole\Coroutine;
|
|||||||
*/
|
*/
|
||||||
class RequestContext
|
class RequestContext
|
||||||
{
|
{
|
||||||
|
/** @var string 请求ID的上下文键 */
|
||||||
|
private const CONTEXT_KEY = 'request_id';
|
||||||
|
|
||||||
/** @var string 请求ID前缀 */
|
/** @var string 请求ID前缀 */
|
||||||
private const REQUEST_ID_PREFIX = 'req';
|
private const REQUEST_ID_PREFIX = 'req';
|
||||||
@ -37,7 +39,16 @@ class RequestContext
|
|||||||
*/
|
*/
|
||||||
public static function getCurrentRequestId($requestId = null): ?string
|
public static function getCurrentRequestId($requestId = null): ?string
|
||||||
{
|
{
|
||||||
return $requestId ?? request()?->requestId;
|
if (str_starts_with($requestId, self::REQUEST_ID_PREFIX)) {
|
||||||
|
return $requestId;
|
||||||
|
}
|
||||||
|
|
||||||
|
$request = request();
|
||||||
|
if (!$request->attributes->has(static::CONTEXT_KEY)) {
|
||||||
|
$request->attributes->set(static::CONTEXT_KEY, self::generateRequestId());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $request->attributes->get(static::CONTEXT_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user