本文整理汇总了C++中AsyncOp::timeOut_inlock方法的典型用法代码示例。如果您正苦于以下问题:C++ AsyncOp::timeOut_inlock方法的具体用法?C++ AsyncOp::timeOut_inlock怎么用?C++ AsyncOp::timeOut_inlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AsyncOp
的用法示例。
在下文中一共展示了AsyncOp::timeOut_inlock方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: startCommand
//.........这里部分代码省略.........
}
// We can't release the AsyncOp until we know we were not canceled.
auto ownedOp = conn->releaseAsyncOp();
op = ownedOp.get();
// This AsyncOp may be recycled. We expect timeout and canceled to be clean.
// If this op was most recently used to connect, its state transitions won't have been
// reset, so we do that here.
MONGO_ASIO_INVARIANT_INLOCK(!op->canceled(), "AsyncOp has dirty canceled flag", op);
MONGO_ASIO_INVARIANT_INLOCK(!op->timedOut(), "AsyncOp has dirty timeout flag", op);
op->clearStateTransitions();
// Now that we're inProgress, an external cancel can touch our op, but
// not until we release the inProgressMutex.
_inProgress.emplace(op, std::move(ownedOp));
op->_cbHandle = std::move(cbHandle);
op->_request = std::move(request);
op->_onFinish = std::move(onFinish);
op->_connectionPoolHandle = std::move(swConn.getValue());
op->startProgress(getConnectionStartTime);
// This ditches the lock and gets us onto the strand (so we're
// threadsafe)
op->_strand.post([this, op, getConnectionStartTime] {
const auto timeout = op->_request.timeout;
// Set timeout now that we have the correct request object
if (timeout != RemoteCommandRequest::kNoTimeout) {
// Subtract the time it took to get the connection from the pool from the request
// timeout.
auto getConnectionDuration = now() - getConnectionStartTime;
if (getConnectionDuration >= timeout) {
// We only assume that the request timer is guaranteed to fire *after* the
// timeout duration - but make no stronger assumption. It is thus possible that
// we have already exceeded the timeout. In this case we timeout the operation
// manually.
std::stringstream msg;
msg << "Remote command timed out while waiting to get a connection from the "
<< "pool, took " << getConnectionDuration << ", timeout was set to "
<< timeout;
auto rs = ResponseStatus(ErrorCodes::NetworkInterfaceExceededTimeLimit,
msg.str(),
getConnectionDuration);
return _completeOperation(op, rs);
}
// The above conditional guarantees that the adjusted timeout will never underflow.
MONGO_ASIO_INVARIANT(timeout > getConnectionDuration, "timeout underflowed", op);
const auto adjustedTimeout = timeout - getConnectionDuration;
const auto requestId = op->_request.id;
try {
op->_timeoutAlarm =
op->_owner->_timerFactory->make(&op->_strand, adjustedTimeout);
} catch (std::system_error& e) {
severe() << "Failed to construct timer for AsyncOp: " << e.what();
fassertFailed(40334);
}
std::shared_ptr<AsyncOp::AccessControl> access;
std::size_t generation;
{
stdx::lock_guard<stdx::mutex> lk(op->_access->mutex);
access = op->_access;
generation = access->id;
}
op->_timeoutAlarm->asyncWait(
[this, op, access, generation, requestId, adjustedTimeout](std::error_code ec) {
// We must pass a check for safe access before using op inside the
// callback or we may attempt access on an invalid pointer.
stdx::lock_guard<stdx::mutex> lk(access->mutex);
if (generation != access->id) {
// The operation has been cleaned up, do not access.
return;
}
if (!ec) {
LOG(2) << "Request " << requestId << " timed out"
<< ", adjusted timeout after getting connection from pool was "
<< adjustedTimeout << ", op was " << redact(op->toString());
op->timeOut_inlock();
} else {
LOG(2) << "Failed to time request " << requestId
<< "out: " << ec.message() << ", op was "
<< redact(op->toString());
}
});
}
_beginCommunication(op);
});
};
_connectionPool.get(request.target, request.timeout, nextStep);
return Status::OK();
}