7 uint_fast8_t threads_number,
9 uint_fast32_t timeout )
12 , updates_queue( 150 )
14 bots = std::vector<T>( threads_number, bot );
32 uint_fast8_t size =
bots.size();
52 T poller =
bots.back();
54 std::vector<std::thread> threads;
57 threads.push_back( std::thread( &Polling::loopBot,
this, std::move( bot ) ) );
63 setThreadAffinity( threads );
69 std::vector<cppgram::types::Update> updates;
76 if ( poller.getUpdates( updates, updates_offset, limit, timeout ) )
78 count = updates.size();
79 updates_queue.enqueue_bulk( producer_token, updates.begin(), count );
80 updates_offset += count;
85 for ( std::thread &t : threads )
96 auto bot =
bots.back();
100 std::vector<cppgram::types::Update> updates;
101 if ( bot.getUpdates( updates, updates_offset ) )
103 for (
auto update : updates )
105 bot.processUpdate( std::move( update ) );
107 updates_offset += updates.size();
117 u_int8_t cores = std::thread::hardware_concurrency();
118 if ( threads.size() <= cores )
120 for ( uint_fast8_t i = 0; i < threads.size(); i++ )
124 CPU_SET( i, &cpuset );
125 int rc = pthread_setaffinity_np(
126 threads[i].native_handle(),
sizeof( cpu_set_t ), &cpuset );
129 console_stderr->critical(
"Error calling pthread_setaffinity_np: " 130 + std::to_string( rc ) );
141 cppgram::types::Update new_update;
145 bot.processUpdate( std::move( new_update ) );
153 std::vector<cppgram::types::Update> first_update;
154 while ( !poller.getUpdates( first_update, 0, 1, timeout ) )
156 return first_update[0].update_id - 1;
163 auto sink = std::make_shared<spdlog::sinks::simple_file_sink_mt>(
"bot.log" );
165 for (
auto &bot :
bots )
167 if ( bot.logger_ptr ==
nullptr )
169 bot.setLogger( sink );
175 spdlog::set_async_mode( 8192,
176 spdlog::async_overflow_policy::block_retry,
178 std::chrono::seconds( 300 ) );
std::shared_ptr< spdlog::logger > console_stdout
Logger that will print messages to the console.
Definition: polling.hpp:94
moodycamel::BlockingConcurrentQueue< types::Update > updates_queue
Queue of the updates to process.
Definition: polling.hpp:88
void init()
Init logger for each bot.
Definition: polling_impl.hpp:161
uint_fast32_t firstUpdateID(T &poller)
Get the offset of the first update to process.
Definition: polling_impl.hpp:151
std::vector< T > bots
Vector of bots.
Definition: polling.hpp:85
Polling(T bot, uint_fast8_t threads_number, uint_fast32_t limit=100, uint_fast32_t timeout=60)
Create a polling object by passing a single bot, and the number of threads.
Definition: polling_impl.hpp:6
std::shared_ptr< spdlog::logger > console_stderr
Logger that will print error messages to the console.
Definition: polling.hpp:91
void run()
Start the polling.
Definition: polling_impl.hpp:30
Handle long (or short) polling for a bot.
Definition: polling.hpp:12