Download the PHP package juanparati/query-timeout without Composer
On this page you can find all versions of the php package juanparati/query-timeout. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download juanparati/query-timeout
More information about juanparati/query-timeout
Files in juanparati/query-timeout
Package query-timeout
Short Description Set a timeout to your Laravel queries.
License MIT
Informations about the package query-timeout
Query Timeout ⏰
A Laravel database library that implements query timeouts at the database level, helping you implement the circuit breaker pattern.
Compatible with the following RDBMS:
- MariaDB
- MySQL
- PostgreSQL
Installation
How it works.
Running queries
Use the QueryTimeout facade to set a maximum execution time for your queries:
or using the fluent builder:
In the previous example if the query exceeds the specified timeout (3 seconds), it will send an error to the log and throw a \Juanparati\QueryTimeout\QueryTimeoutException.
Returning direct results
Instead of passing the results as reference like in the following example:
you can also get the results directly:
Obtain execution time
RDBMS are not very accurate stopping queries, but you can get the real execution time of the query using the getQueryTime method:
The configuration key ´resolution´ defines the precision of the time measurement output.
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
- PostgreSQL: statement_timeout
The timeout mechanism works by:
- Setting the timeout value for the database session.
- Executing the query.
- Restoring the original timeout value even if the main query fails.
Limitations
MySQL-specific
- Only "select" queries are timed out in MySQL.
- Unfortunately, for pure computational queries MySQL kills the query silently without raising any error, so in this case this library determines when a query is timed out measuring the execution time and creating artificially an exception. This method may not be very accurate (See the configuration
mysql.recheck_timeoutfor changing this behavior).
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).