maxwell-utils 0.11.1

Maxwell utils implementation for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
use std::{
  cell::{Cell, RefCell},
  format,
  future::Future,
  rc::Rc,
  sync::atomic::{AtomicU32, Ordering},
  time::Duration,
};

use actix::{prelude::*, Addr};
use anyhow::Error as AnyError;
use fastwebsockets::{
  handshake, CloseCode, FragmentCollectorRead, Frame, OpCode, Payload, WebSocketError,
  WebSocketWrite,
};
use futures_intrusive::sync::LocalManualResetEvent;
use hyper::{
  header::{CONNECTION, UPGRADE},
  upgrade::Upgraded,
  Body, Request as HyperRequest,
};
use maxwell_protocol::{self, HandleError, ProtocolMsg, *};
use tokio::{
  io::{split as tokio_split, ReadHalf, WriteHalf},
  net::TcpStream,
  task::spawn as tokio_spawn,
  time::{sleep, timeout},
};

use super::*;
use crate::arbiter_pool::ArbiterPool;

static ID_SEED: AtomicU32 = AtomicU32::new(0);

// Tie hyper's executor to tokio runtime
struct SpawnExecutor;
impl<Fut> hyper::rt::Executor<Fut> for SpawnExecutor
where
  Fut: Future + Send + 'static,
  Fut::Output: Send + 'static,
{
  fn execute(&self, fut: Fut) {
    tokio_spawn(fut);
  }
}

type Sink = WebSocketWrite<WriteHalf<Upgraded>>;
type Stream = FragmentCollectorRead<ReadHalf<Upgraded>>;

pub trait EventHandler: Send + Sync + Unpin + Sized + 'static {
  #[inline(always)]
  fn on_msg(&self, _msg: ProtocolMsg) {}
  #[inline(always)]
  fn on_connected(&self, _addr: Addr<CallbackStyleConnection<Self>>) {}
  #[inline(always)]
  fn on_disconnected(&self, _addr: Addr<CallbackStyleConnection<Self>>) {}
  #[inline(always)]
  fn on_stopped(&self, _addr: Addr<CallbackStyleConnection<Self>>) {}
}

struct CallbackStyleConnectionInner<EH: EventHandler> {
  id: u32,
  addr: RefCell<Option<Addr<CallbackStyleConnection<EH>>>>,
  endpoint: String,
  options: ConnectionOptions,
  sink: RefCell<Option<Sink>>,
  stream: RefCell<Option<Stream>>,
  connected_event: LocalManualResetEvent,
  disconnected_event: LocalManualResetEvent,
  is_connected: Cell<bool>,
  msg_ref: Cell<u32>,
  event_handler: EH,
  is_stopping: Cell<bool>,
}

impl<EH: EventHandler> CallbackStyleConnectionInner<EH> {
  #[inline]
  pub fn new(endpoint: String, options: ConnectionOptions, event_handler: EH) -> Self {
    CallbackStyleConnectionInner {
      id: ID_SEED.fetch_add(1, Ordering::Relaxed),
      addr: RefCell::new(None),
      endpoint,
      options,
      sink: RefCell::new(None),
      stream: RefCell::new(None),
      connected_event: LocalManualResetEvent::new(false),
      disconnected_event: LocalManualResetEvent::new(true),
      is_connected: Cell::new(false),
      msg_ref: Cell::new(1),
      event_handler,
      is_stopping: Cell::new(false),
    }
  }

  pub async fn connect_repeatedly(self: Rc<Self>) {
    loop {
      if self.is_stopping() {
        break;
      }

      self.disconnected_event.wait().await;

      self.close_sink().await.unwrap_or_else(|err| {
        log::error!("Failed to close sink: actor: {}<{}>, err: {}", &self.endpoint, &self.id, err);
      });

      log::info!("Connecting: actor: {}<{}>", &self.endpoint, &self.id);
      match self.connect().await {
        Ok((sink, stream)) => {
          log::info!("Connected: actor: {}<{}>", &self.endpoint, &self.id);
          self.set_socket_pair(Some(sink), Some(stream));
          self.toggle_to_connected();
        }
        Err(err) => {
          log::error!("Failed to connect: actor: {}<{}>, err: {}", &self.endpoint, &self.id, err);
          self.set_socket_pair(None, None);
          self.toggle_to_disconnected();
          sleep(Duration::from_millis(self.options.reconnect_delay as u64)).await;
        }
      }
    }
  }

