1313#define PC_JSON_MISSING_PERMS -32001
1414#define PC_JSON_NOT_READY -32002
1515#define PC_BATCH_SEND_FAILED -32010
16+ // Flush partial batches if not completed within 400 ms.
17+ #define PC_FLUSH_INTERVAL (400L *PC_NSECS_IN_MSEC)
1618
1719using namespace pc ;
1820
@@ -27,7 +29,8 @@ void user::user_http::parse_content( const char *txt, size_t len )
2729user::user ()
2830: rptr_( nullptr ),
2931 sptr_( nullptr ),
30- psub_( this )
32+ psub_( this ),
33+ last_update_ts_(0 )
3134{
3235 // setup the plumbing
3336 hsvr_.ptr_ = this ;
@@ -330,27 +333,26 @@ void user::parse_get_product( uint32_t tok, uint32_t itok )
330333 add_tail ( itok );
331334}
332335
333- uint32_t user::num_pending_upds ()
336+ void user::send_pending_upds ()
334337{
335- return pending_vec_.size ();
336- }
337-
338- void user::send_pending_upds (uint32_t n)
339- {
340- if ( pending_vec_.empty () ) {
341- return ;
338+ uint32_t n_sent = 0 ;
339+ int64_t curr_ts = get_now ();
340+ if (curr_ts_ - price_upd_ts_ > PC_FLUSH_INTERVAL) {
341+ n_sent = pending_vec_.size ();
342+ } else if (pending_vec_.size () > max_batch_size_) {
343+ n_sent = max_batch_size_;
342344 }
343345
344- uint32_t n_sent = n;
345- if (pending_vec_.size () < n) {
346- n_sent = pending_vec_.size ();
346+ if (n_sent == 0 ) {
347+ return ;
347348 }
348349
349350 if ( !price::send ( pending_vec_.data (), n_sent) ) {
350351 add_error ( 0 , PC_BATCH_SEND_FAILED, " batch send failed - please check the pyth logs" );
351352 }
352353
353354 pending_vec_.erase (pending_vec_.begin (), pending_vec_.begin () + n_sent);
355+ price_upd_ts_ = curr_ts_;
354356}
355357
356358void user::parse_get_all_products ( uint32_t itok )
0 commit comments