提交 93c9367e authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi

Minor refactoring

上级 6b0acc44
...@@ -3,7 +3,7 @@ use crate::worker::common::{EventHandlers, SubscriptionTarget, WeakEventHandlers ...@@ -3,7 +3,7 @@ use crate::worker::common::{EventHandlers, SubscriptionTarget, WeakEventHandlers
use crate::worker::{RequestError, SubscriptionHandler}; use crate::worker::{RequestError, SubscriptionHandler};
use async_executor::Executor; use async_executor::Executor;
use async_fs::File; use async_fs::File;
use bytes::{BufMut, Bytes, BytesMut}; use bytes::Bytes;
use futures_lite::io::BufReader; use futures_lite::io::BufReader;
use futures_lite::{future, AsyncReadExt, AsyncWriteExt}; use futures_lite::{future, AsyncReadExt, AsyncWriteExt};
use log::{debug, trace, warn}; use log::{debug, trace, warn};
...@@ -372,19 +372,14 @@ impl Channel { ...@@ -372,19 +372,14 @@ impl Channel {
debug!("request() [method:{}, id:{}]: {}", method, id, message); debug!("request() [method:{}, id:{}]: {}", method, id, message);
let bytes = { let bytes = Bytes::from(
let mut bytes = BytesMut::new().writer(); serde_json::to_vec(&RequestMessagePrivate {
serde_json::to_writer(
&mut bytes,
&RequestMessagePrivate {
id, id,
method, method,
message, message,
}, })
) .unwrap(),
.unwrap(); );
bytes.into_inner()
};
if bytes.len() > NS_PAYLOAD_MAX_LEN { if bytes.len() > NS_PAYLOAD_MAX_LEN {
self.inner self.inner
...@@ -399,7 +394,7 @@ impl Channel { ...@@ -399,7 +394,7 @@ impl Channel {
self.inner self.inner
.sender .sender
.send(bytes.freeze()) .send(bytes)
.await .await
.map_err(|_| RequestError::ChannelClosed {})?; .map_err(|_| RequestError::ChannelClosed {})?;
......
...@@ -3,7 +3,7 @@ use crate::worker::common::{EventHandlers, SubscriptionTarget, WeakEventHandlers ...@@ -3,7 +3,7 @@ use crate::worker::common::{EventHandlers, SubscriptionTarget, WeakEventHandlers
use crate::worker::{RequestError, SubscriptionHandler}; use crate::worker::{RequestError, SubscriptionHandler};
use async_executor::Executor; use async_executor::Executor;
use async_fs::File; use async_fs::File;
use bytes::{BufMut, Bytes, BytesMut}; use bytes::Bytes;
use futures_lite::io::BufReader; use futures_lite::io::BufReader;
use futures_lite::{future, AsyncReadExt, AsyncWriteExt}; use futures_lite::{future, AsyncReadExt, AsyncWriteExt};
use log::{debug, error, trace, warn}; use log::{debug, error, trace, warn};
...@@ -366,19 +366,14 @@ impl PayloadChannel { ...@@ -366,19 +366,14 @@ impl PayloadChannel {
debug!("request() [method:{}, id:{}]: {}", method, id, message); debug!("request() [method:{}, id:{}]: {}", method, id, message);
let bytes = { let bytes = Bytes::from(
let mut bytes = BytesMut::new().writer(); serde_json::to_vec(&RequestMessagePrivate {
serde_json::to_writer(
&mut bytes,
&RequestMessagePrivate {
id, id,
method, method,
message, message,
}, })
) .unwrap(),
.unwrap(); );
bytes.into_inner()
};
if bytes.len() > NS_PAYLOAD_MAX_LEN { if bytes.len() > NS_PAYLOAD_MAX_LEN {
self.inner self.inner
...@@ -405,7 +400,7 @@ impl PayloadChannel { ...@@ -405,7 +400,7 @@ impl PayloadChannel {
self.inner self.inner
.sender .sender
.send(MessageWithPayload { .send(MessageWithPayload {
message: bytes.freeze(), message: bytes,
payload, payload,
}) })
.await .await
...@@ -465,18 +460,13 @@ impl PayloadChannel { ...@@ -465,18 +460,13 @@ impl PayloadChannel {
return Err(NotificationError::PayloadTooLong); return Err(NotificationError::PayloadTooLong);
} }
let bytes = { let bytes = Bytes::from(
let mut bytes = BytesMut::new().writer(); serde_json::to_vec(&NotificationMessagePrivate {
serde_json::to_writer(
&mut bytes,
&NotificationMessagePrivate {
event, event,
notification, notification,
}, })
) .unwrap(),
.unwrap(); );
bytes.into_inner()
};
if bytes.len() > NS_PAYLOAD_MAX_LEN { if bytes.len() > NS_PAYLOAD_MAX_LEN {
return Err(NotificationError::MessageTooLong); return Err(NotificationError::MessageTooLong);
...@@ -485,7 +475,7 @@ impl PayloadChannel { ...@@ -485,7 +475,7 @@ impl PayloadChannel {
self.inner self.inner
.sender .sender
.send(MessageWithPayload { .send(MessageWithPayload {
message: bytes.freeze(), message: bytes,
payload, payload,
}) })
.await .await
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论