Skip to content

Commit

Permalink
Merge pull request #91 from gearbox-solutions/feature/add-a-way-to-ad…
Browse files Browse the repository at this point in the history
…just-timeout-of-requests

Add setTimeout method to FileMakerConnection
  • Loading branch information
likeadeckofcards authored Nov 18, 2024
2 parents 1fe2cdb + 81338b0 commit 20270ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ You may use the following code block below as a template, which has some good de
'protocol' => env('DB_PROTOCOL', 'https'),
'cache_session_token' => env('DB_CACHE_SESSION_TOKEN', true), // set to false to log out after each reqeust. This can be slower than re-using a session token, but allows for globals to be set for individual user values.
'empty_strings_to_null' => env('DB_EMPTY_STRINGS_TO_NULL', true), // set to false to return empty strings instead of null values when fields are empty in FileMaker
'request_timeout' => env('DB_REQUEST_TIMEOUT', 30), // set the request timeout in seconds (default 30)
]
```
You should add one database connection configuration for each FileMaker database you will be connecting to. Each file can have completely different configurations, and can even be on different servers.
Expand Down Expand Up @@ -389,6 +390,12 @@ FM::setGlobalFields() // not chainable
->getLayoutMetadata()
```
#### Request customization methods
```php
->setRetries($retries) // set the number of retries for a request (value of 2 will make 3 requests in total)
->setTimeout($timeout) // set the timeout for a request in seconds
```
#### Examples:
Perform a find for a person named Jaina
```php
Expand Down
28 changes: 20 additions & 8 deletions src/Services/FileMakerConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,33 @@ class FileMakerConnection extends Connection

protected int $attempts = 2;

protected int $timeout = 30;

protected bool $shouldCacheSessionToken = true;

protected ?string $sessionTokenCacheKey = null;

protected bool $emptyStringToNull = true;

/**
* Crazy high number of records to return.
* Used to get an empty set when using a whereIn with no values.
*/
public const CRAZY_RECORDS_AMOUNT = 1000000000000000000;

public function __construct($pdo, $database = '', $tablePrefix = '', array $config = [])
{

$this->emptyStringToNull = $config['empty_strings_to_null'] ?? true;
$this->shouldCacheSessionToken = $config['cache_session_token'] ?? true;

// set the session cache key with the name of the connection to support multiple connections
$this->sessionTokenCacheKey = 'eloquent-filemaker-session-token-' . $config['name'];

$this->setTimeout($config['request_timeout'] ?? 30);

parent::__construct($pdo, $database, $tablePrefix, $config);
}

/**
* Crazy high number of records to return.
* Used to get an empty set when using a whereIn with no values.
*/
public const CRAZY_RECORDS_AMOUNT = 1000000000000000000;

/**
* @param string $layout
* @return $this
Expand Down Expand Up @@ -717,7 +720,9 @@ protected function prepareRequestForSending($request = null)
}
}

$request->retry($this->attempts, 100, fn () => true, false)->withToken($this->sessionToken);
$request->timeout($this->timeout)
->retry($this->attempts, 100, fn () => true, false)
->withToken($this->sessionToken);

return $request;
}
Expand Down Expand Up @@ -850,6 +855,13 @@ public function setRetries($retries)
return $this;
}

public function setTimeout($timeout)
{
$this->timeout = $timeout;

return $this;
}

protected function getDefaultQueryGrammar()
{
return new FMGrammar;
Expand Down

0 comments on commit 20270ef

Please sign in to comment.