-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: yusheng.ma <[email protected]>
- Loading branch information
1 parent
4534162
commit 6167ead
Showing
6 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (C) 2019-2023 Zilliz. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed under the License | ||
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
// or implied. See the License for the specific language governing permissions and limitations under the License. | ||
#ifndef KNOWHERE_INTERRUPT_H | ||
#define KNOWHERE_INTERRUPT_H | ||
#include <atomic> | ||
#include <chrono> | ||
#include <memory> | ||
|
||
#include "knowhere/expected.h" | ||
|
||
namespace folly { | ||
template <typename T> | ||
class Future; | ||
|
||
} | ||
|
||
namespace knowhere { | ||
class Interrupt { | ||
public: | ||
#ifdef KNOWHERE_WITH_CARDINAL | ||
explicit Interrupt(const std::chrono::seconds& timeout); | ||
|
||
void | ||
Stop(); | ||
|
||
bool | ||
Flag() const; | ||
|
||
bool | ||
IsTimeout() const; | ||
#else | ||
Interrupt(); | ||
#endif | ||
|
||
Status | ||
Get(); | ||
|
||
void | ||
Set(folly::Future<Status>&& future); | ||
|
||
~Interrupt(); | ||
|
||
private: | ||
#ifdef KNOWHERE_WITH_CARDINAL | ||
std::chrono::steady_clock::time_point start; | ||
std::chrono::seconds timeout; | ||
mutable std::atomic_bool flag = false; | ||
#endif | ||
std::unique_ptr<folly::Future<Status>> future = nullptr; | ||
}; | ||
} // namespace knowhere | ||
#endif /* INTERRUPT_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (C) 2019-2023 Zilliz. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed under the License | ||
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
// or implied. See the License for the specific language governing permissions and limitations under the License. | ||
|
||
#include "knowhere/index/interrupt.h" | ||
|
||
#include "folly/futures/Future.h" | ||
namespace knowhere { | ||
#ifdef KNOWHERE_WITH_CARDINAL | ||
Interrupt::Interrupt(const std::chrono::seconds& timeout) : start(std::chrono::steady_clock::now()), timeout(timeout) { | ||
} | ||
#else | ||
Interrupt::Interrupt() = default; | ||
#endif | ||
|
||
#ifdef KNOWHERE_WITH_CARDINAL | ||
void | ||
Interrupt::Stop() { | ||
this->flag.store(true); | ||
}; | ||
|
||
bool | ||
Interrupt::Flag() const { | ||
return this->flag.load(); | ||
} | ||
|
||
bool | ||
Interrupt::IsTimeout() const { | ||
auto now = std::chrono::steady_clock::now(); | ||
auto dur = std::chrono::duration_cast<std::chrono::seconds>(now - start); | ||
return dur.count() > timeout.count(); | ||
} | ||
#endif | ||
Status | ||
Interrupt::Get() { | ||
future->wait(); | ||
if (this->Flag() || this->IsTimeout()) | ||
return Status::timeout; | ||
return std::move(*future).get(); | ||
} | ||
|
||
void | ||
Interrupt::Set(folly::Future<Status>&& future) { | ||
this->future = std::make_unique<folly::Future<Status>>(std::move(future)); | ||
} | ||
|
||
Interrupt::~Interrupt() { | ||
} | ||
|
||
} // namespace knowhere |