|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is a part of the Phystrix library |
| 4 | + * |
| 5 | + * Copyright 2013-2014 oDesk Corporation. All Rights Reserved. |
| 6 | + * |
| 7 | + * This file is licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +namespace Odesk\Phystrix; |
| 20 | + |
| 21 | +/** |
| 22 | + * APCU cache driven storage for Circuit Breaker metrics statistics |
| 23 | + */ |
| 24 | +class ApcuStateStorage implements StateStorageInterface |
| 25 | +{ |
| 26 | + const BUCKET_EXPIRE_SECONDS = 120; |
| 27 | + |
| 28 | + const CACHE_PREFIX = 'phystrix_cb_'; |
| 29 | + |
| 30 | + const OPENED_NAME = 'opened'; |
| 31 | + |
| 32 | + const SINGLE_TEST_BLOCKED = 'single_test_blocked'; |
| 33 | + |
| 34 | + /** |
| 35 | + * Constructor |
| 36 | + * @throws Exception\ApcNotLoadedException |
| 37 | + */ |
| 38 | + public function __construct() |
| 39 | + { |
| 40 | + if (!extension_loaded('apcu')) { |
| 41 | + throw new Exception\ApcNotLoadedException('"apcu" PHP extension is required for Phystrix to work'); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Prepends cache prefix and filters out invalid characters |
| 47 | + * |
| 48 | + * @param string $name |
| 49 | + * @return string |
| 50 | + */ |
| 51 | + protected function prefix($name) |
| 52 | + { |
| 53 | + return self::CACHE_PREFIX . $name; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns counter value for the given bucket |
| 58 | + * |
| 59 | + * @param string $commandKey |
| 60 | + * @param string $type |
| 61 | + * @param integer $index |
| 62 | + * @return integer |
| 63 | + */ |
| 64 | + public function getBucket($commandKey, $type, $index) |
| 65 | + { |
| 66 | + $bucketName = $this->prefix($commandKey . '_' . $type . '_' . $index); |
| 67 | + return apcu_fetch($bucketName); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Increments counter value for the given bucket |
| 72 | + * |
| 73 | + * @param string $commandKey |
| 74 | + * @param string $type |
| 75 | + * @param integer $index |
| 76 | + */ |
| 77 | + public function incrementBucket($commandKey, $type, $index) |
| 78 | + { |
| 79 | + $bucketName = $this->prefix($commandKey . '_' . $type . '_' . $index); |
| 80 | + if (!apcu_add($bucketName, 1, self::BUCKET_EXPIRE_SECONDS)) { |
| 81 | + apcu_inc($bucketName); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * If the given bucket is found, sets counter value to 0. |
| 87 | + * |
| 88 | + * @param string $commandKey Circuit breaker key |
| 89 | + * @param string $type |
| 90 | + * @param integer $index |
| 91 | + */ |
| 92 | + public function resetBucket($commandKey, $type, $index) |
| 93 | + { |
| 94 | + $bucketName = $this->prefix($commandKey . '_' . $type . '_' . $index); |
| 95 | + if (apcu_exists($bucketName)) { |
| 96 | + apcu_store($bucketName, 0, self::BUCKET_EXPIRE_SECONDS); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Marks the given circuit as open |
| 102 | + * |
| 103 | + * @param string $commandKey Circuit key |
| 104 | + * @param integer $sleepingWindowInMilliseconds In how much time we should allow a single test |
| 105 | + */ |
| 106 | + public function openCircuit($commandKey, $sleepingWindowInMilliseconds) |
| 107 | + { |
| 108 | + $openedKey = $this->prefix($commandKey . self::OPENED_NAME); |
| 109 | + $singleTestFlagKey = $this->prefix($commandKey . self::SINGLE_TEST_BLOCKED); |
| 110 | + |
| 111 | + apcu_store($openedKey, true); |
| 112 | + // the single test blocked flag will expire automatically in $sleepingWindowInMilliseconds |
| 113 | + // thus allowing us a single test. Notice, APC doesn't allow us to use |
| 114 | + // expire time less than a second. |
| 115 | + $sleepingWindowInSeconds = ceil($sleepingWindowInMilliseconds / 1000); |
| 116 | + apcu_add($singleTestFlagKey, true, $sleepingWindowInSeconds); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Whether a single test is allowed |
| 121 | + * |
| 122 | + * @param string $commandKey Circuit breaker key |
| 123 | + * @param integer $sleepingWindowInMilliseconds In how much time we should allow the next single test |
| 124 | + * @return boolean |
| 125 | + */ |
| 126 | + public function allowSingleTest($commandKey, $sleepingWindowInMilliseconds) |
| 127 | + { |
| 128 | + $singleTestFlagKey = $this->prefix($commandKey . self::SINGLE_TEST_BLOCKED); |
| 129 | + // using 'add' enforces thread safety. |
| 130 | + $sleepingWindowInSeconds = ceil($sleepingWindowInMilliseconds / 1000); |
| 131 | + // another APCU limitation is that within the current request variables will never expire. |
| 132 | + return (boolean) apcu_add($singleTestFlagKey, true, $sleepingWindowInSeconds); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Whether a circuit is open |
| 137 | + * |
| 138 | + * @param string $commandKey Circuit breaker key |
| 139 | + * @return boolean |
| 140 | + */ |
| 141 | + public function isCircuitOpen($commandKey) |
| 142 | + { |
| 143 | + $openedKey = $this->prefix($commandKey . self::OPENED_NAME); |
| 144 | + return (boolean) apcu_fetch($openedKey); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Marks the given circuit as closed |
| 149 | + * |
| 150 | + * @param string $commandKey Circuit key |
| 151 | + */ |
| 152 | + public function closeCircuit($commandKey) |
| 153 | + { |
| 154 | + $openedKey = $this->prefix($commandKey . self::OPENED_NAME); |
| 155 | + apcu_store($openedKey, false); |
| 156 | + } |
| 157 | +} |
0 commit comments