Skip to content

Commit

Permalink
refactor: remove thread pool from request queue, run it off dpp::time…
Browse files Browse the repository at this point in the history
…r instances instead
  • Loading branch information
braindigitalis committed Nov 27, 2024
1 parent c26defc commit f2d5cfd
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 206 deletions.
6 changes: 3 additions & 3 deletions include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ class DPP_EXPORT cluster {
* @param maxclusters The total number of clusters that are active, which may be on separate processes or even separate machines.
* @param compressed Whether or not to use compression for shards on this cluster. Saves a ton of bandwidth at the cost of some CPU
* @param policy Set the caching policy for the cluster, either lazy (only cache users/members when they message the bot) or aggressive (request whole member lists on seeing new guilds too)
* @param request_threads The number of threads to allocate for making HTTP requests to Discord. This defaults to 12. You can increase this at runtime via the object returned from get_rest().
* @param request_threads_raw The number of threads to allocate for making HTTP requests to sites outside of Discord. This defaults to 1. You can increase this at runtime via the object returned from get_raw_rest().
* @param pool_threads The number of threads to allocate for the thread pool. This defaults to half your system concurrency and if set to a number less than 4, will default to 4.
* All callbacks and events are placed into the thread pool. The bigger you make this pool (but generally no bigger than your number of cores), the more your bot will scale.
* @throw dpp::exception Thrown on windows, if WinSock fails to initialise, or on any other system if a dpp::request_queue fails to construct
*/
cluster(const std::string& token, uint32_t intents = i_default_intents, uint32_t shards = 0, uint32_t cluster_id = 0, uint32_t maxclusters = 1, bool compressed = true, cache_policy_t policy = cache_policy::cpol_default, uint32_t request_threads = 12, uint32_t request_threads_raw = 1);
cluster(const std::string& token, uint32_t intents = i_default_intents, uint32_t shards = 0, uint32_t cluster_id = 0, uint32_t maxclusters = 1, bool compressed = true, cache_policy_t policy = cache_policy::cpol_default, uint32_t pool_threads = std::thread::hardware_concurrency() / 2);

/**
* @brief Place some arbitrary work into the thread pool for execution when time permits.
Expand Down
1 change: 1 addition & 0 deletions include/dpp/coro/awaitable.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct awaitable_dummy {
#include <type_traits>
#include <exception>
#include <atomic>
#include <condition_variable>

namespace dpp {

Expand Down
50 changes: 25 additions & 25 deletions include/dpp/discordvoiceclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class DPP_EXPORT discord_voice_client : public websocket_client
/**
* @brief Last connect time of voice session
*/
time_t connect_time;
time_t connect_time{};

/*
* @brief For mixing outgoing voice data.
Expand All @@ -286,12 +286,12 @@ class DPP_EXPORT discord_voice_client : public websocket_client
/**
* @brief Port number of UDP/RTP endpoint
*/
uint16_t port;
uint16_t port{};

/**
* @brief SSRC value
*/
uint64_t ssrc;
uint64_t ssrc{};

/**
* @brief List of supported audio encoding modes
Expand All @@ -301,7 +301,7 @@ class DPP_EXPORT discord_voice_client : public websocket_client
/**
* @brief Timescale in nanoseconds
*/
uint64_t timescale;
uint64_t timescale{};

/**
* @brief Output buffer
Expand Down Expand Up @@ -428,14 +428,14 @@ class DPP_EXPORT discord_voice_client : public websocket_client
/**
* @brief If true, audio packet sending is paused
*/
bool paused;
bool paused{};

/**
* @brief Whether has sent 5 frame of silence before stopping on pause.
*
* This is to avoid unintended Opus interpolation with subsequent transmissions.
*/
bool sent_stop_frames;
bool sent_stop_frames{};

/**
* @brief Number of times we have tried to reconnect in the last few seconds
Expand All @@ -451,13 +451,13 @@ class DPP_EXPORT discord_voice_client : public websocket_client
/**
* @brief libopus encoder
*/
OpusEncoder* encoder;
OpusEncoder* encoder{};

/**
* @brief libopus repacketizer
* (merges frames into one packet)
*/
OpusRepacketizer* repacketizer;
OpusRepacketizer* repacketizer{};

/**
* @brief This holds the state information for DAVE E2EE.
Expand Down Expand Up @@ -499,14 +499,14 @@ class DPP_EXPORT discord_voice_client : public websocket_client
/**
* @brief File descriptor for UDP connection
*/
dpp::socket fd;
dpp::socket fd{};

/**
* @brief Secret key for encrypting voice.
* If it has been sent, this contains a sequence of exactly 32 bytes
* (secret_key_size) and has_secret_key is set to true.
*/
std::array<uint8_t, secret_key_size> secret_key;
std::array<uint8_t, secret_key_size> secret_key{};

/**
* @brief True if the voice client has a secret key
Expand All @@ -517,21 +517,21 @@ class DPP_EXPORT discord_voice_client : public websocket_client
* @brief Sequence number of outbound audio. This is incremented
* once per frame sent.
*/
uint16_t sequence;
uint16_t sequence{};

/**
* @brief Last received sequence from gateway.
*
* Needed for heartbeat and resume payload.
*/
int32_t receive_sequence;
int32_t receive_sequence{};

/**
* @brief Timestamp value used in outbound audio. Each packet
* has the timestamp value which is incremented to match
* how many frames are sent.
*/
uint32_t timestamp;
uint32_t timestamp{};

/**
* @brief Each packet should have a nonce, a 32-bit incremental
Expand All @@ -542,7 +542,7 @@ class DPP_EXPORT discord_voice_client : public websocket_client
*
* Current initial value is hardcoded to 1.
*/
uint32_t packet_nonce;
uint32_t packet_nonce{};

/**
* @brief Last sent packet high-resolution timestamp
Expand All @@ -552,7 +552,7 @@ class DPP_EXPORT discord_voice_client : public websocket_client
/**
* @brief Fraction of the sleep that was not executed after the last audio packet was sent
*/
std::chrono::nanoseconds last_sleep_remainder;
std::chrono::nanoseconds last_sleep_remainder{};

/**
* @brief Maps receiving ssrc to user id
Expand All @@ -564,7 +564,7 @@ class DPP_EXPORT discord_voice_client : public websocket_client
* When this moves from false to true, this causes the
* client to send the 'talking' notification to the websocket.
*/
bool sending;
bool sending{};

/**
* @brief Number of track markers in the buffer. For example if there
Expand All @@ -575,7 +575,7 @@ class DPP_EXPORT discord_voice_client : public websocket_client
* If the buffer is empty, there are zero tracks in the
* buffer.
*/
uint32_t tracks;
uint32_t tracks{};

/**
* @brief Meta data associated with each track.
Expand All @@ -587,7 +587,7 @@ class DPP_EXPORT discord_voice_client : public websocket_client
/**
* @brief Encoding buffer for opus repacketizer and encode
*/
uint8_t encode_buffer[65536];
uint8_t encode_buffer[65536]{};

/**
* @brief DAVE - Discord Audio Visual Encryption
Expand Down Expand Up @@ -708,37 +708,37 @@ class DPP_EXPORT discord_voice_client : public websocket_client
/**
* @brief Owning cluster
*/
class dpp::cluster* creator;
class dpp::cluster* creator{};

/**
* @brief True when the thread is shutting down
*/
bool terminating;
bool terminating{};

/**
* @brief The gain value for the end of the current voice iteration.
*/
float end_gain;
float end_gain{};

/**
* @brief The gain value for the current voice iteration.
*/
float current_gain;
float current_gain{};

/**
* @brief The amount to increment each successive sample for, for the current voice iteration.
*/
float increment;
float increment{};

/**
* @brief Heartbeat interval for sending heartbeat keepalive
*/
uint32_t heartbeat_interval;
uint32_t heartbeat_interval{};

/**
* @brief Last voice channel websocket heartbeat
*/
time_t last_heartbeat;
time_t last_heartbeat{};

/**
* @brief Discord voice session token
Expand Down
Loading

0 comments on commit f2d5cfd

Please sign in to comment.