Download the PHP package juanparati/laravel-timeout without Composer
On this page you can find all versions of the php package juanparati/laravel-timeout. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download juanparati/laravel-timeout
More information about juanparati/laravel-timeout
Files in juanparati/laravel-timeout
Package laravel-timeout
Short Description Set a timeout to your queries.
License MIT
Informations about the package laravel-timeout
Laravel Query Timeout ⏰
A Laravel database extension that implements query timeouts at the database level, helping you implement the circuit breaker pattern.
Note: This library only supports MariaDB and MySQL databases.
How it works.
Use the \DB::timeout
method to set a maximum execution time for your queries:
In the previous example if the query exceeds the specified timeout (3 seconds), it will be terminated and throw a \Juanparati\LaravelTimeout\QueryTimeoutException
.
How it works under the hood
Instead of using co-routines or parallel execution monitoring, this library leverages native database features:
- MariaDB: max_statement_time
- MySQL: max_execution_time
The timeout mechanism works by:
- Setting the timeout value for the database session
- Executing the query
- Restoring the original timeout value
Limitations
MySQL-specific
- Only "select" queries are timed out in MySQL.
- Unfortunately, MySQL kills the query silently without raising any error and always returns an empty result, so the way that laravel-timeout determines when a query was in reality timed out it's measuring the execution time and creating artificially an exception, so I recommend run only one single query inside the closure when MySQL is used.
MariaDB-specific
- Old MariaDB embedded servers may not work properly.
- COMMIT statements don't timeout in Galera clusters.
General Limitations:
- May be unreliable with persistent connections or connection pools in distributed environments
Best Practices
- Keep application logic outside the closure.
✅ Recommended:
❌ Not recommended:
- In MySQL, try to use only one query per closure (Be cautious with ORM operations that might trigger multiple queries).