Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize AsyncEvent #5084

Merged
merged 1 commit into from
Jul 4, 2023
Merged
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
15 changes: 8 additions & 7 deletions include/swoole_async.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,12 @@ enum AsyncFlag {
};

struct AsyncEvent {
int fd;
size_t task_id;
uint8_t lock;
uint8_t canceled;
/**
* input & output
*/
uint16_t flags;
off_t offset;
size_t nbytes;
void *buf;
void *req;
void *data;
/**
* output
*/
Expand All @@ -66,6 +60,13 @@ struct AsyncEvent {
}
};

struct GethostbynameData {
const char *hostname;
int domain;
char *host;
size_t host_len;
};

class AsyncThreads {
public:
bool schedule = false;
Expand Down
23 changes: 12 additions & 11 deletions src/coroutine/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,22 @@ ssize_t System::write_file(const char *file, char *buf, size_t length, bool lock

std::string gethostbyname_impl_with_async(const std::string &hostname, int domain, double timeout) {
AsyncEvent ev{};
GethostbynameData data;
ev.data = &data;

if (hostname.size() < INET6_ADDRSTRLEN) {
ev.nbytes = INET6_ADDRSTRLEN + 1;
data.host_len = INET6_ADDRSTRLEN + 1;
} else {
ev.nbytes = hostname.size() + 1;
data.host_len = hostname.size() + 1;
}

ev.buf = sw_malloc(ev.nbytes);
if (!ev.buf) {
data.host = (char *) sw_malloc(data.host_len);
if (!data.host) {
return "";
}

memcpy(ev.buf, hostname.c_str(), hostname.size());
((char *) ev.buf)[hostname.size()] = 0;
ev.flags = domain;
data.hostname = hostname.c_str();
data.domain = domain;
ev.retval = 1;

coroutine::async(async::handler_gethostbyname, ev, timeout);
Expand All @@ -163,8 +164,8 @@ std::string gethostbyname_impl_with_async(const std::string &hostname, int domai
swoole_set_last_error(ev.error);
return "";
} else {
std::string addr((char *) ev.buf);
sw_free(ev.buf);
std::string addr(data.host);
sw_free(data.host);
return addr;
}
}
Expand Down Expand Up @@ -215,7 +216,7 @@ std::vector<std::string> System::getaddrinfo(
AsyncEvent ev{};
network::GetaddrinfoRequest req{};

ev.req = &req;
ev.data = &req;

struct sockaddr_in6 result_buffer[SW_DNS_HOST_BUFFER_SIZE];

Expand Down Expand Up @@ -639,7 +640,7 @@ bool async(async::Handler handler, AsyncEvent &event, double timeout) {
return false;
} else {
event.canceled = _ev->canceled;
event.error = errno = _ev->error;
event.error = errno = _ev->error;
event.retval = _ev->retval;
return true;
}
Expand Down
28 changes: 16 additions & 12 deletions src/network/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -615,30 +615,31 @@ static int Client_tcp_connect_async(Client *cli, const char *host, int port, dou

if (cli->wait_dns) {
AsyncEvent ev{};
auto data = (GethostbynameData *) sw_malloc(sizeof(GethostbynameData));
ev.data = data;

size_t len = strlen(cli->server_host);
if (len < INET6_ADDRSTRLEN) {
ev.nbytes = INET6_ADDRSTRLEN;
data->host_len = INET6_ADDRSTRLEN;
} else {
ev.nbytes = len + 1;
data->host_len = len + 1;
}

ev.buf = sw_malloc(ev.nbytes);
if (!ev.buf) {
data->host = (char *) sw_malloc(data->host_len);
if (!data->host) {
swoole_warning("malloc failed");
return SW_ERR;
}

memcpy(ev.buf, cli->server_host, len);
((char *) ev.buf)[len] = 0;
ev.flags = cli->_sock_domain;
data->hostname = host;
data->domain = cli->_sock_domain;
ev.object = cli;
ev.fd = cli->socket->fd;
ev.handler = async::handler_gethostbyname;
ev.callback = Client_onResolveCompleted;

if (swoole::async::dispatch(&ev) == nullptr) {
sw_free(ev.buf);
sw_free(data->host);
sw_free(data);
return SW_ERR;
} else {
return SW_OK;
Expand Down Expand Up @@ -1128,16 +1129,18 @@ static void Client_onTimeout(Timer *timer, TimerNode *tnode) {
}

static void Client_onResolveCompleted(AsyncEvent *event) {
auto data = (GethostbynameData *) event->data;
if (event->canceled) {
sw_free(event->buf);
sw_free(data->host);
sw_free(data);
return;
}

Client *cli = (Client *) event->object;
cli->wait_dns = 0;

if (event->error == 0) {
Client_tcp_connect_async(cli, (char *) event->buf, cli->server_port, cli->timeout, 1);
Client_tcp_connect_async(cli, data->host, cli->server_port, cli->timeout, 1);
} else {
swoole_set_last_error(SW_ERROR_DNSLOOKUP_RESOLVE_FAILED);
cli->socket->removed = 1;
Expand All @@ -1146,7 +1149,8 @@ static void Client_onResolveCompleted(AsyncEvent *event) {
cli->onError(cli);
}
}
sw_free(event->buf);
sw_free(data->host);
sw_free(data);
}

static int Client_onWrite(Reactor *reactor, Event *event) {
Expand Down
9 changes: 5 additions & 4 deletions src/os/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ namespace async {

void handler_gethostbyname(AsyncEvent *event) {
char addr[INET6_ADDRSTRLEN];
int ret = network::gethostbyname(event->flags, (char *) event->buf, addr);
sw_memset_zero(event->buf, event->nbytes);
auto data = (GethostbynameData *) event->data;
int ret = network::gethostbyname(data->domain, data->hostname, addr);
sw_memset_zero(data->host, data->host_len);

if (ret < 0) {
event->error = SW_ERROR_DNSLOOKUP_RESOLVE_FAILED;
} else {
if (inet_ntop(event->flags, addr, (char *) event->buf, event->nbytes) == nullptr) {
if (inet_ntop(data->domain, addr, data->host, data->host_len) == nullptr) {
ret = -1;
event->error = SW_ERROR_BAD_IPV6_ADDRESS;
} else {
Expand All @@ -103,7 +104,7 @@ void handler_gethostbyname(AsyncEvent *event) {
}

void handler_getaddrinfo(AsyncEvent *event) {
network::GetaddrinfoRequest *req = (network::GetaddrinfoRequest *) event->req;
network::GetaddrinfoRequest *req = (network::GetaddrinfoRequest *) event->data;
event->retval = network::getaddrinfo(req);
event->error = req->error;
}
Expand Down