  #[inline]
  pub async fn send(
    self: Rc<Self>, mut msg: ProtocolMsg,
  ) -> Result<ProtocolMsg, HandleError<ProtocolMsg>> {
    let mut msg_ref = maxwell_protocol::get_ref(&msg);
    if msg_ref == 0 {
      msg_ref = self.next_msg_ref();
      maxwell_protocol::set_ref(&mut msg, msg_ref);
    } else {
      self.try_set_msg_ref(msg_ref);
    }

    if !self.is_connected() {
      for i in 0..3 {
        if let Err(_) =
          timeout(Duration::from_millis(i * 500 + 500), self.connected_event.wait()).await
        {
          continue;
        } else {
          break;
        }
      }
      if !self.is_connected() {
        let desc = format!("Timeout to send msg: actor: {}<{}>", &self.endpoint, &self.id);
        log::error!("{:?}", desc);
        return Err(HandleError::Any { code: 1, desc, msg });
      }
    }

    if let Err(err) = self
      .sink
      .borrow_mut()
      .as_mut()
      .unwrap()
      .write_frame(Frame::binary(encode(&msg).as_ref().into()))
      .await
    {
      let desc =
        format!("Failed to send msg: actor: {}<{}>, err: {}", &self.endpoint, &self.id, &err);
      log::error!("{:?}", desc);
      log::warn!(
        "The connection maybe broken, try to reconnect: actor: {}<{}>",
        &self.endpoint,
        &self.id
      );
      self.toggle_to_disconnected();
      return Err(HandleError::Any { code: 2, desc, msg });
    }

    Ok(ProtocolMsg::None)
  }

  pub async fn receive_repeatedly(self: Rc<Self>) {
    loop {
      if self.is_stopping() {
        break;
      }

      if !self.is_connected() {
        self.connected_event.wait().await;
      }

      match self
        .stream
        .borrow_mut()
        .as_mut()
        .unwrap()
        // send_fn is empty because we do not create obligated writes here.
        .read_frame(&mut move |_| async { Ok::<_, WebSocketError>(()) })
        .await
      {
        Ok(frame) => match frame.opcode {
          OpCode::Ping => {}
          OpCode::Pong => {}
          OpCode::Binary => {
            self.event_handler.on_msg(decode_bytes(&frame.payload).unwrap());
          }
          OpCode::Close => {
            log::error!(
              "Disconnected: actor: {}<{}>, reason: {}",
              &self.endpoint,
              &self.id,
              Self::stringify(&frame.payload)
            );
            self.toggle_to_disconnected();
          }
          other => {
            log::warn!("Received unknown msg: {:?}({:?})", &frame.payload, &other);
          }
        },
        Err(err) => {
          log::error!("Protocol error occured: err: {}", &err);
          self.toggle_to_disconnected();
        }
      }
    }
  }

  #[inline]
  pub fn stop(&self) {
    self.is_stopping.set(true);
    self.notify_stopped_event();
  }

  #[inline]
  fn notify_connected_event(&self) {
    let addr = self.addr.borrow();
    self.event_handler.on_connected(addr.as_ref().unwrap().clone());
  }

  #[inline]
  fn notify_disconnected_event(&self) {
    let addr = self.addr.borrow();
    self.event_handler.on_disconnected(addr.as_ref().unwrap().clone());
  }

  #[inline]
  fn notify_stopped_event(&self) {
    let addr = self.addr.borrow();
    self.event_handler.on_stopped(addr.as_ref().unwrap().clone());
  }

  #[inline]
  pub fn next_msg_ref(&self) -> u32 {
    let prev_msg_ref = self.msg_ref.get();
    if prev_msg_ref < MAX_MSG_REF {
      let curr_msg_ref = prev_msg_ref + 1;
      self.msg_ref.set(curr_msg_ref);
      curr_msg_ref
    } else {
      self.msg_ref.set(1);
      1
    }
  }

  #[inline]
  fn try_set_msg_ref(&self, msg_ref: u32) {
    if msg_ref > self.msg_ref.get() {
      self.msg_ref.set(msg_ref);
    }
  }

