Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions net/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,36 +266,46 @@ class DefaultResolver : public Resolver {
struct IPAddrList : public intrusive_list<IPAddrNode>, spinlock {
~IPAddrList() { delete_all(); }
};
struct ResolveCtx {
std::string host;
Delegate<bool, IPAddr> filter;
spinlock lock;
IPAddrList *addrs;
photon::semaphore sem;
};
IPAddr do_resolve(std::string_view host, Delegate<bool, IPAddr> filter) {
auto ctr = [&]() -> IPAddrList* {
auto addrs = new IPAddrList();
photon::semaphore sem;
std::thread([&]() {
auto now = std::chrono::steady_clock::now();
IPAddrList ret;
std::unique_ptr<IPAddrList> addrs(new IPAddrList());
auto ctx = std::make_shared<ResolveCtx>();
ctx->addrs = addrs.get();
ctx->host = std::string(host);
ctx->filter = filter;
std::thread([ctx]() {
auto cb = [&](IPAddr addr) -> int {
if (filter && !filter.fire(addr))
return 0;
ret.push_back(new IPAddrNode(addr));
SCOPED_LOCK(ctx->lock);
if (ctx->filter && !ctx->filter.fire(addr)) return 0;
if (ctx->addrs) {
ctx->addrs->push_back(new IPAddrNode(addr));
}
return 0;
};
_gethostbyname(host, cb);
auto time_elapsed = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - now).count();
if ((uint64_t)time_elapsed <= resolve_timeout_) {
addrs->push_back(std::move(ret));
sem.signal(1);
} else {
LOG_ERROR("resolve timeout");
while(!ret.empty())
delete ret.pop_front();
}
_gethostbyname(ctx->host, cb);
ctx->sem.signal(1);
}).detach();
sem.wait(1, resolve_timeout_);
return addrs;
ctx->sem.wait(1, resolve_timeout_);
SCOPED_LOCK(ctx->lock);
ctx->addrs = nullptr;
ctx->filter = {};
if (addrs->empty()) {
return nullptr;
}
return addrs.release();
};
auto ips = dnscache_.borrow(host, ctr);
if (ips->empty()) LOG_ERRNO_RETURN(0, IPAddr(), "Domain resolution for '`' failed", host);
auto ips = dnscache_.borrow(host, ctr, 1UL * 1000);
if (!ips || ips->empty()) {
ips.recycle(true);
LOG_ERRNO_RETURN(0, IPAddr(), "Domain resolution for '`' failed", host);
}
SCOPED_LOCK(*ips);
auto ret = ips->front();
ips->node = ret->next(); // access in round robin order
Expand Down
11 changes: 11 additions & 0 deletions thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ namespace photon
thread* pop_front()
{
auto ret = q[0];
if (q.size() == 1) {
q.pop_back();
return ret;
}
q[0] = q.back();
q[0]->idx = 0;
q.pop_back();
Expand All @@ -402,6 +406,11 @@ namespace photon
}

auto id = obj->idx;
if (q.size() == 1) {
assert(id == 0);
q.pop_back();
return 0;
}
q[obj->idx] = q.back();
q[id]->idx = id;
q.pop_back();
Expand All @@ -419,6 +428,7 @@ namespace photon
// compare m_nodes[idx] with parent node.
bool up(int idx)
{
assert(!q.empty());
auto tmp = q[idx];
bool ret = false;
while (idx != 0){
Expand All @@ -438,6 +448,7 @@ namespace photon
// compare m_nodes[idx] with child node.
bool down(int idx)
{
assert(!q.empty());
auto tmp = q[idx];
size_t cmpIdx = (idx << 1) + 1;
bool ret = false;
Expand Down