net: add tcp_default_read_timeout and tcp_default_write_timeout and use them consistently

pull/7351/head
Delyan Angelov 2020-12-15 17:39:11 +02:00
parent 560c21629e
commit e3a1756b11
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
9 changed files with 80 additions and 130 deletions

View File

@ -175,8 +175,6 @@ fn (mut cfg DocConfig) serve_html() {
server.close() or { } server.close() or { }
panic(err) panic(err)
} }
conn.set_read_timeout(5 * time.second)
conn.set_write_timeout(5 * time.second)
handle_http_connection(mut conn, server_context) handle_http_connection(mut conn, server_context)
conn.close() or { eprintln('error closing the connection: $err') } conn.close() or { eprintln('error closing the connection: $err') }
} }

View File

@ -3,12 +3,11 @@ import net
import time import time
import io import io
fn main() {
// Make a new connection // Make a new connection
mut conn := net.dial_tcp('google.com:80')? mut conn := net.dial_tcp('google.com:80')?
// Simple http HEAD request for a file // Simple http HEAD request for a file
conn.write_str('GET /index.html HTTP/1.0\r\n\r\n')? conn.write_str('GET /index.html HTTP/1.0\r\n\r\n')?
// Make sure to set a timeout so we can wait for a response!
conn.set_read_timeout(10 * time.second)
// Wrap in a buffered reader // Wrap in a buffered reader
mut r := io.new_buffered_reader(reader: io.make_reader(conn)) mut r := io.new_buffered_reader(reader: io.make_reader(conn))
for { for {
@ -19,3 +18,4 @@ for {
// Make it nice and obvious that we are doing this line by line // Make it nice and obvious that we are doing this line by line
time.sleep_ms(10) time.sleep_ms(10)
} }
}

View File

@ -1,15 +1,14 @@
// Simple raw HTTP head request
import net import net
import io import io
fn main() {
// Make a new connection // Make a new connection
mut conn := net.dial_tcp('google.com:80')? mut conn := net.dial_tcp('google.com:80')?
defer { conn.close() } defer { conn.close() }
// Simple http HEAD request for a file // Simple http HEAD request for a file
conn.write_str('HEAD /index.html HTTP/1.0\r\n\r\n')? conn.write_str('HEAD /index.html HTTP/1.0\r\n\r\n')?
// Make sure to set a timeout so we can wait for a response!
conn.set_read_timeout(net.infinite_timeout)
// Read all the data that is waiting // Read all the data that is waiting
result := io.read_all(conn)? result := io.read_all(conn)?
// Cast to string and print result // Cast to string and print result
println(result.bytestr()) println(result.bytestr())
}

View File

@ -49,7 +49,9 @@ fn (mut r BufferedReader) fill_buffer() ? {
// trying to call this // trying to call this
r.offset = 0 r.offset = 0
new_len := r.reader.read(mut r.buf) or { new_len := r.reader.read(mut r.buf) or {
eprintln('>> BufferedReader.reader.read err: $err') if errcode != 0 || err.len != 0 {
eprintln('>> BufferedReader.reader.read err: $err | errcode: $errcode')
}
0 0
} }
r.len = new_len r.len = new_len

View File

@ -6,7 +6,6 @@ module http
import net.urllib import net.urllib
import net.http.chunked import net.http.chunked
import net import net
import time
import io import io
const ( const (
@ -398,7 +397,6 @@ fn (req &Request) http_do(host string, method Method, path string) ?Response {
s := req.build_request_headers(method, host_name, path) s := req.build_request_headers(method, host_name, path)
mut client := net.dial_tcp(host)? mut client := net.dial_tcp(host)?
// TODO this really needs to be exposed somehow // TODO this really needs to be exposed somehow
client.set_read_timeout(time.second * 30)
client.write(s.bytes())? client.write(s.bytes())?
mut bytes := io.read_all(client)? mut bytes := io.read_all(client)?
client.close() client.close()

View File

@ -2,14 +2,17 @@ module net
import time import time
const (
tcp_default_read_timeout = 30 * time.second
tcp_default_write_timeout = 30 * time.second
)
pub struct TcpConn { pub struct TcpConn {
pub: pub:
sock TcpSocket sock TcpSocket
mut: mut:
write_deadline time.Time write_deadline time.Time
read_deadline time.Time read_deadline time.Time
read_timeout time.Duration read_timeout time.Duration
write_timeout time.Duration write_timeout time.Duration
} }
@ -17,12 +20,10 @@ mut:
pub fn dial_tcp(address string) ?TcpConn { pub fn dial_tcp(address string) ?TcpConn {
s := new_tcp_socket() ? s := new_tcp_socket() ?
s.connect(address) ? s.connect(address) ?
return TcpConn{ return TcpConn{
sock: s sock: s
read_timeout: tcp_default_read_timeout
read_timeout: 30 * time.second write_timeout: tcp_default_write_timeout
write_timeout: 30 * time.second
} }
} }
@ -36,12 +37,10 @@ pub fn (c TcpConn) write_ptr(b byteptr, len int) ? {
unsafe { unsafe {
mut ptr_base := byteptr(b) mut ptr_base := byteptr(b)
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 remaining := len - total_sent
mut sent := C.send(c.sock.handle, ptr, remaining, msg_nosignal) mut sent := C.send(c.sock.handle, ptr, remaining, msg_nosignal)
if sent < 0 { if sent < 0 {
code := error_code() code := error_code()
match code { match code {
@ -72,11 +71,9 @@ pub fn (c TcpConn) write_str(s string) ? {
pub fn (c TcpConn) read_ptr(buf_ptr byteptr, len int) ?int { pub fn (c TcpConn) read_ptr(buf_ptr byteptr, len int) ?int {
mut res := wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ? mut res := wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ?
if res > 0 { if res > 0 {
return res return res
} }
code := error_code() code := error_code()
match code { match code {
error_ewouldblock { error_ewouldblock {
@ -145,9 +142,7 @@ pub fn (c TcpConn) wait_for_write() ? {
pub fn (c TcpConn) peer_addr() ?Addr { pub fn (c TcpConn) peer_addr() ?Addr {
mut addr := C.sockaddr{} mut addr := C.sockaddr{}
len := sizeof(C.sockaddr) len := sizeof(C.sockaddr)
socket_error(C.getpeername(c.sock.handle, &addr, &len)) ? socket_error(C.getpeername(c.sock.handle, &addr, &len)) ?
return new_addr(addr) return new_addr(addr)
} }
@ -171,7 +166,6 @@ pub fn (c TcpConn) str() string {
pub struct TcpListener { pub struct TcpListener {
sock TcpSocket sock TcpSocket
mut: mut:
accept_timeout time.Duration accept_timeout time.Duration
accept_deadline time.Time accept_deadline time.Time
@ -179,21 +173,16 @@ mut:
pub fn listen_tcp(port int) ?TcpListener { pub fn listen_tcp(port int) ?TcpListener {
s := new_tcp_socket() ? s := new_tcp_socket() ?
validate_port(port) ? validate_port(port) ?
mut addr := C.sockaddr_in{} mut addr := C.sockaddr_in{}
addr.sin_family = SocketFamily.inet addr.sin_family = SocketFamily.inet
addr.sin_port = C.htons(port) addr.sin_port = C.htons(port)
addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY) addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY)
size := sizeof(C.sockaddr_in) size := sizeof(C.sockaddr_in)
// cast to the correct type // cast to the correct type
sockaddr := &C.sockaddr(&addr) sockaddr := &C.sockaddr(&addr)
socket_error(C.bind(s.handle, sockaddr, size)) ? socket_error(C.bind(s.handle, sockaddr, size)) ?
socket_error(C.listen(s.handle, 128)) ? socket_error(C.listen(s.handle, 128)) ?
return TcpListener{ return TcpListener{
sock: s sock: s
accept_deadline: no_deadline accept_deadline: no_deadline
@ -203,29 +192,23 @@ pub fn listen_tcp(port int) ?TcpListener {
pub fn (l TcpListener) accept() ?TcpConn { pub fn (l TcpListener) accept() ?TcpConn {
addr := C.sockaddr_storage{} addr := C.sockaddr_storage{}
unsafe { unsafe {C.memset(&addr, 0, sizeof(C.sockaddr_storage))}
C.memset(&addr, 0, sizeof(C.sockaddr_storage))
}
size := sizeof(C.sockaddr_storage) size := sizeof(C.sockaddr_storage)
// cast to correct type // cast to correct type
sock_addr := &C.sockaddr(&addr) sock_addr := &C.sockaddr(&addr)
mut new_handle := C.accept(l.sock.handle, sock_addr, &size) mut new_handle := C.accept(l.sock.handle, sock_addr, &size)
if new_handle <= 0 { if new_handle <= 0 {
l.wait_for_accept() ? l.wait_for_accept() ?
new_handle = C.accept(l.sock.handle, sock_addr, &size) new_handle = C.accept(l.sock.handle, sock_addr, &size)
if new_handle == -1 || new_handle == 0 { if new_handle == -1 || new_handle == 0 {
return none return none
} }
} }
new_sock := tcp_socket_from_handle(new_handle) ? new_sock := tcp_socket_from_handle(new_handle) ?
return TcpConn{ return TcpConn{
sock: new_sock sock: new_sock
read_timeout: tcp_default_read_timeout
write_timeout: tcp_default_write_timeout
} }
} }
@ -302,19 +285,15 @@ pub fn (s TcpSocket) set_option_bool(opt SocketOption, value bool) ? {
// if opt !in opts_can_set { // if opt !in opts_can_set {
// return err_option_not_settable // return err_option_not_settable
// } // }
// if opt !in opts_bool { // if opt !in opts_bool {
// return err_option_wrong_type // return err_option_wrong_type
// } // }
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(bool))) ? socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(bool))) ?
return none return none
} }
pub fn (s TcpSocket) set_option_int(opt SocketOption, value int) ? { pub fn (s TcpSocket) set_option_int(opt SocketOption, value int) ? {
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(int))) ? socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(int))) ?
return none return none
} }
@ -332,15 +311,11 @@ const (
fn (s TcpSocket) connect(a string) ? { fn (s TcpSocket) connect(a string) ? {
addr := resolve_addr(a, .inet, .tcp) ? addr := resolve_addr(a, .inet, .tcp) ?
res := C.connect(s.handle, &addr.addr, addr.len) res := C.connect(s.handle, &addr.addr, addr.len)
if res == 0 { if res == 0 {
return none return none
} }
_ := error_code() _ := error_code()
write_result := s.@select(.write, connect_timeout) ? write_result := s.@select(.write, connect_timeout) ?
if write_result { if write_result {
// succeeded // succeeded

View File

@ -14,13 +14,9 @@ fn setup() (net.TcpListener, net.TcpConn, net.TcpConn) {
mut client := net.dial_tcp('127.0.0.1:$server_port') or { mut client := net.dial_tcp('127.0.0.1:$server_port') or {
panic(err) panic(err)
} }
client.set_read_timeout(3 * time.second)
client.set_write_timeout(3 * time.second)
mut socket := server.accept() or { mut socket := server.accept() or {
panic(err) panic(err)
} }
socket.set_read_timeout(3 * time.second)
socket.set_write_timeout(3 * time.second)
$if debug_peer_ip ? { $if debug_peer_ip ? {
ip := con.peer_ip() or { ip := con.peer_ip() or {
'$err' '$err'

View File

@ -7,10 +7,6 @@ const (
fn handle_conn(_c net.TcpConn) { fn handle_conn(_c net.TcpConn) {
mut c := _c mut c := _c
// arbitrary timeouts to ensure that it doesnt
// instantly throw its hands in the air and give up
c.set_read_timeout(10 * time.second)
c.set_write_timeout(10 * time.second)
for { for {
mut buf := []byte{len: 100, init: 0} mut buf := []byte{len: 100, init: 0}
read := c.read(mut buf) or { read := c.read(mut buf) or {
@ -39,10 +35,6 @@ fn echo() ? {
defer { defer {
c.close() or { } c.close() or { }
} }
// arbitrary timeouts to ensure that it doesnt
// instantly throw its hands in the air and give up
c.set_read_timeout(10 * time.second)
c.set_write_timeout(10 * time.second)
data := 'Hello from vlib/net!' data := 'Hello from vlib/net!'
c.write_str(data)? c.write_str(data)?
mut buf := []byte{len: 4096} mut buf := []byte{len: 4096}

View File

@ -3,10 +3,6 @@ import time
fn echo_server(_c net.UdpConn) { fn echo_server(_c net.UdpConn) {
mut c := _c mut c := _c
// arbitrary timeouts to ensure that it doesnt
// instantly throw its hands in the air and give up
c.set_read_timeout(10 * time.second)
c.set_write_timeout(10 * time.second)
for { for {
mut buf := []byte{ len: 100, init: 0 } mut buf := []byte{ len: 100, init: 0 }
read, addr := c.read(mut buf) or { read, addr := c.read(mut buf) or {
@ -23,12 +19,6 @@ fn echo_server(_c net.UdpConn) {
fn echo() ? { fn echo() ? {
mut c := net.dial_udp('127.0.0.1:40003', '127.0.0.1:40001')? mut c := net.dial_udp('127.0.0.1:40003', '127.0.0.1:40001')?
defer { c.close() or { } } defer { c.close() or { } }
// arbitrary timeouts to ensure that it doesnt
// instantly throw its hands in the air and give up
c.set_read_timeout(10 * time.second)
c.set_write_timeout(10 * time.second)
data := 'Hello from vlib/net!' data := 'Hello from vlib/net!'
c.write_str(data)? c.write_str(data)?