upscale/swoole-session
Composer 安装命令:
composer require upscale/swoole-session
包简介
PHP sessions compatibility with Swoole web-server
README 文档
README
This library implements compatibility of native PHP sessions with Swoole / Open Swoole web-server.
Features:
- Transparent session start/stop
- Session ID in cookies or query string
- Native or custom session ID generator
- Automatic session data persistence
- Compliance with PHP session configuration
Installation
The library is to be installed via Composer as a dependency:
composer require upscale/swoole-session
Usage
Wrap your request handling middleware into the session decorator:
require 'vendor/autoload.php'; use Upscale\Swoole\Session\SessionDecorator; $server = new \Swoole\Http\Server('127.0.0.1', 8080); $server->set([ // Disable coroutines to safely access $_SESSION 'enable_coroutine' => false, ]); $server->on('request', new SessionDecorator(function ($request, $response) { $_SESSION['data'] ??= rand(); $response->end($_SESSION['data']); })); $server->start();
Limitations
Coroutines
PHP sessions rely on the superglobal variable $_SESSION making them incompatible with the Swoole coroutines.
When a request idles for an asynchronous I/O operation, its worker process is reused to handle other request(s).
Swoole switches the call stack context, but the superglobals stay in memory shared across coroutines/requests.
Session data loaded for one request leaks to other requests causing all sorts of data integrity issues.
Disable coroutines to safely use the PHP sessions:
$server->set([ 'enable_coroutine' => false, ]);
Output
Direct output bypassing the response instance \Swoole\Http\Response is prohibited in the Swoole environment.
Writing to the standard output stream violates the headers_sent requirement of the PHP session functions:
PHP Warning: session_start(): Cannot start session when headers already sent
Statements that "send headers" and hinder the sessions:
echo/printfwrite(STDOUT)file_put_contents('php://stdout')include 'template.phtml'header()setcookie/setrawcookie()- etc.
Output buffering commonly used by template engines avoids this pitfall, for example:
ob_start(); include $templatePhtml; $output = ob_get_clean(); $response->end($output);
Warning! Coroutines used to "send headers" despite the output buffering until this has been fixed in Swoole 4.5.3. This is not a problem since coroutines have to be disabled for the data integrity reasons discussed above.
Blocking
Concurrent requests are prone to the session write race conditions.
The default file-based session storage of PHP employs the filesystem locking to avoid the data corruption.
Requests of the same session ID execute sequentially blocking their respective worker processes from session_start() until session_write_close().
Asynchronous coroutine-aware libraries built specifically for Swoole:
License
Licensed under the Apache License, Version 2.0.
upscale/swoole-session 适用场景与选型建议
upscale/swoole-session 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.71k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2018 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「server」 「session」 「compatibility」 「swoole」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 upscale/swoole-session 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 upscale/swoole-session 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 upscale/swoole-session 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Secure session middleware for Slim 3 framework
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Non-I/O blocking PHP SessionHandler implementation using Nette/Caching with DI Extension for Nette framework
A jQuery augmented PHP library for creating and validating HTML forms
annotations-scan-plugin
Magento - Clean up session extension
统计信息
- 总下载量: 5.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2018-09-30