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
@@ -34,6 +36,7 @@ user::user()
3436 hsvr_.set_net_connect ( this );
3537 hsvr_.set_ws_parser ( this );
3638 set_net_parser ( &hsvr_ );
39+ last_upd_ts_ = get_now ();
3740}
3841
3942void user::set_rpc_client ( rpc_client *rptr )
@@ -332,15 +335,24 @@ void user::parse_get_product( uint32_t tok, uint32_t itok )
332335
333336void user::send_pending_upds ()
334337{
335- if ( pending_vec_.empty () ) {
338+ uint32_t n_to_send = 0 ;
339+ int64_t curr_ts = get_now ();
340+ if (curr_ts - last_upd_ts_ > PC_FLUSH_INTERVAL) {
341+ n_to_send = pending_vec_.size ();
342+ } else if (pending_vec_.size () >= sptr_->get_max_batch_size ()) {
343+ n_to_send = sptr_->get_max_batch_size ();
344+ }
345+
346+ if (n_to_send == 0 ) {
336347 return ;
337348 }
338349
339- if ( !price::send ( pending_vec_.data (), pending_vec_. size () ) ) {
350+ if ( !price::send ( pending_vec_.data (), n_to_send ) ) {
340351 add_error ( 0 , PC_BATCH_SEND_FAILED, " batch send failed - please check the pyth logs" );
341352 }
342353
343- pending_vec_.clear ();
354+ pending_vec_.erase (pending_vec_.begin (), pending_vec_.begin () + n_to_send);
355+ last_upd_ts_ = curr_ts;
344356}
345357
346358void user::parse_get_all_products ( uint32_t itok )
0 commit comments