net: implement a customizable send chunk size limit for TcpConn.write_ptr

pull/9862/head
Delyan Angelov 2021-04-23 17:03:35 +03:00
parent e6c4c4de3d
commit 6f50157abc
1 changed files with 10 additions and 6 deletions

View File

@ -9,7 +9,8 @@ const (
pub struct TcpConn { pub struct TcpConn {
pub mut: pub mut:
sock TcpSocket sock TcpSocket
max_write_chunk_size int = 4096
mut: mut:
write_deadline time.Time write_deadline time.Time
read_deadline time.Time read_deadline time.Time
@ -49,10 +50,13 @@ pub fn (mut c TcpConn) write_ptr(b &byte, len int) ?int {
mut total_sent := 0 mut total_sent := 0
for total_sent < len { for total_sent < len {
ptr := ptr_base + total_sent ptr := ptr_base + total_sent
remaining := len - total_sent mut chunk_size := len - total_sent
mut sent := C.send(c.sock.handle, ptr, remaining, msg_nosignal) if chunk_size > c.max_write_chunk_size {
chunk_size = c.max_write_chunk_size
}
mut sent := C.send(c.sock.handle, ptr, chunk_size, msg_nosignal)
$if trace_tcp_data_write ? { $if trace_tcp_data_write ? {
eprintln('>>> TcpConn.write_ptr | data chunk, total_sent: ${total_sent:6}, remaining: ${remaining:6}, sent: ${sent:6}, ptr: ${ptr_str(ptr)}') eprintln('>>> TcpConn.write_ptr | data chunk, total_sent: ${total_sent:6}, chunk_size: ${chunk_size:6}, sent: ${sent:6}, ptr: ${ptr_str(ptr)}')
} }
if sent < 0 { if sent < 0 {
code := error_code() code := error_code()
@ -303,7 +307,7 @@ fn new_tcp_socket() ?TcpSocket {
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ? socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ?
} }
$if trace_tcp ? { $if trace_tcp ? {
eprintln(' new_tcp_socket | s.handle: $s.handle') eprintln(' new_tcp_socket | s.handle: ${s.handle:6}')
} }
return s return s
} }
@ -321,7 +325,7 @@ fn tcp_socket_from_handle(sockfd int) ?TcpSocket {
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ? socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ?
} }
$if trace_tcp ? { $if trace_tcp ? {
eprintln(' tcp_socket_from_handle | s.handle: $s.handle') eprintln(' tcp_socket_from_handle | s.handle: ${s.handle:6}')
} }
return s return s
} }