1 /** 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "cancellation_token.h" 17 18 namespace ark::es2panda::lsp { ThrottledCancellationCheck()19bool CancellationToken::ThrottledCancellationCheck() 20 { 21 if (this->host_ == nullptr) { 22 return false; 23 } 24 25 time_t time = std::time(nullptr); 26 time_t duration = time - this->lastCancellationTime_; 27 if (duration >= this->throttleTime_) { 28 this->lastCancellationTime_ = time; 29 return this->host_->IsCancellationRequested(); 30 } 31 return false; 32 } IsCancellationRequested()33bool CancellationToken::IsCancellationRequested() 34 { 35 if (this->host_ == nullptr) { 36 return false; 37 } 38 39 this->lastCancellationTime_ = std::time(nullptr); 40 return this->host_->IsCancellationRequested(); 41 } 42 CancellationToken(time_t designatedThrottleTime,HostCancellationToken * hostCancellationToken)43CancellationToken::CancellationToken(time_t designatedThrottleTime, HostCancellationToken *hostCancellationToken) 44 { 45 this->lastCancellationTime_ = 0; 46 this->throttleTime_ = designatedThrottleTime; 47 this->host_ = hostCancellationToken; 48 } 49 } // namespace ark::es2panda::lsp 50