Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setTimeout method to FileMakerConnection #91

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading