/** * Whether the circuit is open * * @return boolean */ public function isOpen() { if ($this->stateStorage->isCircuitOpen($this->commandKey)) { // if we're open we immediately return true and don't bother attempting to 'close' ourself // as that is left to allowSingleTest and a subsequent successful test to close return true; } $healthCounts = $this->metrics->getHealthCounts(); if ($healthCounts->getTotal() < $this->config->get('circuitBreaker')->get('requestVolumeThreshold')) { // we are not past the minimum volume threshold for the statistical window // so we'll return false immediately and not calculate anything return false; } $allowedErrorPercentage = $this->config->get('circuitBreaker')->get('errorThresholdPercentage'); if ($healthCounts->getErrorPercentage() < $allowedErrorPercentage) { return false; } else { $this->stateStorage->openCircuit($this->commandKey, $this->config->get('circuitBreaker')->get('sleepWindowInMilliseconds')); return true; } }
public function testGetHealthCountsExpiringSnapshot() { $now = \Odesk\Phystrix\microtime() * 1000; // current time in milliseconds // a snapshot created two seconds ago is no longer valid and we expect a new one $snapshot = new HealthCountsSnapshot($now - 2000, 11, 22); // setting it as the last snapshot into metrics $reflection = new \ReflectionClass('Odesk\\Phystrix\\CommandMetrics'); $property = $reflection->getProperty('lastSnapshot'); $property->setAccessible(true); $property->setValue($this->metrics, $snapshot); $this->counter->expects($this->exactly(2))->method('get')->will($this->returnValueMap(array(array(MetricsCounter::FAILURE, 22), array(MetricsCounter::SUCCESS, 33)))); $newSnapshot = $this->metrics->getHealthCounts(); $this->assertNotEquals($snapshot, $newSnapshot); $this->assertEquals(33, $newSnapshot->getSuccessful()); }