Wiki source code of Асинхронність
Last modified by Ashterix on 2024/05/17 11:34
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{box cssClass="floatinginfobox" title="**Contents**"}} | ||
| 2 | {{toc/}} | ||
| 3 | {{/box}} | ||
| 4 | |||
| 5 | (% class="box infomessage" %) | ||
| 6 | ((( | ||
| 7 | The JsonRpcBundle library actively uses asynchronous processing for handling requests, which significantly improves API interaction efficiency and system performance. | ||
| 8 | ))) | ||
| 9 | |||
| 10 | = Asynchronous Processing in Synchronous Requests = | ||
| 11 | |||
| 12 | When a client sends an HTTP [[batch request>>doc:docs.JsonRpcBundle.functionality.batch.WebHome]] or a [[callback request>>doc:docs.JsonRpcBundle.functionality.callback.WebHome]], the server actually processes the data asynchronously using the [[Symfony Process>>https://symfony.com/doc/current/components/process.html]] mechanism. | ||
| 13 | |||
| 14 | == ##Batch Requests## == | ||
| 15 | |||
| 16 | Batch requests allow combining multiple requests into one, reducing network and server load. The server receives a single request containing multiple operations, processes them asynchronously, and returns a single response with the results of all operations. | ||
| 17 | |||
| 18 | == ##Callback Requests## == | ||
| 19 | |||
| 20 | Callback requests use the {{code language="none"}}$rpc.callback{{/code}} parameter, which contains a DSN for the callback. The server accepts the request, validates the DSN, immediately returns a confirmation response, processes the request asynchronously, and sends the result to the specified callback URL. | ||
| 21 | |||
| 22 | = Using Asynchronous Transport for RPC = | ||
| 23 | |||
| 24 | The second option involves configuring the parameter [[ {{code language="none"}}async:rpc_async{{/code}}>>https://docs.ufo-tech.space/bin/view/docs/JsonRpcBundle/config#H41143B43E43AA0-1]], which accepts a DSN for connecting to asynchronous transport such as RabbitMQ, Redis, Kafka, etc. In this case, the RPC server uses the [[Symfony Messenger>>https://symfony.com/doc/current/components/messenger.html]]. | ||
| 25 | |||
| 26 | (% class="box warningmessage" %) | ||
| 27 | ((( | ||
| 28 | This mechanism is in the early stages of implementation and does not yet have detailed mechanics described. | ||
| 29 | ))) | ||
| 30 | |||
| 31 | == ##Connecting Asynchronous Transport## == | ||
| 32 | |||
| 33 | To start the consumer, you need to run the command: | ||
| 34 | |||
| 35 | {{code language="bash"}} | ||
| 36 | php bin/console messenger:consume rpc-async | ||
| 37 | {{/code}} | ||
| 38 | |||
| 39 | This will start the RPC server as a listener for the specified transport, and it will process all RPC requests that come through this channel. | ||
| 40 | |||
| 41 | == ##Advantages of Using Asynchronous Transport## == | ||
| 42 | |||
| 43 | 1. **Scalability**: Using asynchronous transport allows easy system scaling by adding new consumers. | ||
| 44 | 2. **High Performance**: Asynchronous transport ensures fast processing of a large number of requests by distributing the load among different system components. | ||
| 45 | 3. **Reliability**: Using transport systems like RabbitMQ or Kafka guarantees reliable message delivery and request processing. | ||
| 46 | |||
| 47 | By utilizing these asynchronous capabilities, you can significantly improve your system's performance and provide a more convenient and faster data exchange between the client and the server. |