53
53
} ;
54
54
55
55
pub const PING_INTERVAL_DURATION : Duration = Duration :: from_secs ( 30 ) ;
56
+ pub const NOTIFICATIONS_CHAN_LEN : usize = 1000 ;
56
57
57
58
pub async fn ws_route_handler (
58
59
ws : WebSocketUpgrade ,
@@ -64,19 +65,14 @@ pub async fn ws_route_handler(
64
65
async fn websocket_handler ( stream : WebSocket , state : super :: State ) {
65
66
let ws_state = state. ws . clone ( ) ;
66
67
let id = ws_state. subscriber_counter . fetch_add ( 1 , Ordering :: SeqCst ) ;
67
-
68
- let ( sender, receiver) = stream. split ( ) ;
69
-
70
- // TODO: Use a configured value for the buffer size or make it const static
71
- // TODO: Use redis stream to source the updates instead of a channel
72
- let ( tx, rx) = mpsc:: channel :: < ( ) > ( 1000 ) ;
73
-
74
- ws_state. subscribers . insert ( id, tx) ;
75
-
76
68
log:: debug!( "New websocket connection, assigning id: {}" , id) ;
77
69
78
- let mut subscriber = Subscriber :: new ( id, state. store . clone ( ) , rx, receiver, sender) ;
70
+ let ( notify_sender, notify_receiver) = mpsc:: channel :: < ( ) > ( NOTIFICATIONS_CHAN_LEN ) ;
71
+ let ( sender, receiver) = stream. split ( ) ;
72
+ let mut subscriber =
73
+ Subscriber :: new ( id, state. store . clone ( ) , notify_receiver, receiver, sender) ;
79
74
75
+ ws_state. subscribers . insert ( id, notify_sender) ;
80
76
subscriber. run ( ) . await ;
81
77
}
82
78
@@ -88,7 +84,7 @@ pub struct Subscriber {
88
84
id : SubscriberId ,
89
85
closed : bool ,
90
86
store : Arc < Store > ,
91
- update_rx : mpsc:: Receiver < ( ) > ,
87
+ notify_receiver : mpsc:: Receiver < ( ) > ,
92
88
receiver : SplitStream < WebSocket > ,
93
89
sender : SplitSink < WebSocket , Message > ,
94
90
price_feeds_with_config : HashMap < PriceIdentifier , PriceFeedClientConfig > ,
@@ -100,15 +96,15 @@ impl Subscriber {
100
96
pub fn new (
101
97
id : SubscriberId ,
102
98
store : Arc < Store > ,
103
- update_rx : mpsc:: Receiver < ( ) > ,
99
+ notify_receiver : mpsc:: Receiver < ( ) > ,
104
100
receiver : SplitStream < WebSocket > ,
105
101
sender : SplitSink < WebSocket , Message > ,
106
102
) -> Self {
107
103
Self {
108
104
id,
109
105
closed : false ,
110
106
store,
111
- update_rx ,
107
+ notify_receiver ,
112
108
receiver,
113
109
sender,
114
110
price_feeds_with_config : HashMap :: new ( ) ,
@@ -128,7 +124,7 @@ impl Subscriber {
128
124
129
125
async fn handle_next ( & mut self ) -> Result < ( ) > {
130
126
tokio:: select! {
131
- maybe_update_feeds = self . update_rx . recv( ) => {
127
+ maybe_update_feeds = self . notify_receiver . recv( ) => {
132
128
if maybe_update_feeds. is_none( ) {
133
129
return Err ( anyhow!( "Update channel closed. This should never happen. Closing connection." ) ) ;
134
130
} ;
@@ -257,9 +253,7 @@ impl Subscriber {
257
253
}
258
254
}
259
255
260
- pub async fn dispatch_updates ( state : super :: State ) {
261
- let ws_state = state. ws . clone ( ) ;
262
-
256
+ pub async fn notify_updates ( ws_state : Arc < WsState > ) {
263
257
let closed_subscribers: Vec < Option < SubscriberId > > = join_all (
264
258
ws_state
265
259
. subscribers
0 commit comments