From 19937d75d040fd28722f225f2d08bcf1412f1054 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Thu, 2 Dec 2021 23:38:17 +0100 Subject: [PATCH] Remove outdated docs --- docs/_config.yml | 25 +- docs/event-loop/README.md | 115 --------- docs/event-loop/api.md | 416 ------------------------------- docs/iterators/README.md | 74 ------ docs/iterators/combinators.md | 14 -- docs/iterators/transformation.md | 20 -- docs/utils/README.md | 11 - docs/utils/callable-maker.md | 16 -- docs/utils/struct.md | 47 ---- 9 files changed, 11 insertions(+), 727 deletions(-) delete mode 100644 docs/event-loop/README.md delete mode 100644 docs/event-loop/api.md delete mode 100644 docs/iterators/README.md delete mode 100644 docs/iterators/combinators.md delete mode 100644 docs/iterators/transformation.md delete mode 100644 docs/utils/README.md delete mode 100644 docs/utils/callable-maker.md delete mode 100644 docs/utils/struct.md diff --git a/docs/_config.yml b/docs/_config.yml index 6f3a6f7..c3ab84c 100644 --- a/docs/_config.yml +++ b/docs/_config.yml @@ -6,27 +6,24 @@ baseurl: "/amp" layouts_dir: ".shared/layout" includes_dir: ".shared/includes" -exclude: ["Gemfile", "Gemfile.lock", "README.md", "vendor"] +exclude: [ "Gemfile", "Gemfile.lock", "README.md", "vendor" ] safe: true repository: amphp/amp gems: - - "jekyll-github-metadata" - - "jekyll-relative-links" + - "jekyll-github-metadata" + - "jekyll-relative-links" defaults: - - scope: - path: "" - type: "pages" - values: - layout: "docs" + - scope: + path: "" + type: "pages" + values: + layout: "docs" shared_asset_path: "/amp/asset" navigation: - - event-loop - - promises - - coroutines - - iterators - - cancellation - - utils + - promises + - coroutines + - cancellation diff --git a/docs/event-loop/README.md b/docs/event-loop/README.md deleted file mode 100644 index fa1ce57..0000000 --- a/docs/event-loop/README.md +++ /dev/null @@ -1,115 +0,0 @@ ---- -layout: docs -title: Event Loop -permalink: /event-loop/ ---- -It may surprise people to learn that the PHP standard library already has everything we need to write event-driven and non-blocking applications. We only reach the limits of native PHP's functionality in this area when we ask it to poll thousands of file descriptors for IO activity at the same time. Even in this case, though, the fault is not with PHP but the underlying system `select()` call which is linear in its performance degradation as load increases. - -For performance that scales out to high volume we require more advanced capabilities currently found only in extensions. If you wish to, for example, service 10,000 simultaneous clients in an Amp-backed socket server, you should use one of the event loop implementations based on a PHP extension. However, if you're using Amp in a strictly local program for non-blocking concurrency or you don't need to handle more than a few hundred simultaneous clients in a server application the native PHP functionality should be adequate. - -## Global Accessor - -Amp uses a global accessor for the event loop as there's only one event loop for each application. It doesn't make sense to have two loops running at the same time, as they would just have to schedule each other in a busy waiting manner to operate correctly. - -The event loop should be accessed through the methods provided by `Amp\Loop`. On the first use of the accessor, Amp will automatically setup the best available driver, see next section. - -`Amp\Loop::set()` can be used to set a custom driver or to reset the driver in tests, as each test should run with a fresh driver instance to achieve test isolation. In case of PHPUnit, you can use a [`TestListener` to reset the event loop](https://github.com/amphp/phpunit-util) automatically after each tests. - -## Implementations - -Amp offers different event loop implementations based on various backends. All implementations extend `Amp\Loop\Driver`. Each behaves exactly the same way from an external API perspective. The main differences have to do with underlying performance characteristics. The current implementations are listed here: - -| Class | Extension | Repository | -| ------------------------- | ------------------------------------------------------ | ---------- | -| `Amp\Loop\NativeDriver` | – | - | -| `Amp\Loop\EvDriver` | [`pecl/ev`](https://pecl.php.net/package/ev) | [`php-ev`](https://bitbucket.org/osmanov/pecl-ev) | -| `Amp\Loop\EventDriver` | [`pecl/event`](https://pecl.php.net/package/event) | [`pecl-event`](https://bitbucket.org/osmanov/pecl-event) | -| `Amp\Loop\UvDriver` | [`pecl/uv`](https://pecl.php.net/package/uv) | [`php-uv`](https://github.com/bwoebi/php-uv) | - -It's not important to choose one implementation for your application. Amp will automatically select the best available driver. It's perfectly fine to have one of the extensions in production while relying on the `NativeDriver` locally for development. - -If you want to quickly switch implementations during development, e.g. for comparison or testing, you can set the `AMP_LOOP_DRIVER` environment variable to one of the classes. If you use a custom implementation, this only works if the implementation doesn't take any arguments. - -## Event Loop as Task Scheduler - -The first thing we need to understand to program effectively using an event loop is this: - -**The event loop is our task scheduler.** - -The event loop controls the program flow as long as it runs. Once we tell the event loop to run it will maintain control until the application errors out, has nothing left to do, or is explicitly stopped. - -Consider this very simple example: - -```php - This is an advanced low-level API. Most users should use [`amphp/byte-stream`](https://github.com/amphp/byte-stream) instead. - -Watchers registered via `Loop::onReadable()` trigger their callbacks in the following situations: - - - When data is available to read on the stream under observation - - When the stream is at EOF (for sockets, this means the connection is broken) - -A common usage pattern for reacting to readable data looks something like this example: - -```php - You should always read a multiple of the configured chunk size (default: 8192), otherwise your code might not work as expected with loop backends other than `stream_select()`, see [amphp/amp#65](https://github.com/amphp/amp/issues/65) for more information. - -### `onWritable()` - -{:.note} -> This is an advanced low-level API. Most users should use [`amphp/byte-stream`](https://github.com/amphp/byte-stream) instead. - - - Streams are essentially *"always"* writable. The only time they aren't is when their respective write buffers are full. - -A common usage pattern for reacting to writability involves initializing a writability watcher without enabling it when a client first connects to a server. Once incomplete writes occur we're then able to "unpause" the write watcher using `Loop::enable()` until data is fully sent without having to create and cancel new watcher resources on the same stream multiple times. - -## Pausing, Resuming and Canceling Watchers - -All watchers, regardless of type, can be temporarily disabled and enabled in addition to being cleared via `Loop::cancel()`. This allows for advanced capabilities such as disabling the acceptance of new socket clients in server applications when simultaneity limits are reached. In general, the performance characteristics of watcher reuse via pause/resume are favorable by comparison to repeatedly canceling and re-registering watchers. - -### `disable()` - -A simple disable example: - -```php -socket = $sock; - $readWatcher = Loop::onReadable($sock, function () use ($client) { - $this->onReadable($client); - }); - $writeWatcher = Loop::onWritable($sock, function () use ($client) { - $this->doWrite($client); - }); - - Loop::disable($writeWatcher); // <-- let's initialize the watcher as "disabled" - - $client->readWatcher = $readWatcher; - $client->writeWatcher = $writeWatcher; - - $this->clients[$socketId] = $client; - } - - // ... other class implementation details here ... - - private function writeToClient($client, $data) - { - $client->writeBuffer .= $data; - $this->doWrite($client); - } - - private function doWrite(ClientStruct $client) - { - $bytesToWrite = strlen($client->writeBuffer); - $bytesWritten = @fwrite($client->socket, $client->writeBuffer); - - if ($bytesToWrite === $bytesWritten) { - Loop::disable($client->writeWatcher); - } elseif ($bytesWritten >= 0) { - $client->writeBuffer = substr($client->writeBuffer, $bytesWritten); - Loop::enable($client->writeWatcher); - } elseif ($this->isSocketDead($client->socket)) { - $this->unloadClient($client); - } - } - - // ... other class implementation details here ... -} -``` - -### `cancel()` - -It's important to *always* cancel persistent watchers once you're finished with them or you'll create memory leaks in your application. This functionality works in exactly the same way as the above `enable` / `disable` examples: - -```php -= 3) { - Loop::cancel($watcherId); // <-- cancel myself! - } -}); -``` - -It is also always safe to cancel a watcher from multiple places. A double-cancel will simply be ignored. - -### An Important Note on Writability - -Because streams are essentially *"always"* writable you should only enable writability watchers while you have data to send. If you leave these watchers enabled when your application doesn't have anything to write the watcher will trigger endlessly until disabled or canceled. This will max out your CPU. If you're seeing inexplicably high CPU usage in your application it's a good bet you've got a writability watcher that you failed to disable or cancel after you were finished with it. - -A standard pattern in this area is to initialize writability watchers in a disabled state before subsequently enabling them at a later time as shown here: - -```php -advance()) { - $element = $iterator->getCurrent(); - // do something with $element -} -``` - -## Iterator Creation - -### Emitter - -What `Deferred` is for promises, is `Emitter` for iterators. A library that returns an `Iterator` for asynchronous consumption of an iterable result creates an `Amp\Emitter` and returns the `Iterator` using `iterate()`. This ensures a consumer can only consume the iterator, but not emit values or complete the iterator. - -#### `emit()` - -`emit()` emits a new value to the `Iterator`, which can be consumed by a consumer. The emitted value is passed as first argument to `emit()`. `emit()` returns a `Promise` that can be waited on before emitting new values. This allow emitting values just as fast as the consumer can consume them. - -#### `complete()` - -`complete()` marks the `Emitter` / linked `Iterator` as complete. No further emits are allowed after completing an `Emitter` / `Iterator`. - -### Producer - -`Producer` is a simplified form of `Emitter` that can be used when a single coroutine can emit all values. - -`Producer` accepts a `callable` as first constructor parameter that gets run as a coroutine and passed an `$emit` callable that can be used to emit values just like the `emit()` method does in `Emitter`. - -#### Example - -```php -$iterator = new Producer(function (callable $emit) { - yield $emit(1); - yield $emit(new Delayed(500, 2)); - yield $emit(3); - yield $emit(4); -}); -``` - -### `fromIterable` - -Iterators can also be created from ordinary PHP arrays or `Traversable` instances, which is mainly useful in tests, but might also be used for the same reasons as `Success` and `Failure`. - -```php -function fromIterable($iterable, int $delay = 0) { ... } -``` - -`$delay` allows adding a delay between each emission. diff --git a/docs/iterators/combinators.md b/docs/iterators/combinators.md deleted file mode 100644 index 175c2b2..0000000 --- a/docs/iterators/combinators.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -layout: docs -title: Iterator Combination -permalink: /iterators/combinators ---- -Amp provides two common combination helpers for iterators: `concat` and `merge`. - -## `concat()` - -`concat()` accepts an array of `Iterator` instances and concatenates the given iterators into a single iterator, emitting values from a single iterator at a time. The prior iterator must complete before values are emitted from any subsequent iterators. Iterators are concatenated in the order given (iteration order of the array). - -## `merge()` - -`merge()` accepts an array of `Iterator` instances and creates an `Iterator` that emits values emitted from any iterator in the array of iterators ending once all emitters completed. diff --git a/docs/iterators/transformation.md b/docs/iterators/transformation.md deleted file mode 100644 index c1d9f74..0000000 --- a/docs/iterators/transformation.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -layout: docs -title: Iterator Transformation -permalink: /iterators/transformation ---- -Amp provides some common transformation helpers for iterators: `map`, `filter` and `toArray`. - -Further primitives are very easy to implement using `Producer` with these as examples. - -## `map()` - -`map()` accepts an `Iterator` and a `callable` `$onEmit` that can transform each value into another value. - -## `filter()` - -`filter()` accepts an `Iterator` and a `callable` `$filter`. If `$filter($value)` returns `false` the value gets filtered, otherwise the value is retained in the resulting `Iterator`. - -## `toArray()` - -`toArray()` accepts an `Iterator` and returns a `Promise` which resolves to an array of all the items from the iterator. diff --git a/docs/utils/README.md b/docs/utils/README.md deleted file mode 100644 index 10788ff..0000000 --- a/docs/utils/README.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -layout: docs -title: Utils -permalink: /utils/ ---- -This documentation section deals with helpers that are not async specific, but generic helpers. - - * [`CallableMaker`](./callable-maker.md) - * [`Struct`](./struct.md) - -Further utils for PHPUnit are provided in [`amphp/phpunit-util`](https://github.com/amphp/phpunit-util). diff --git a/docs/utils/callable-maker.md b/docs/utils/callable-maker.md deleted file mode 100644 index 7940644..0000000 --- a/docs/utils/callable-maker.md +++ /dev/null @@ -1,16 +0,0 @@ ---- -layout: docs -title: CallableMaker -permalink: /utils/callable-maker ---- -`Amp\CallableMaker` is a helper trait that allows creating closures from private / protected static and instance methods in an easy way. Creating such callables might be necessary to register private / protected methods as callbacks in an efficient manner without making those methods public. - -This trait should only be used in projects with a PHP 7.0 minimum requirement. If PHP 7.1 or later are the minimum requirement, `Closure::fromCallable` should be used directly. - -## `callableFromInstanceMethod()` - -Creates a `Closure` form an instance method with the given name and returns it. The closure can be passed around without worrying about the method's visibility. - -## `callableFromStaticMethod()` - -Same as `callableFromInstanceMethod()`, but for static methods. diff --git a/docs/utils/struct.md b/docs/utils/struct.md deleted file mode 100644 index 1f8cd6a..0000000 --- a/docs/utils/struct.md +++ /dev/null @@ -1,47 +0,0 @@ ---- -layout: docs -title: Struct -permalink: /utils/struct ---- -A struct is a generic computer science term for an object composed of public properties. The `\Amp\Struct` trait -is intended to make using public properties a little safer by throwing an `\Error` when undefined properties -are attempted to be written or read. - -PHP allows for dynamic creation of public properties. This can lead to insidious bugs created by typos related to -writing to and reading from public properties. One common solution to this problem is to make all properties private and -provide public setter and getter methods which control access to the underlying properties. However effective this -solution may be, it requires that additional code be written and subsequently tested for the setter and getter methods. - -Let's try some examples with anonymous classes to demonstrate the advantages of using the `\Amp\Struct` trait. Running -the following code will not error; although, the typo will likely create some unexpected behavior: - -```php -$obj = new class { - public $foo = null; -}; - -$obj->fooo = "bar"; -``` - -If you were to access the `$foo` property of the `$obj` object after the above code, you might expect the value -to be `"bar"` when it would actually be `NULL`. - -When a class uses the `\Amp\Struct` trait, an `\Error` will be thrown when attempting to access a property not defined -in the class definition. For example, the code below will throw an `\Error` with some context that attempts to help -diagnose the issue. - -```php -$obj = new class { - use Amp\Struct; - - public $foo = null; -}; - -$obj->fooo = "bar"; -``` - -The message for the thrown `\Error` will be similar to: -*Uncaught Error: class@anonymous@php shell code0x10ee8005b property "fooo" does not exist ... did you mean "foo?"* - -Although, an `\Error` being thrown in your code may cause some havoc, it will not allow for unpredictable -behavior caused by the use of properties which are not part of the class definition.