  #[inline]
  fn stringify(payload: &Payload) -> String {
    match Self::decode_error_payload(payload) {
      Ok(code) => format!("{:?}({})", code, u16::from(code)),
      Err(err) => format!("{:?}", err),
    }
  }

  #[inline]
  fn decode_error_payload(payload: &Payload) -> Result<CloseCode, WebSocketError> {
    match payload.len() {
      0 => Ok(CloseCode::Normal),
      1 => return Err(WebSocketError::InvalidCloseFrame),
      _ => {
        let code = CloseCode::from(u16::from_be_bytes(payload[0..2].try_into().unwrap()));
        if !code.is_allowed() {
          return Err(WebSocketError::InvalidCloseCode);
        }
        Ok(code)
      }
    }
  }

  #[inline]
  async fn connect(&self) -> Result<(Sink, Stream), AnyError> {
    let stream = TcpStream::connect(&self.endpoint).await?;
    let req = HyperRequest::builder()
      .method("GET")
      .uri("/$ws")
      .header("Host", &self.endpoint)
      .header(UPGRADE, "websocket")
      .header(CONNECTION, "upgrade")
      .header("CLIENT-ID", &format!("{}", self.id))
      .header("Sec-WebSocket-Key", handshake::generate_key())
      .header("Sec-WebSocket-Version", "13")
      .body(Body::empty())?;

    let (mut ws, _) = handshake::client(&SpawnExecutor, req, stream).await?;
    ws.set_auto_close(false);
    ws.set_auto_pong(false);
    ws.set_max_message_size(self.options.max_frame_size as usize);
    let (stream, sink) = ws.split(|s| tokio_split(s));
    Ok((sink, FragmentCollectorRead::new(stream)))
  }

  #[inline]
  fn set_socket_pair(&self, sink: Option<Sink>, stream: Option<Stream>) {
    *self.sink.borrow_mut() = sink;
    *self.stream.borrow_mut() = stream;
  }

  #[inline]
  fn toggle_to_connected(&self) {
    self.is_connected.set(true);
    self.connected_event.set();
    self.disconnected_event.reset();
    self.notify_connected_event();
  }

  #[inline]
  fn toggle_to_disconnected(&self) {
    self.is_connected.set(false);
    self.connected_event.reset();
    self.disconnected_event.set();
    self.notify_disconnected_event();
  }

  #[inline]
  async fn close_sink(&self) -> Result<(), AnyError> {
    if let Some(sink) = self.sink.try_borrow_mut()?.as_mut() {
      Ok(sink.write_frame(Frame::close_raw(vec![].into())).await?)
    } else {
      Ok(())
    }
  }

  #[inline]
  fn is_connected(&self) -> bool {
    self.is_connected.get()
  }

  #[inline]
  fn is_stopping(&self) -> bool {
    self.is_stopping.get()
  }
}

pub struct CallbackStyleConnection<EH: EventHandler> {
  inner: Rc<CallbackStyleConnectionInner<EH>>,
}
impl<EH: EventHandler> Connection for CallbackStyleConnection<EH> {}

impl<EH: EventHandler> Actor for CallbackStyleConnection<EH> {
  type Context = Context<Self>;

  #[inline]
  fn started(&mut self, ctx: &mut Self::Context) {
    log::info!("Started: actor: {}<{}>", &self.inner.endpoint, &self.inner.id);
    *self.inner.addr.borrow_mut() = Some(ctx.address());

    ctx.set_mailbox_capacity(self.inner.options.mailbox_capacity as usize);

    Box::pin(Rc::clone(&self.inner).connect_repeatedly().into_actor(self)).spawn(ctx);
    Box::pin(Rc::clone(&self.inner).receive_repeatedly().into_actor(self)).spawn(ctx);
  }

  #[inline]
  fn stopping(&mut self, _: &mut Self::Context) -> Running {
    log::info!("Stopping: actor: {}<{}>", &self.inner.endpoint, &self.inner.id);
    self.inner.stop();
    Running::Stop
  }

  #[inline]
  fn stopped(&mut self, _: &mut Self::Context) {
    log::info!("Stopped: actor: {}<{}>", &self.inner.endpoint, &self.inner.id);
  }
}

impl<EH: EventHandler> Handler<ProtocolMsg> for CallbackStyleConnection<EH> {
  type Result = ResponseFuture<Result<ProtocolMsg, HandleError<ProtocolMsg>>>;

