Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ext/net): Add Conn.setNoDelay and Conn.setKeepAlive #13103

Merged
merged 16 commits into from
Jan 31, 2022
Merged
8 changes: 8 additions & 0 deletions ext/net/01_net.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
return core.opAsync("op_dns_resolve", { query, recordType, options });
}

function opSetNoDelay(rid, noDelay) {
return core.opAsync("op_set_nodelay", { rid, noDelay });
yos1p marked this conversation as resolved.
Show resolved Hide resolved
}

class Conn {
#rid = 0;
#remoteAddr = null;
Expand Down Expand Up @@ -95,6 +99,10 @@
closeWrite() {
return shutdown(this.rid);
}

async setNoDelay(noDelay) {
return await opSetNoDelay(this.rid, noDelay);
}
}

class Listener {
Expand Down
11 changes: 11 additions & 0 deletions ext/net/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ impl Resource for TcpStreamResource {
}
}

impl TcpStreamResource {
pub async fn set_nodelay(
self: Rc<Self>,
nodelay: bool,
) -> Result<(), AnyError> {
let wr = self.wr_borrow_mut().await;
let stream = wr.as_ref().as_ref();
Ok(stream.set_nodelay(nodelay)?)
}
}

#[cfg(unix)]
pub type UnixStreamResource =
FullDuplexResource<unix::OwnedReadHalf, unix::OwnedWriteHalf>;
Expand Down
2 changes: 2 additions & 0 deletions ext/net/lib.deno_net.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ declare namespace Deno {
/** Shuts down (`shutdown(2)`) the write side of the connection. Most
* callers should just use `close()`. */
closeWrite(): Promise<void>;
/** Enable/disable the use of Nagle's algorithm. */
setNoDelay(nodelay: boolean): Promise<void>;
yos1p marked this conversation as resolved.
Show resolved Hide resolved
}

// deno-lint-ignore no-empty-interface
Expand Down
21 changes: 21 additions & 0 deletions ext/net/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn init<P: NetPermissions + 'static>() -> Vec<OpPair> {
("op_dgram_recv", op_async(op_dgram_recv)),
("op_dgram_send", op_async(op_dgram_send::<P>)),
("op_dns_resolve", op_async(op_dns_resolve::<P>)),
("op_set_nodelay", op_async(op_set_nodelay::<P>)),
]
}

Expand Down Expand Up @@ -665,6 +666,26 @@ where
Ok(results)
}

pub async fn op_set_nodelay<NP>(
state: Rc<RefCell<OpState>>,
args: NoDelayArgs,
_: (),
) -> Result<(), AnyError> {
let rid = args.rid;
let nodelay = args.nodelay;
let resource: Rc<TcpStreamResource> = state
.borrow_mut()
.resource_table
.get::<TcpStreamResource>(rid)?;
resource.set_nodelay(nodelay).await
}
yos1p marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Deserialize)]
pub struct NoDelayArgs {
rid: ResourceId,
nodelay: bool,
}

yos1p marked this conversation as resolved.
Show resolved Hide resolved
fn rdata_to_return_record(
ty: RecordType,
) -> impl Fn(&RData) -> Option<DnsReturnRecord> {
Expand Down