improve conn reading

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
development
kim (grufwub) 3 years ago
parent d6bf0f44a6
commit f2c3dd60b7

@ -2,7 +2,6 @@ package core
import (
"bufio"
"bytes"
"io"
"net"
"time"
@ -50,7 +49,7 @@ func wrapConn(c net.Conn) *conn {
wd: &connWriteDeadline,
}
return &conn{
b: connRequestBufferPool.Get(),
b: connRequestBufferPool.Get()[:0], // reset the buffer
br: connBufferedReaderPool.Get(deadlineConn),
bw: connBufferedWriterPool.Get(deadlineConn),
c: c,
@ -64,24 +63,28 @@ func (c *conn) Conn() net.Conn {
// ReadLine reads a single line and returns the result, or nil and error
func (c *conn) ReadLine() ([]byte, errors.Error) {
// Perform single read into buffer slice
count, err := c.br.Read(c.b)
if err != nil {
return nil, ErrConnRead.Wrap(err)
for {
// Attempt to read next line
b, isPrefix, err := c.br.ReadLine()
if err != nil {
return nil, ErrConnRead.Wrap(err)
}
// If we hit the max request size (i.e. capacity
// of the request buffer), return error
if len(c.b)+len(b) > cap(c.b) {
return nil, ErrInvalidRequest
}
// Add current bytes to request buffer
c.b = append(c.b, b...)
// If we hit the end, break out
if !isPrefix {
break
}
}
// Get up to first '\r\n' (or just '\n')
end := bytes.IndexByte(c.b[:count], '\n')
if end > 0 && c.b[end-1] == '\r' {
end--
}
// If end never reached return error,
// else return slice up to the end
if end == -1 {
return nil, ErrInvalidRequest
}
return c.b[:end], nil
return c.b, nil
}
// WriteBytes writes a byte slice to the buffer and returns error status
@ -113,7 +116,6 @@ func (c *conn) Close() errors.Error {
err := c.c.Close()
// Put buffers back
connBufferedReaderPool.Put(c.br)
connBufferedWriterPool.Put(c.bw)
connRequestBufferPool.Put(c.b)

@ -31,19 +31,19 @@ user-spaces = false
[connection]
# Connection read timeout
read-timeout = "5s"
read-timeout = "5s"
# Connection write timeout
write-timeout = "15s"
# Connection read buffer size (in bytes)
read-buf = 1024
# Connection write buffer size (in bytes)
write-buf = 1024
write-buf = 1024
# Connection read buffer size (in bytes)
read-buf = 1024
# Connection read max (in bytes)
read-max = 1024
read-max = 1024
[filesystem]
# File read buffer size (in bytes)
@ -54,15 +54,15 @@ user-spaces = false
monitor-freq = "60s"
# Maximum cached file size (in MB)
file-max = 1.0
file-max = 1.0
# Maximum file age before mark
# as stale, i.e. safe to be
# removed on next monitor sweep
age-max = "5m"
age-max = "5m"
# Cache size count
size = 100
size= 100
[requests]
# NOTE: please use apostrophe declared
@ -109,19 +109,19 @@ user-spaces = false
# if you have built gophi as a gopher server
#[gopher]
# # Page width before line truncation
# page-width = 80
# page-width = 80
#
# # Footer text included below gophermaps
# footer = ""
# footer = ""
#
# # Subgophermap size max (in megabytes)
# subgopher-max = 1.0
#
# # Information included in caps.txt
# # policy file
# admin-email = ""
# description = ""
# geolocation = ""
# admin-email = ""
# description = ""
# geolocation = ""
# Gemini specific configuration, uncomment
# if you have built gophi as a gemini server

Loading…
Cancel
Save