Skip to content

Commit

Permalink
feat: add support for timeout in settings call
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhi591 authored and Varun Malhotra committed Nov 15, 2024
1 parent 539c9ee commit 311cfad
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.69.0] - 2024-11-15

### Added

- Implemented a timeout mechanism for the `getSettingsFile` API ensuring that the connection is automatically terminated either after the specified timeout period or, by default, after 60 seconds.
```php
use vwo\VWO;

// 10 seconds timeout
// third parameter is false by default and is true only when used after webhook.
$settingsFile = VWO::getSettingsFile(ACCOUNT_ID, SDK_KEY, false, ['timeout' => 10]);

// no timeout, default is 60 seconds
$settingsFile = VWO::getSettingsFile(ACCOUNT_ID, SDK_KEY);
```

## [1.68.0] - 2024-01-12

### Added
Expand Down
5 changes: 4 additions & 1 deletion src/HttpHandler/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public function getHeader($header)
*
* @return mixed
*/
public function get($url, $query = false)
public function get($url, $query = false, $timeout)
{
$this->initializeRequest();
if (is_array($query)) {
Expand All @@ -374,6 +374,9 @@ public function get($url, $query = false)
curl_setopt($this->curl, CURLOPT_POST, false);
curl_setopt($this->curl, CURLOPT_PUT, false);
curl_setopt($this->curl, CURLOPT_HTTPGET, true);
if ($timeout) {
curl_setopt($this->curl, CURLOPT_TIMEOUT, $timeout);
}
curl_exec($this->curl);
return $this->handleResponse();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ function __construct($isDevelopmentMode)
*
* @return boolean
*/
public function send($url, $parameters)
public function send($url, $parameters, $timeout)
{

if (self::$isDevelopmentMode) {
return false;
} else {
$connection = new Connection();

$response = $connection->get($url, $parameters);
$response = $connection->get($url, $parameters, $timeout);
}

if (isset($response['httpStatus']) && $response['httpStatus'] == 200) {
Expand Down
7 changes: 5 additions & 2 deletions src/VWO.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ function __construct($config)
* @param String|Integer $accountId
* @param String $sdkKey
* @param bool $isTriggeredByWebhook
* @param array $options
* @return bool|mixed
*/
public static function getSettingsFile($accountId, $sdkKey, $isTriggeredByWebhook = false)
public static function getSettingsFile($accountId, $sdkKey, $isTriggeredByWebhook = false, $options = [])
{
self::$apiName = 'getSettingsFile';
LoggerService::setApiName(self::$apiName);
Expand All @@ -217,14 +218,16 @@ public static function getSettingsFile($accountId, $sdkKey, $isTriggeredByWebhoo
try {
$parameters = ImpressionBuilder::getSettingsFileQueryParams($accountId, $sdkKey);
$eventDispatcher = new EventDispatcher(false);

$timeout = isset($options['timeout']) ? $options['timeout'] : 60;

if ($isTriggeredByWebhook) {
$url = UrlConstants::WEBHOOK_SETTINGS_URL;
} else {
$url = UrlConstants::SETTINGS_URL;
}

return $eventDispatcher->send($url, $parameters);
return $eventDispatcher->send($url, $parameters, $timeout);
} catch (Exception $e) {
LoggerService::log(Logger::ERROR, $e->getMessage(), [], self::CLASSNAME);
}
Expand Down

0 comments on commit 311cfad

Please sign in to comment.