Skip to content

Commit 551f104

Browse files
authored
Merge pull request #9 from eplusminus/eb/stop-on-401
stop trying to do HTTP after we get a 401
2 parents e90344f + 15fd0ef commit 551f104

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

src/LaunchDarkly/EventPublisher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ public function __construct($sdkKey, array $options);
1818
* @return bool Whether the events were successfully published
1919
*/
2020
public function publish($payload);
21-
}
21+
}

src/LaunchDarkly/GuzzleEventPublisher.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,19 @@ function __construct($sdkKey, array $options = array()) {
4747

4848
public function publish($payload) {
4949
$client = new Client(['base_uri' => $this->_eventsUri]);
50+
$response = null;
5051

5152
try {
5253
$options = $this->_requestOptions;
5354
$options['body'] = $payload;
5455
$response = $client->request('POST', '/bulk', $options);
55-
56-
return $response->getStatusCode() < 300;
5756
} catch (\Exception $e) {
5857
$this->_logger->warning("GuzzleEventPublisher::publish caught $e");
5958
return false;
6059
}
60+
if ($response && ($response->getStatusCode() == 401)) {
61+
throw new InvalidSDKKeyException();
62+
}
63+
return $response && ($response->getStatusCode() < 300);
6164
}
6265
}

src/LaunchDarkly/GuzzleFeatureRequester.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function get($key)
6565
if ($code == 404) {
6666
$this->_logger->warning("GuzzleFeatureRequester::get returned 404. Feature flag does not exist for key: " . $key);
6767
} else {
68-
$this->_logger->error("GuzzleFeatureRequester::get received an unexpected HTTP status code $code");
68+
$this->handleUnexpectedStatus($code, "GuzzleFeatureRequester::get");
6969
}
7070
return null;
7171
}
@@ -83,9 +83,15 @@ public function getAll() {
8383
$body = $response->getBody();
8484
return array_map(FeatureFlag::getDecoder(), json_decode($body, true));
8585
} catch (BadResponseException $e) {
86-
$code = $e->getResponse()->getStatusCode();
87-
$this->_logger->error("GuzzleFeatureRequester::getAll received an unexpected HTTP status code $code");
86+
$this->handleUnexpectedStatus($e->getResponse()->getStatusCode(), "GuzzleFeatureRequester::getAll");
8887
return null;
8988
}
9089
}
90+
91+
private function handleUnexpectedStatus($code, $method) {
92+
$this->_logger->error("$method received an unexpected HTTP status code $code");
93+
if ($code == 401) {
94+
throw new InvalidSDKKeyException();
95+
}
96+
}
9197
}

src/LaunchDarkly/LDClient.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
use Monolog\Logger;
66
use Psr\Log\LoggerInterface;
77

8+
/**
9+
* Used internally.
10+
*/
11+
class InvalidSDKKeyException extends \Exception
12+
{
13+
}
14+
815
/**
916
* A client for the LaunchDarkly API.
1017
*/
@@ -144,7 +151,12 @@ public function variation($key, $user, $default = false) {
144151
if ($user->isKeyBlank()) {
145152
$this->_logger->warning("User key is blank. Flag evaluation will proceed, but the user will not be stored in LaunchDarkly.");
146153
}
147-
$flag = $this->_featureRequester->get($key);
154+
try {
155+
$flag = $this->_featureRequester->get($key);
156+
} catch (InvalidSDKKeyException $e) {
157+
$this->handleInvalidSDKKey();
158+
return $default;
159+
}
148160

149161
if (is_null($flag)) {
150162
$this->_sendFlagRequestEvent($key, $user, $default, $default);
@@ -252,7 +264,15 @@ public function allFlags($user) {
252264
$this->_logger->warn("allFlags called with null user or null/empty user key! Returning null");
253265
return null;
254266
}
255-
$flags = $this->_featureRequester->getAll();
267+
if ($this->isOffline()) {
268+
return null;
269+
}
270+
try {
271+
$flags = $this->_featureRequester->getAll();
272+
} catch (InvalidSDKKeyException $e) {
273+
$this->handleInvalidSDKKey();
274+
return null;
275+
}
256276
if ($flags === null) {
257277
return null;
258278
}
@@ -285,7 +305,11 @@ public function secureModeHash($user) {
285305
*/
286306
public function flush()
287307
{
288-
return $this->_eventProcessor->flush();
308+
try {
309+
return $this->_eventProcessor->flush();
310+
} catch (InvalidSDKKeyException $e) {
311+
$this->handleInvalidSDKKey();
312+
}
289313
}
290314

291315
/**
@@ -310,4 +334,9 @@ protected function _get_default($key, $default) {
310334
return $default;
311335
}
312336
}
337+
338+
protected function handleInvalidSDKKey() {
339+
$this->_logger->error("Received 401 error, no further HTTP requests will be made during lifetime of LDClient since SDK key is invalid");
340+
$this->_offline = true;
341+
}
313342
}

0 commit comments

Comments
 (0)