  #[inline]
  fn handle(&mut self, msg: ProtocolMsg, _ctx: &mut Context<Self>) -> Self::Result {
    Box::pin(Rc::clone(&self.inner).send(msg))
  }
}

impl<EH: EventHandler> Handler<StopMsg> for CallbackStyleConnection<EH> {
  type Result = ();

  #[inline]
  fn handle(&mut self, _msg: StopMsg, ctx: &mut Context<Self>) -> Self::Result {
    log::info!("Received StopMsg: actor: {}<{}>", &self.inner.endpoint, &self.inner.id);
    ctx.stop();
  }
}

impl<EH: EventHandler> Handler<DumpInfoMsg> for CallbackStyleConnection<EH> {
  type Result = ();

  #[inline]
  fn handle(&mut self, _msg: DumpInfoMsg, _ctx: &mut Context<Self>) -> Self::Result {
    log::info!("Connection info: id: {:?}, endpoint: {:?}", self.inner.id, self.inner.endpoint);
  }
}

impl<EH: EventHandler> CallbackStyleConnection<EH> {
  #[inline]
  pub fn new(endpoint: String, options: ConnectionOptions, event_handler: EH) -> Self {
    CallbackStyleConnection {
      inner: Rc::new(CallbackStyleConnectionInner::new(endpoint, options, event_handler)),
    }
  }

  #[inline]
  pub fn start3(endpoint: String, options: ConnectionOptions, event_handler: EH) -> Addr<Self> {
    CallbackStyleConnection::start_in_arbiter(
      &ArbiterPool::singleton().fetch_arbiter(),
      move |_ctx| CallbackStyleConnection::new(endpoint, options, event_handler),
    )
  }

  #[inline]
  pub fn start4(
    endpoint: String, options: ConnectionOptions, arbiter: ArbiterHandle, event_handler: EH,
  ) -> Addr<Self> {
    CallbackStyleConnection::start_in_arbiter(&arbiter, move |_ctx| {
      CallbackStyleConnection::new(endpoint, options, event_handler)
    })
  }

  #[inline]
  pub async fn stop(addr: Addr<Self>) -> Result<(), HandleError<StopMsg>> {
    match addr.send(StopMsg).await {
      Ok(ok) => Ok(ok),
      Err(err) => match err {
        MailboxError::Closed => Err(HandleError::MailboxClosed),
        MailboxError::Timeout => Err(HandleError::Timeout),
      },
    }
  }

  pub fn dump_info(addr: Addr<Self>) {
    addr.do_send(DumpInfoMsg);
  }
}

////////////////////////////////////////////////////////////////////////////////
/// test cases
////////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
  use std::time::Duration;

  use actix::prelude::*;
  use futures_util::future::join_all;
  use maxwell_protocol::IntoEnum;

  use crate::connection::*;

  struct EventHandler;
  impl super::EventHandler for EventHandler {
    fn on_msg(&self, msg: maxwell_protocol::ProtocolMsg) {
      println!("Received msg: {:?}", msg);
    }
  }

  #[actix::test]
  async fn test_send_msg() {
    let conn = CallbackStyleConnection::<EventHandler>::new(
      String::from("localhost:8081"),
      ConnectionOptions::default(),
      EventHandler,
    )
    .start();
    for _ in 1..2 {
      let msg = maxwell_protocol::PingReq { r#ref: 0 }.into_enum();
      let res = conn.send(msg).timeout_ext(Duration::from_millis(3000)).await;
      println!("received result: {:?}", res);
    }
  }

  #[actix::test]
  async fn test_concurrent() {
    let conn = CallbackStyleConnection::<EventHandler>::new(
      String::from("localhost:8081"),
      ConnectionOptions::default(),
      EventHandler,
    )
    .start();

    // Spawn n threads.
    let threads: Vec<_> = (0..16_u8)
      .map(|thread_id| {
        let conn = conn.clone();
        tokio::spawn(async move {
          println!("Thread {} started.", thread_id);
          for _ in 0..10000 {
            let req = maxwell_protocol::PingReq { r#ref: 0 }.into_enum();
            let res = conn.send(req).timeout_ext(Duration::from_millis(3000)).await;
            println!("received result: res: {:?}, thread_id {:?}", res, thread_id);
          }
        })
      })
      .collect();

    join_all(threads).await;
  }
}