1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vweb: make vweb async (#8095)

This commit is contained in:
Louis Schmieder 2021-01-21 11:08:38 +01:00 committed by GitHub
parent 0c249fa040
commit b44ec4921f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -225,7 +225,7 @@ struct SimpleTcpClientConfig {
host string = 'static.dev' host string = 'static.dev'
path string = '/' path string = '/'
agent string = 'v/net.tcp.v' agent string = 'v/net.tcp.v'
headers string headers string = '\r\n'
content string content string
} }

View File

@ -66,6 +66,9 @@ pub fn (ctx Context) init_once() {}
// declaring init in your App struct is optional // declaring init in your App struct is optional
pub fn (ctx Context) init() {} pub fn (ctx Context) init() {}
// declaring uninit in your App struct is optional
pub fn (ctx Context) uninit() {}
pub struct Cookie { pub struct Cookie {
name string name string
value string value string
@ -255,7 +258,7 @@ pub fn run_app<T>(mut app T, port int) {
// app.reset() // app.reset()
for { for {
mut conn := l.accept() or { panic('accept() failed') } mut conn := l.accept() or { panic('accept() failed') }
handle_conn<T>(mut conn, mut app) go handle_conn<T>(mut conn, mut app)
// app.vweb.page_gen_time = time.ticks() - t // app.vweb.page_gen_time = time.ticks() - t
// eprintln('handle conn() took ${time.ticks()-t}ms') // eprintln('handle conn() took ${time.ticks()-t}ms')
// message := readall(conn) // message := readall(conn)
@ -281,7 +284,8 @@ pub fn run_app<T>(mut app T, port int) {
} }
fn handle_conn<T>(mut conn net.TcpConn, mut app T) { fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
conn.set_read_timeout(1 * time.second) conn.set_read_timeout(30 * time.second)
conn.set_write_timeout(30 * time.second)
defer { defer {
conn.close() or { } conn.close() or { }
} }
@ -291,11 +295,13 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
mut reader := io.new_buffered_reader(reader: io.make_reader(conn)) mut reader := io.new_buffered_reader(reader: io.make_reader(conn))
page_gen_start := time.ticks() page_gen_start := time.ticks()
first_line := reader.read_line() or { first_line := reader.read_line() or {
println('Failed first_line') $if debug {
eprintln('Failed first_line') // show this only in debug mode, because it always would be shown after a chromium user visits the site
}
return return
} }
$if debug { $if debug {
println('firstline="$first_line"') eprintln('firstline="$first_line"')
} }
// Parse the first line // Parse the first line
// "GET / HTTP/1.1" // "GET / HTTP/1.1"
@ -329,7 +335,8 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
// break // break
//} //}
// read body // read body
read_body := io.read_all(reader: reader) or { []byte{} } mut read_body := []byte{len: len}
reader.read(mut read_body) // read just the amount of content len if there is no content there is nothing more to read here
body += read_body.bytestr() body += read_body.bytestr()
break break
} }
@ -465,6 +472,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
println('easy match method=$method.name') println('easy match method=$method.name')
} }
app.$method(vars) app.$method(vars)
return return
} }
} else if method.name == 'index' { } else if method.name == 'index' {
@ -473,6 +481,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
println('route to .index()') println('route to .index()')
} }
app.$method(vars) app.$method(vars)
return return
} }
} else { } else {
@ -531,6 +540,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
if matching && !unknown { if matching && !unknown {
// absolute router words like `/test/site` // absolute router words like `/test/site`
app.$method(vars) app.$method(vars)
return return
} else if matching && unknown { } else if matching && unknown {
// router words with paramter like `/:test/site` // router words with paramter like `/:test/site`