mirror of https://github.com/tidwall/redcon.git
added SetReadBuffer
This commit is contained in:
parent
32650a5023
commit
a0dd5b1b13
18
redcon.go
18
redcon.go
|
@ -1,3 +1,4 @@
|
||||||
|
// Package redcon provides a custom redis server implementation.
|
||||||
package redcon
|
package redcon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -8,6 +9,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Conn represents a client connection
|
||||||
type Conn interface {
|
type Conn interface {
|
||||||
RemoteAddr() string
|
RemoteAddr() string
|
||||||
Close() error
|
Close() error
|
||||||
|
@ -17,6 +19,7 @@ type Conn interface {
|
||||||
WriteInt(num int)
|
WriteInt(num int)
|
||||||
WriteArray(count int)
|
WriteArray(count int)
|
||||||
WriteNull()
|
WriteNull()
|
||||||
|
SetReadBuffer(bytes int)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -25,6 +28,8 @@ var (
|
||||||
errInvalidMultiBulkLength = &errProtocol{"invalid multibulk length"}
|
errInvalidMultiBulkLength = &errProtocol{"invalid multibulk length"}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultBufLen = 1024 * 64
|
||||||
|
|
||||||
type errProtocol struct {
|
type errProtocol struct {
|
||||||
msg string
|
msg string
|
||||||
}
|
}
|
||||||
|
@ -33,6 +38,7 @@ func (err *errProtocol) Error() string {
|
||||||
return "Protocol error: " + err.msg
|
return "Protocol error: " + err.msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListenAndServe creates a new server and binds to addr.
|
||||||
func ListenAndServe(
|
func ListenAndServe(
|
||||||
addr string, handler func(conn Conn, cmds [][]string),
|
addr string, handler func(conn Conn, cmds [][]string),
|
||||||
accept func(conn Conn) bool, closed func(conn Conn, err error),
|
accept func(conn Conn) bool, closed func(conn Conn, err error),
|
||||||
|
@ -139,21 +145,23 @@ func (c *conn) WriteNull() {
|
||||||
func (c *conn) RemoteAddr() string {
|
func (c *conn) RemoteAddr() string {
|
||||||
return c.addr
|
return c.addr
|
||||||
}
|
}
|
||||||
|
func (c *conn) SetReadBuffer(bytes int) {
|
||||||
|
c.rd.buflen = bytes
|
||||||
|
}
|
||||||
|
|
||||||
// Reader represents a RESP command reader.
|
// Reader represents a RESP command reader.
|
||||||
type reader struct {
|
type reader struct {
|
||||||
r io.Reader // base reader
|
r io.Reader // base reader
|
||||||
b []byte // unprocessed bytes
|
b []byte // unprocessed bytes
|
||||||
a []byte // static read buffer
|
a []byte // static read buffer
|
||||||
|
buflen int // buffer len
|
||||||
}
|
}
|
||||||
|
|
||||||
const buflen = 1024 * 8
|
|
||||||
|
|
||||||
// NewReader returns a RESP command reader.
|
// NewReader returns a RESP command reader.
|
||||||
func newReader(r io.Reader) *reader {
|
func newReader(r io.Reader) *reader {
|
||||||
return &reader{
|
return &reader{
|
||||||
r: r,
|
r: r,
|
||||||
a: make([]byte, buflen),
|
buflen: defaultBufLen,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -314,7 +322,7 @@ func (r *reader) ReadCommands() ([][]string, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(r.a) == 0 {
|
if len(r.a) == 0 {
|
||||||
r.a = make([]byte, buflen)
|
r.a = make([]byte, r.buflen)
|
||||||
}
|
}
|
||||||
n, err := r.r.Read(r.a)
|
n, err := r.r.Read(r.a)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -344,7 +352,7 @@ type writer struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWriter(w *net.TCPConn) *writer {
|
func newWriter(w *net.TCPConn) *writer {
|
||||||
return &writer{w: w, b: make([]byte, 0, 256)}
|
return &writer{w: w, b: make([]byte, 0, 512)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *writer) WriteNull() error {
|
func (w *writer) WriteNull() error {
|
||||||
|
|
Loading…
Reference in New Issue