Skip to content

Commit f2d5cfd

Browse files
refactor: remove thread pool from request queue, run it off dpp::timer instances instead
1 parent c26defc commit f2d5cfd

File tree

11 files changed

+230
-206
lines changed

11 files changed

+230
-206
lines changed

include/dpp/cluster.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,11 @@ class DPP_EXPORT cluster {
272272
* @param maxclusters The total number of clusters that are active, which may be on separate processes or even separate machines.
273273
* @param compressed Whether or not to use compression for shards on this cluster. Saves a ton of bandwidth at the cost of some CPU
274274
* @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)
275-
* @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().
276-
* @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().
275+
* @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.
276+
* 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.
277277
* @throw dpp::exception Thrown on windows, if WinSock fails to initialise, or on any other system if a dpp::request_queue fails to construct
278278
*/
279-
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);
279+
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);
280280

281281
/**
282282
* @brief Place some arbitrary work into the thread pool for execution when time permits.

include/dpp/coro/awaitable.h

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct awaitable_dummy {
4444
#include <type_traits>
4545
#include <exception>
4646
#include <atomic>
47+
#include <condition_variable>
4748

4849
namespace dpp {
4950

include/dpp/discordvoiceclient.h

+25-25
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ class DPP_EXPORT discord_voice_client : public websocket_client
271271
/**
272272
* @brief Last connect time of voice session
273273
*/
274-
time_t connect_time;
274+
time_t connect_time{};
275275

276276
/*
277277
* @brief For mixing outgoing voice data.
@@ -286,12 +286,12 @@ class DPP_EXPORT discord_voice_client : public websocket_client
286286
/**
287287
* @brief Port number of UDP/RTP endpoint
288288
*/
289-
uint16_t port;
289+
uint16_t port{};
290290

291291
/**
292292
* @brief SSRC value
293293
*/
294-
uint64_t ssrc;
294+
uint64_t ssrc{};
295295

296296
/**
297297
* @brief List of supported audio encoding modes
@@ -301,7 +301,7 @@ class DPP_EXPORT discord_voice_client : public websocket_client
301301
/**
302302
* @brief Timescale in nanoseconds
303303
*/
304-
uint64_t timescale;
304+
uint64_t timescale{};
305305

306306
/**
307307
* @brief Output buffer
@@ -428,14 +428,14 @@ class DPP_EXPORT discord_voice_client : public websocket_client
428428
/**
429429
* @brief If true, audio packet sending is paused
430430
*/
431-
bool paused;
431+
bool paused{};
432432

433433
/**
434434
* @brief Whether has sent 5 frame of silence before stopping on pause.
435435
*
436436
* This is to avoid unintended Opus interpolation with subsequent transmissions.
437437
*/
438-
bool sent_stop_frames;
438+
bool sent_stop_frames{};
439439

440440
/**
441441
* @brief Number of times we have tried to reconnect in the last few seconds
@@ -451,13 +451,13 @@ class DPP_EXPORT discord_voice_client : public websocket_client
451451
/**
452452
* @brief libopus encoder
453453
*/
454-
OpusEncoder* encoder;
454+
OpusEncoder* encoder{};
455455

456456
/**
457457
* @brief libopus repacketizer
458458
* (merges frames into one packet)
459459
*/
460-
OpusRepacketizer* repacketizer;
460+
OpusRepacketizer* repacketizer{};
461461

462462
/**
463463
* @brief This holds the state information for DAVE E2EE.
@@ -499,14 +499,14 @@ class DPP_EXPORT discord_voice_client : public websocket_client
499499
/**
500500
* @brief File descriptor for UDP connection
501501
*/
502-
dpp::socket fd;
502+
dpp::socket fd{};
503503

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

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

522522
/**
523523
* @brief Last received sequence from gateway.
524524
*
525525
* Needed for heartbeat and resume payload.
526526
*/
527-
int32_t receive_sequence;
527+
int32_t receive_sequence{};
528528

529529
/**
530530
* @brief Timestamp value used in outbound audio. Each packet
531531
* has the timestamp value which is incremented to match
532532
* how many frames are sent.
533533
*/
534-
uint32_t timestamp;
534+
uint32_t timestamp{};
535535

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

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

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

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

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

592592
/**
593593
* @brief DAVE - Discord Audio Visual Encryption
@@ -708,37 +708,37 @@ class DPP_EXPORT discord_voice_client : public websocket_client
708708
/**
709709
* @brief Owning cluster
710710
*/
711-
class dpp::cluster* creator;
711+
class dpp::cluster* creator{};
712712

713713
/**
714714
* @brief True when the thread is shutting down
715715
*/
716-
bool terminating;
716+
bool terminating{};
717717

718718
/**
719719
* @brief The gain value for the end of the current voice iteration.
720720
*/
721-
float end_gain;
721+
float end_gain{};
722722

723723
/**
724724
* @brief The gain value for the current voice iteration.
725725
*/
726-
float current_gain;
726+
float current_gain{};
727727

728728
/**
729729
* @brief The amount to increment each successive sample for, for the current voice iteration.
730730
*/
731-
float increment;
731+
float increment{};
732732

733733
/**
734734
* @brief Heartbeat interval for sending heartbeat keepalive
735735
*/
736-
uint32_t heartbeat_interval;
736+
uint32_t heartbeat_interval{};
737737

738738
/**
739739
* @brief Last voice channel websocket heartbeat
740740
*/
741-
time_t last_heartbeat;
741+
time_t last_heartbeat{};
742742

743743
/**
744744
* @brief Discord voice session token

0 commit comments

Comments
 (0)