mPrefix = "{$chname}-{$quality}-{$card_type}-{$amount}"; $this->mName = $chname; $this->mCardType = $card_type; $this->mAmount = $amount; $this->mQuality = $quality; $this->mMaxSpeed = $max_speed; $this->mPriority = $priority; $this->mStorge = $storge; $this->mOpened = $opened; $this->mRecords = []; $this->mPrice = 0.0; $this->mCommitSucc = 0; $this->mCommitFail = 0; $this->mNotifySucc = 0; $this->mNotifyFail = 0; $this->mLazySpeed = 0; $this->mSleepTime = 0; $this->mSleeping = false; $this->load(time() - ctl_item::speed_secs + 1); } private function load($start_time) { $end = time(); for ($time = $start_time; $time <= $end; $time++) { $val = util::hget_commit_pre_sec($this->mName, $this->mCardType, $this->mAmount, $this->mQuality, $time); $this->mRecords[$time] = $val; } $this->mLastReadTime = $end; $min = $end - ctl_item::speed_secs + 1; $rmkeys = []; foreach ($this->mRecords as $time => $val) { if ($time < $min) { $rmkeys[] = $time; } } foreach ($rmkeys as $key) { unset($this->mRecords[$key]); } } public function cur_speed(): int { $this->load($this->mLastReadTime); $speed = array_sum($this->mRecords); //Log::record("{$this->mPrefix} speed = {$speed} max_speed={$this->mMaxSpeed}", Log::DEBUG); return $speed; } public function speed_overload(): bool { if ($this->mMaxSpeed < 0) { return false; } elseif ($this->mMaxSpeed == 0) { return true; } else { return $this->lazy_speed() >= $this->mMaxSpeed; } } public function calc_speed() { $this->mLazySpeed = $this->cur_speed(); } public function lazy_speed() { return $this->mLazySpeed; } public function name() { return $this->mName; } public function priority() { return $this->mPriority; } public function storge() { return $this->mStorge; } public function opened() { return $this->mOpened; } public function max_speed() { return $this->mMaxSpeed; } public function quality() { return $this->mQuality; } public function spec() { return $this->mAmount; } public function card_type() { return $this->mCardType; } public function set_price($price) { $this->mPrice = $price; } public function price() { return $this->mPrice; } public function set_ratio($ratio) { $this->mCommitSucc = $ratio[0]; $this->mCommitFail = $ratio[1]; $this->mNotifySucc = $ratio[2]; $this->mNotifyFail = $ratio[3]; } public function commit_statics() { $count = $this->mCommitSucc + $this->mCommitFail; return [$count, $this->mCommitSucc]; } public function notify_statics() { $count = $this->mNotifySucc + $this->mNotifyFail; return [$count, $this->mNotifySucc]; } public function notify_ratio() { $count = $this->mNotifySucc + $this->mNotifyFail; if ($count > 0) { return [$count, $this->mNotifySucc / $count]; } else { return [$count, 0]; } } public function sleeping() { return [$this->mSleeping, $this->mSleepTime]; } public function sleep() { $this->mSleeping = true; $this->mSleepTime = time(); } public function wakeup() { $this->mSleeping = false; $this->mSleepTime = 0; } }