From 98c97dc2bf7e7d934064620be4697f75fc043049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Mejio?= Date: Mon, 29 Jun 2020 22:50:05 +0200 Subject: [PATCH 1/2] Add timeout support for CURL operations. --- .env.dist | 7 ++- .github/scripts/decrypt_env_test.sh | 3 -- .github/secrets/.env.asc | 9 ---- .github/workflows/php.yml | 39 --------------- examples/SmsGatewayByRemoteServer.php | 35 +++++++++++++ src/Base.php | 71 ++++++++++++++++++++++----- src/Sms/SmsGateway.php | 29 +++++++++-- 7 files changed, 124 insertions(+), 69 deletions(-) delete mode 100755 .github/scripts/decrypt_env_test.sh delete mode 100644 .github/secrets/.env.asc delete mode 100644 .github/workflows/php.yml create mode 100644 examples/SmsGatewayByRemoteServer.php diff --git a/.env.dist b/.env.dist index 5b7bee0..07deab8 100644 --- a/.env.dist +++ b/.env.dist @@ -10,4 +10,9 @@ REMOTE_CONTROL_PASSWORD= SLAVE_NAME= # Form: server:port, e.g.: localhost:11211 # Leave blank to disable "locking" functionality -MEMCACHED_LOCK_STORE= \ No newline at end of file +MEMCACHED_LOCK_STORE= +# Timeout for network operations +CONNECT_TIMEOUT=5000 +TIMEOUT=60000 +TEST_ADDRESSEE=22741 +TEST_MESSAGE=R \ No newline at end of file diff --git a/.github/scripts/decrypt_env_test.sh b/.github/scripts/decrypt_env_test.sh deleted file mode 100755 index 63ce44f..0000000 --- a/.github/scripts/decrypt_env_test.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -gpg --quiet --batch --yes --decrypt --passphrase="$ENV_PASSPHRASE" --output ./.env.test ./.github/secrets/.env.asc \ No newline at end of file diff --git a/.github/secrets/.env.asc b/.github/secrets/.env.asc deleted file mode 100644 index 52e29bd..0000000 --- a/.github/secrets/.env.asc +++ /dev/null @@ -1,9 +0,0 @@ ------BEGIN PGP MESSAGE----- - -jA0ECQMKspbuXoZx/V//0sAXAQOc7wvjW8FNuuKZLnkHERc4idLLAU7Wm64qYiRi -o5LbvuEEX1HuZj/TSVueY6FYit/LGAqbZitNJjv1U0XKsnu6xum44Pup2uNAAPCN -JYgsbgZ9LxyfZxmucyiXE5pWXUspx9alRcD4ax4rsFCNHqKYNSZPSz8cyVDakT8N -QgTb1gDNDcd4uZ74nGJj9YWzM6Gm14fAAZ8DDrXkPDYM2JoZ5tc593mB5pvqc3wy -7iU2sUTlvVYpVgO+8bd9J5I90t0nkK18GCTeVVV9rDYnx3c3VNIMZI0= -=Af1T ------END PGP MESSAGE----- diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml deleted file mode 100644 index 5b856e1..0000000 --- a/.github/workflows/php.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: PHP Composer - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - - name: Validate composer.json and composer.lock - run: composer validate - - - name: Cache Composer packages - id: composer-cache - uses: actions/cache@v2 - with: - path: vendor - key: ${{ runner.os }}-node-${{ hashFiles('**/composer.lock') }} - restore-keys: | - ${{ runner.os }}-node- - - - name: Install dependencies - if: steps.composer-cache.outputs.cache-hit != 'true' - run: composer install --prefer-dist --no-progress --no-suggest - - - name: Decrypt test env file - run: ./.github/scripts/decrypt_env_test.sh - env: - ENV_PASSPHRASE: ${{ secrets.ENV_PASSPHRASE }} - - - name: Run test suite - run: composer run-script test diff --git a/examples/SmsGatewayByRemoteServer.php b/examples/SmsGatewayByRemoteServer.php new file mode 100644 index 0000000..8814ddc --- /dev/null +++ b/examples/SmsGatewayByRemoteServer.php @@ -0,0 +1,35 @@ +findSlave($_ENV['SLAVE_NAME']); + +// Create the SMS gateway +$slaveGateway = $slave->getSmsGateway( + $_ENV['GOIP_CLIENT_USERNAME'], + $_ENV['GOIP_CLIENT_PASSWORD'] +) + // And set the timeout configuration + ->setConnectTimeout(intval($_ENV['CONNECT_TIMEOUT'])) + ->setTimeout(intval($_ENV['TIMEOUT'])) +; + +// Make a ping request +$sms = $slaveGateway->sendSmsAndWaitResponse( + $_ENV['TEST_ADDRESSEE'], + $_ENV['TEST_MESSAGE'], + intval($_ENV['GOIP_CLIENT_DEFAULT_LINE']), + intval($_ENV['TIMEOUT']) +); + +// And dump it +dump($sms); diff --git a/src/Base.php b/src/Base.php index c47c814..06edc2e 100644 --- a/src/Base.php +++ b/src/Base.php @@ -23,17 +23,34 @@ */ class Base { + const DEFAULT_CONNECT_TIMEOUT = 5000; + const DEFAULT_TIMEOUT = 60000; + + /** + * @var int + */ + private $connectTimeout; + + /** + * @var int + */ + private $timeout; + public $goip; /** * Initialize the client connection * given the host, port, username and password * - * @param GoIp|null $goip */ - public function __construct(GoipClient $goip) - { + public function __construct( + GoipClient $goip, + int $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT, + int $timeout = self::DEFAULT_TIMEOUT + ) { $this->goip = $goip; + $this->connectTimeout = $connectTimeout; + $this->timeout = $timeout; } /** @@ -48,18 +65,26 @@ public function connect($route, array $params = [], array $data = []) $url = "http://" . $this->goip->host . '/default/en_US'; $url .= $route . '?' . http_build_query($params); $user = $this->goip->username . ":" . $this->goip->password; - $curl = curl_init(); + $curl = curl_init($url); - curl_setopt($curl, CURLOPT_URL, $url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_USERPWD, $user); - curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - curl_setopt($curl, CURLOPT_PORT, $this->goip->port); + curl_setopt_array( + $curl, + [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_USERPWD => $user, + CURLOPT_HTTPAUTH => CURLAUTH_BASIC, + CURLOPT_PORT => $this->goip->port, + CURLOPT_CONNECTTIMEOUT_MS => $this->connectTimeout, + CURLOPT_TIMEOUT_MS => $this->timeout, + ] + ); if ($data) { - curl_setopt($curl, CURLOPT_POST, count($data)); - curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']); - curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); + curl_setopt_array($curl, [ + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => http_build_query($data), + CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'], + ]); } $results = curl_exec($curl); @@ -67,4 +92,26 @@ public function connect($route, array $params = [], array $data = []) return $results; } + + public function getConnectTimeout(): int + { + return $this->connectTimeout; + } + + public function setConnectTimeout(int $connectTimeout): Base + { + $this->connectTimeout = $connectTimeout; + return $this; + } + + public function getTimeout(): int + { + return $this->timeout; + } + + public function setTimeout(int $timeout): Base + { + $this->timeout = $timeout; + return $this; + } } diff --git a/src/Sms/SmsGateway.php b/src/Sms/SmsGateway.php index 9f08d98..8d0601d 100644 --- a/src/Sms/SmsGateway.php +++ b/src/Sms/SmsGateway.php @@ -20,6 +20,8 @@ class SmsGateway * Time in seconds to check the response for a sent SMS. */ const CHECK_RESPONSE_INTERVAL = 1; + const DEFAULT_CONNECT_TIMEOUT = 5000; + const DEFAULT_TIMEOUT = 60000; /** * @var \GoIP\Sms @@ -31,9 +33,14 @@ class SmsGateway */ private $lockStore; - public function __construct(\GoIP\Sms $sms) - { + public function __construct( + \GoIP\Sms $sms, + int $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT, + int $timeout = self::DEFAULT_TIMEOUT + ) { $this->sms = $sms; + $this->sms->setConnectTimeout($connectTimeout); + $this->sms->setTimeout($timeout); } /** @@ -81,7 +88,7 @@ public function getSms(int $line = 1): array * @throws \Exception Thrown when the initial SMS is not sent or when first * occurs the timeout before receiving the response. */ - public function sendSmsAndWaitResponse(string $addressee, string $message, int $line = 1, int $responseTimeout = 60): Sms + public function sendSmsAndWaitResponse(string $addressee, string $message, int $line = 1, int $responseTimeout = self::DEFAULT_TIMEOUT): Sms { $lock = null; if (null !== $this->lockStore) { @@ -117,7 +124,7 @@ public function sendSmsAndWaitResponse(string $addressee, string $message, int $ $lock->release(); } return $responseCheckMessage; - } while ($stopwatch->lap('check_response')->getDuration() > $responseTimeout); + } while ($stopwatch->lap('check_response')->getDuration() <= $responseTimeout); if (null !== $lock) { $lock->release(); @@ -128,7 +135,7 @@ public function sendSmsAndWaitResponse(string $addressee, string $message, int $ if (null !== $lock) { $lock->release(); } - throw new MissingSmsResponseException('Could not retrieve the SMS response. ' . $th->getMessage()); + throw new MissingSmsResponseException('Could not retrieve the SMS response. ' . $th->getMessage(), $th->getCode(), $th); } } @@ -142,4 +149,16 @@ public function setLockStore(MemcachedStore $lockStore): SmsGateway $this->lockStore = $lockStore; return $this; } + + public function setConnectTimeout(int $connectTimeout): SmsGateway + { + $this->sms->setConnectTimeout($connectTimeout); + return $this; + } + + public function setTimeout(int $timeout): SmsGateway + { + $this->sms->setTimeout($timeout); + return $this; + } } From fe457a572aaf37bb7f8bbccf807c3640be004987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Mejio?= Date: Mon, 29 Jun 2020 22:52:55 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Add=20Juli=C3=A1n=20Mejio=20as=20author.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/composer.json b/composer.json index 6481152..4b3c1e6 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,10 @@ { "name": "April Sacil", "email": "aprilvsacil@gmail.com" + }, + { + "name": "Julián Mejio", + "email": "julianmejio@gmail.com" } ], "require": {