4.0.1

主要更新

  • 支持MySQL8全新的caching_sha2_password密码验证算法
  • 增加enable_coroutine配置项,用于关闭自动创建协程
  • 移除--enable-coroutine编译配置
  • 修复chan->push无法立即唤醒等待协程的问题

关闭内置协程

4.0.1之前的版本,底层在ServerTimer的事件回调函数中会自动创建协程,如果回调函数中没有使用任何协程API,这会浪费一次协程创建/销毁操作。而且无法与1.x保持兼容。

新版本中增加了enable_coroutine配置项,可关闭内置协程。用户可根据需要,在回调函数中自行使用Coroutine::creatego创建协程。关闭内置协程后,底层与1.x版本的行为保持了一致性,实现了100%兼容。原先使用1.x的框架,也可以直接基于4.0作为运行环境。

现在可以动态选择是否启用内置协程,那么--enable-coroutine编译配置变得可有可无了。新版本中移除了该编译选项。

  1. use Swoole\Http\Request;
  2. use Swoole\Http\Response;
  3. $http = new swoole_http_server('127.0.0.1', 9501);
  4. $http->set([
  5. 'enable_coroutine' => false, // close build-in coroutine
  6. ]);
  7. $http->on('workerStart', function () {
  8. echo "Coroutine is " . (Co::getuid() > 0 ? 'enable' : 'disable')."\n";
  9. });
  10. $http->on("request", function (Request $request, Response $response) {
  11. $response->header("Content-Type", "text/plain");
  12. if ($request->server['request_uri'] == '/co') {
  13. //关闭内置协程后,需要手工创建协程
  14. go(function () use ($response) {
  15. $response->end("Hello Coroutine #" . Co::getuid());
  16. });
  17. } else {
  18. //没有任何协程操作,这里不存在额外的协程调度开销
  19. $response->end("Hello Swoole #" . Co::getuid());
  20. }
  21. });
  22. $http->start();