[meta] Use Fibers for concurrency in core

Created on 10 January 2022, almost 3 years ago
Updated 21 October 2023, about 1 year ago

Problem/Motivation

PHP 8.1 adds Fibers, opening this meta to group together potential use cases.

https://php.watch/versions/8.1/fibers

https://www.php.net/releases/8.1/en.php

https://clue.engineering/2021/fibers-in-php

Steps to reproduce

Proposed resolution

Fibers only work when you're using an async API.

i.e. it needs to look something like this:

1. Fiber::start() executes a callback

2. Code inside the callback calls a non-blocking function - send an async guzzle request, send an async MySQL query, immediately after doing so, it calls Fiber::suspend()

4. The code that called Fiber::start() can then call some other thing.

5. When we get back to the code in #2 (because other fibers finished or suspended) we'll be at the point after Fiber::suspend() and the code can then continue if it's got something back, or Fiber::suspend() again.

This means we need at least two things to make Fibers work:

1. Something that controls multiple similar code paths (cron execution, queue running, big pipe placeholder rendering).

2. Code that has an 'async mode' for being called inside a fiber, where it will execute non-blocking things and call Fiber::suspend()

Per discussion in this issue, combined with a blog post from Matt Glaman, we may have an initial candidate.

1. We add Fibers to Big Pipe placeholder rendering, so that the rendering of each placeholder happens inside a fiber.

2. We add async MySQL support via a new mysqli driver, then we implement support for that, as well as Fiber::suspend(), in the views SQL Query plugin. Async MySQL queries: ๐Ÿ“Œ [PP-1] Create the database driver for MySQLi Postponed

This will allow views listing queries to be executed non-blocking, so that other big pipe placeholders can be rendered in the meantime, those other placeholders might execute other async queries, or they might be loading and rendering entities - but either way it will fill in the time while queries are returning, whether they're 30ms or 1s.

Views queries are a relatively easy candidate, because they happen in one place, they often take long enough to make a difference to response times, and there's only one at a time. There is lots of other database and file i/o in core but this could be harder to implement with Fibers (like scanning and parsing YAML files). But once we've got one example going we can add more.

Other possible applications:
Queues: โœจ Add an 'instant' queue runner Needs work
Cron: #3257727: Run automated cron in a Fiber โ†’
Rebuilds: ๐Ÿ“Œ Add a cache prewarm API and use it to distribute cache rebuids after cache clears / during stampedes Needs work

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐ŸŒฑ Plan
Status

Active

Version

10.0 โœจ

Component
Baseย  โ†’

Last updated about 1 hour ago

Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024