Skip to content

Commit 2a97edf

Browse files
committed
add option to disable ws connection to rpc node
1 parent c3578b1 commit 2a97edf

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

pc/manager.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ void manager_sub::on_add_symbol( manager *, price * )
4545
// manager
4646

4747
manager::manager()
48-
: thost_( PC_RPC_HOST ),
48+
: wconn_{ nullptr },
49+
thost_( PC_RPC_HOST ),
4950
rhost_( PC_RPC_HOST ),
5051
sub_( nullptr ),
5152
status_( 0 ),
@@ -260,7 +261,12 @@ void manager::teardown()
260261

261262
// destroy rpc connections
262263
hconn_.close();
263-
wconn_.close();
264+
if ( wconn_ ) {
265+
wconn_->close();
266+
delete wconn_;
267+
wconn_ = nullptr;
268+
clnt_.set_ws_conn( nullptr );
269+
}
264270
}
265271

266272
bool manager::init()
@@ -305,15 +311,18 @@ bool manager::init()
305311
hconn_.set_host( rhost );
306312
hconn_.set_net_loop( &nl_ );
307313
clnt_.set_http_conn( &hconn_ );
308-
wconn_.set_port( wport );
309-
wconn_.set_host( rhost );
310-
wconn_.set_net_loop( &nl_ );
311-
clnt_.set_ws_conn( &wconn_ );
314+
if ( get_do_ws() ) {
315+
wconn_ = new ws_connect{};
316+
wconn_->set_port( wport );
317+
wconn_->set_host( rhost );
318+
wconn_->set_net_loop( &nl_ );
319+
clnt_.set_ws_conn( wconn_ );
320+
}
312321
if ( !hconn_.init() ) {
313322
return set_err_msg( hconn_.get_err_msg() );
314323
}
315-
if ( !wconn_.init() ) {
316-
return set_err_msg( wconn_.get_err_msg() );
324+
if ( wconn_ && !wconn_->init() ) {
325+
return set_err_msg( wconn_->get_err_msg() );
317326
}
318327
// connect to pyth_tx server
319328
if ( do_tx_ ) {
@@ -358,7 +367,7 @@ bool manager::get_is_tx_send() const
358367

359368
bool manager::get_is_rpc_send() const
360369
{
361-
return hconn_.get_is_send() || wconn_.get_is_send();
370+
return hconn_.get_is_send() || ( wconn_ && wconn_->get_is_send() );
362371
}
363372

364373
bool manager::bootstrap()
@@ -420,7 +429,9 @@ void manager::poll( bool do_wait )
420429
} else {
421430
if ( has_status( PC_PYTH_RPC_CONNECTED ) ) {
422431
hconn_.poll();
423-
wconn_.poll();
432+
if ( wconn_ ) {
433+
wconn_->poll();
434+
}
424435
}
425436
if ( do_tx_ ) {
426437
tconn_.poll();
@@ -476,7 +487,7 @@ void manager::poll( bool do_wait )
476487
// submit new quotes while connected
477488
if ( has_status( PC_PYTH_RPC_CONNECTED ) &&
478489
!hconn_.get_is_err() &&
479-
!wconn_.get_is_err() ) {
490+
( !wconn_ || !wconn_->get_is_err() ) ) {
480491
poll_schedule();
481492
} else {
482493
reconnect_rpc();
@@ -506,15 +517,15 @@ void manager::reconnect_rpc()
506517
if ( hconn_.get_is_wait() ) {
507518
hconn_.check();
508519
}
509-
if ( wconn_.get_is_wait() ) {
510-
wconn_.check();
520+
if ( wconn_ && wconn_->get_is_wait() ) {
521+
wconn_->check();
511522
}
512-
if ( hconn_.get_is_wait() || wconn_.get_is_wait() ) {
523+
if ( hconn_.get_is_wait() || ( wconn_ && wconn_->get_is_wait() ) ) {
513524
return;
514525
}
515526

516527
// check for successful (re)connect
517-
if ( !hconn_.get_is_err() && !wconn_.get_is_err() ) {
528+
if ( !hconn_.get_is_err() && ( !wconn_ || !wconn_->get_is_err() ) ) {
518529
PC_LOG_INF( "rpc_connected" ).end();
519530
set_status( PC_PYTH_RPC_CONNECTED );
520531

@@ -613,7 +624,9 @@ void manager::reconnect_rpc()
613624
ctimeout_ = std::min( ctimeout_, PC_RECONNECT_TIMEOUT );
614625
wait_conn_ = true;
615626
hconn_.init();
616-
wconn_.init();
627+
if ( wconn_ ) {
628+
wconn_->init();
629+
}
617630
}
618631

619632
void manager::log_disconnect()
@@ -626,11 +639,11 @@ void manager::log_disconnect()
626639
.end();
627640
return;
628641
}
629-
if ( wconn_.get_is_err() ) {
642+
if ( wconn_ && wconn_->get_is_err() ) {
630643
PC_LOG_ERR( "rpc_websocket_reset" )
631-
.add( "error", wconn_.get_err_msg() )
644+
.add( "error", wconn_->get_err_msg() )
632645
.add( "host", rhost_ )
633-
.add( "port", wconn_.get_port() )
646+
.add( "port", wconn_->get_port() )
634647
.end();
635648
return;
636649
}

pc/manager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ namespace pc
215215

216216
net_loop nl_; // epoll loop
217217
tcp_connect hconn_; // rpc http connection
218-
ws_connect wconn_; // rpc websocket sonnection
218+
ws_connect *wconn_; // rpc websocket sonnection
219219
tcp_listen lsvr_; // listening socket
220220
rpc_client clnt_; // rpc api
221221
tx_connect tconn_; // tx proxy connection

pc/rpc_client.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ net_connect *rpc_client::get_http_conn() const
123123
void rpc_client::set_ws_conn( net_connect *wptr )
124124
{
125125
wptr_ = wptr;
126-
wptr_->set_net_parser( &wp_ );
126+
if ( wptr_ ) {
127+
wptr_->set_net_parser( &wp_ );
128+
}
127129
wp_.set_net_connect( wptr_ );
128130
}
129131

@@ -171,12 +173,15 @@ void rpc_client::send( rpc_request *rptr )
171173
msg.add_hdr( "Content-Type", "application/json" );
172174
msg.commit( jw );
173175
hptr_->add_send( msg );
174-
} else {
176+
} else if ( wptr_ ) {
175177
// submit websocket message
176178
ws_wtr msg;
177179
msg.commit( ws_wtr::text_id, jw, true );
178180
wptr_->add_send( msg );
179181
}
182+
else {
183+
PC_LOG_WRN( "no ws connection to send msg" ).end();
184+
}
180185
}
181186

182187
void rpc_client::rpc_http::parse_content( const char *txt, size_t len )

pcapps/pythd.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ int usage()
6262
std::cerr << " -x" << std::endl;
6363
std::cerr << " Disable connection to pyth_tx transaction proxy server"
6464
"\n" << std::endl;
65+
std::cerr << " -z" << std::endl;
66+
std::cerr << " Disable WebSocket connection to Solana RPC node"
67+
"\n" << std::endl;
6568
std::cerr << " -m <commitment_level>" << std::endl;
6669
std::cerr << " Subscription commitment level: processed, confirmed or "
6770
"finalized\n" << std::endl;
@@ -98,8 +101,8 @@ int main(int argc, char **argv)
98101
std::string tx_host = get_tx_host();
99102
int pyth_port = get_port();
100103
int opt = 0;
101-
bool do_wait = true, do_tx = true, do_debug = false;
102-
while( (opt = ::getopt(argc,argv, "r:t:p:k:w:c:l:m:dnxh" )) != -1 ) {
104+
bool do_wait = true, do_tx = true, do_ws = true, do_debug = false;
105+
while( (opt = ::getopt(argc,argv, "r:t:p:k:w:c:l:m:dnxhz" )) != -1 ) {
103106
switch(opt) {
104107
case 'r': rpc_host = optarg; break;
105108
case 't': tx_host = optarg; break;
@@ -111,6 +114,7 @@ int main(int argc, char **argv)
111114
case 'm': cmt = str_to_commitment(optarg); break;
112115
case 'n': do_wait = false; break;
113116
case 'x': do_tx = false; break;
117+
case 'z': do_ws = false; break;
114118
case 'd': do_debug = true; break;
115119
default: return usage();
116120
}
@@ -138,6 +142,7 @@ int main(int argc, char **argv)
138142
mgr.set_content_dir( cnt_dir );
139143
mgr.set_capture_file( cap_file );
140144
mgr.set_do_tx( do_tx );
145+
mgr.set_do_ws( do_ws );
141146
mgr.set_do_capture( !cap_file.empty() );
142147
mgr.set_commitment( cmt );
143148
if ( !mgr.init() ) {

0 commit comments

Comments
 (0)