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,19 +3,19 @@ import net
import time import time
import io import io
// Make a new connection fn main() {
mut conn := net.dial_tcp('google.com:80')? // Make a new connection
// Simple http HEAD request for a file mut conn := net.dial_tcp('google.com:80')?
conn.write_str('GET /index.html HTTP/1.0\r\n\r\n')? // Simple http HEAD request for a file
// Make sure to set a timeout so we can wait for a response! conn.write_str('GET /index.html HTTP/1.0\r\n\r\n')?
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 {
l := r.read_line() or { l := r.read_line() or {
break break
} }
println('$l') println('$l')
// 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
// Make a new connection fn main() {
mut conn := net.dial_tcp('google.com:80')? // Make a new connection
defer { conn.close() } mut conn := net.dial_tcp('google.com:80')?
// Simple http HEAD request for a file defer { conn.close() }
conn.write_str('HEAD /index.html HTTP/1.0\r\n\r\n')? // Simple http HEAD request for a file
// Make sure to set a timeout so we can wait for a response! conn.write_str('HEAD /index.html HTTP/1.0\r\n\r\n')?
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,32 +2,33 @@ 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
} }
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
} }
} }
pub fn (c TcpConn) close() ? { pub fn (c TcpConn) close() ? {
c.sock.close()? c.sock.close() ?
return none return none
} }
@ -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 {
@ -50,7 +49,7 @@ pub fn (c TcpConn) write_ptr(b byteptr, len int) ? {
continue continue
} }
else { else {
wrap_error(code)? wrap_error(code) ?
} }
} }
} }
@ -71,21 +70,19 @@ 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 {
c.wait_for_read()? c.wait_for_read() ?
res = wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0))? res = wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ?
return socket_error(res) return socket_error(res)
} }
else { else {
wrap_error(code)? wrap_error(code) ?
} }
} }
} }
@ -120,7 +117,7 @@ pub fn (c TcpConn) read_timeout() time.Duration {
return c.read_timeout return c.read_timeout
} }
pub fn(mut c TcpConn) set_read_timeout(t time.Duration) { pub fn (mut c TcpConn) set_read_timeout(t time.Duration) {
c.read_timeout = t c.read_timeout = t
} }
@ -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)
} }
@ -155,7 +150,7 @@ pub fn (c TcpConn) peer_ip() ?string {
buf := [44]byte{} buf := [44]byte{}
peeraddr := C.sockaddr_in{} peeraddr := C.sockaddr_in{}
speeraddr := sizeof(peeraddr) speeraddr := sizeof(peeraddr)
socket_error(C.getpeername(c.sock.handle, &C.sockaddr(&peeraddr), &speeraddr))? socket_error(C.getpeername(c.sock.handle, &C.sockaddr(&peeraddr), &speeraddr)) ?
cstr := C.inet_ntop(C.AF_INET, &peeraddr.sin_addr, buf, sizeof(buf)) cstr := C.inet_ntop(C.AF_INET, &peeraddr.sin_addr, buf, sizeof(buf))
if cstr == 0 { if cstr == 0 {
return error('net.peer_ip: inet_ntop failed') return error('net.peer_ip: inet_ntop failed')
@ -171,30 +166,24 @@ 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
} }
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
accept_timeout: infinite_timeout accept_timeout: infinite_timeout
@ -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
} }
} }
@ -244,7 +227,7 @@ pub fn (c TcpListener) accept_timeout() time.Duration {
return c.accept_timeout return c.accept_timeout
} }
pub fn(mut c TcpListener) set_accept_timeout(t time.Duration) { pub fn (mut c TcpListener) set_accept_timeout(t time.Duration) {
c.accept_timeout = t c.accept_timeout = t
} }
@ -253,7 +236,7 @@ pub fn (c TcpListener) wait_for_accept() ? {
} }
pub fn (c TcpListener) close() ? { pub fn (c TcpListener) close() ? {
c.sock.close()? c.sock.close() ?
return none return none
} }
@ -267,15 +250,15 @@ pub:
} }
fn new_tcp_socket() ?TcpSocket { fn new_tcp_socket() ?TcpSocket {
sockfd := socket_error(C.socket(SocketFamily.inet, SocketType.tcp, 0))? sockfd := socket_error(C.socket(SocketFamily.inet, SocketType.tcp, 0)) ?
s := TcpSocket { s := TcpSocket{
handle: sockfd handle: sockfd
} }
//s.set_option_bool(.reuse_addr, true)? // s.set_option_bool(.reuse_addr, true)?
s.set_option_int(.reuse_addr, 1)? s.set_option_int(.reuse_addr, 1) ?
$if windows { $if windows {
t := true t := true
socket_error(C.ioctlsocket(sockfd, fionbio, &t))? socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
} $else { } $else {
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))
} }
@ -283,14 +266,14 @@ fn new_tcp_socket() ?TcpSocket {
} }
fn tcp_socket_from_handle(sockfd int) ?TcpSocket { fn tcp_socket_from_handle(sockfd int) ?TcpSocket {
s := TcpSocket { s := TcpSocket{
handle: sockfd handle: sockfd
} }
//s.set_option_bool(.reuse_addr, true)? // s.set_option_bool(.reuse_addr, true)?
s.set_option_int(.reuse_addr, 1)? s.set_option_int(.reuse_addr, 1) ?
$if windows { $if windows {
t := true t := true
socket_error(C.ioctlsocket(sockfd, fionbio, &t))? socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
} $else { } $else {
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))
} }
@ -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
} }
@ -331,22 +310,18 @@ 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
return none return none
} }
except_result := s.@select(.except, connect_timeout)? except_result := s.@select(.except, connect_timeout) ?
if except_result { if except_result {
return err_connect_failed return err_connect_failed
} }

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)?