diff options
author | turret <turret@duck.com> | 2025-09-29 00:13:47 -0500 |
---|---|---|
committer | turret <turret@duck.com> | 2025-09-29 00:13:47 -0500 |
commit | 1f753f79694aba734f4f77cc69095a35fbd10873 (patch) | |
tree | b768d771f1d853ab2390ccf29aaf5f57c8eb0a8d /util/net.c | |
parent | 4e360a4ed7cd77709588b659c704b43d6d474031 (diff) | |
download | discord-bot-skeleton-1f753f79694aba734f4f77cc69095a35fbd10873.tar.gz discord-bot-skeleton-1f753f79694aba734f4f77cc69095a35fbd10873.tar.bz2 discord-bot-skeleton-1f753f79694aba734f4f77cc69095a35fbd10873.zip |
ws: add latency to ident/ready
when using api_latency during the websocket heartbeat jitter, it will
report 0.00. this resolves that by using the READY event by measuring
time from ident send
additionally, remove headers curl slist free since it would also free
the overarching slist, which isnt created by us. we can dirty it but i
would rather free it in the context that created it so its obvious that
its Not A Memory Leak:tm:
Diffstat (limited to 'util/net.c')
-rw-r--r-- | util/net.c | 12 |
1 files changed, 10 insertions, 2 deletions
@@ -137,9 +137,9 @@ int api_request(HTTPMethod method, char *url, if(token_header == NULL) setup_token_header(); struct curl_slist *headers_auth = curl_slist_append(headers, token_header); + curl_slist_append(headers_auth, "User-Agent: DiscordBot (DBS, dev)"); int ret = http_request(method, new_url, headers_auth, writebuf, response_code); free(new_url); - curl_slist_free_all(headers_auth); return ret; } @@ -190,6 +190,7 @@ static void ws_handle_event(cJSON *event) { int op = cJSON_GetObjectItem(event, "op")->valueint; char *msg; + struct timeval now; cJSON *data = cJSON_GetObjectItem(event, "d"); switch(op) { case 0: ; /* Event dispatch */ @@ -209,6 +210,13 @@ static void ws_handle_event(cJSON *event) #undef E { ev = EVENT_INVALID; } + if(ev == READY) { + gettimeofday(&now, NULL); + api_latency = (now.tv_sec - last_heartbeat_sent.tv_sec) * 1000.0f + + (now.tv_usec - last_heartbeat_sent.tv_usec) / 1000.0f; + print(LOG_DEBUG "ws: READY rcvd (latency %.4f)", api_latency); + } + int (*ev_handler)(cJSON *) = *ev_get_handler(ev); /* TODO: intercept ready event for session information */ if(ev_handler != NULL) { @@ -277,6 +285,7 @@ static void ws_handle_event(cJSON *event) msg = cJSON_PrintUnformatted(ev_payload); size_t sent; curl_ws_send(ws_handle, msg, strlen(msg), &sent, 0, CURLWS_TEXT); + gettimeofday(&last_heartbeat_sent, NULL); free(msg); cJSON_Delete(ev_payload); print(LOG_DEBUG "hello: sent IDENT"); @@ -290,7 +299,6 @@ static void ws_handle_event(cJSON *event) break; case 11: ; /* Heartbeat ACK */ // last_heartbeat_sent - struct timeval now; gettimeofday(&now, NULL); api_latency = (now.tv_sec - last_heartbeat_sent.tv_sec) * 1000.0f + (now.tv_usec - last_heartbeat_sent.tv_usec) / 1000.0f; |