mirror of https://github.com/tidwall/redcon.git
read pipeline
This commit is contained in:
parent
4380ff9799
commit
ffebbd588e
31
redcon.go
31
redcon.go
|
@ -80,6 +80,8 @@ type Conn interface {
|
||||||
// }
|
// }
|
||||||
// }()
|
// }()
|
||||||
Detach() DetachedConn
|
Detach() DetachedConn
|
||||||
|
// ReadPipeline returns all commands in current pipeline, if any
|
||||||
|
ReadPipeline() []Command
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer returns a new Redcon server.
|
// NewServer returns a new Redcon server.
|
||||||
|
@ -219,7 +221,14 @@ func handle(s *Server, c *conn) {
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, cmd := range cmds {
|
c.cmds = cmds
|
||||||
|
for len(c.cmds) > 0 {
|
||||||
|
cmd := c.cmds[0]
|
||||||
|
if len(c.cmds) == 1 {
|
||||||
|
c.cmds = nil
|
||||||
|
} else {
|
||||||
|
c.cmds = c.cmds[1:]
|
||||||
|
}
|
||||||
s.handler(c, cmd)
|
s.handler(c, cmd)
|
||||||
}
|
}
|
||||||
if c.detached {
|
if c.detached {
|
||||||
|
@ -245,6 +254,7 @@ type conn struct {
|
||||||
ctx interface{}
|
ctx interface{}
|
||||||
detached bool
|
detached bool
|
||||||
closed bool
|
closed bool
|
||||||
|
cmds []Command
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *conn) Close() error {
|
func (c *conn) Close() error {
|
||||||
|
@ -263,6 +273,11 @@ func (c *conn) WriteArray(count int) { c.wr.WriteArray(count) }
|
||||||
func (c *conn) WriteNull() { c.wr.WriteNull() }
|
func (c *conn) WriteNull() { c.wr.WriteNull() }
|
||||||
func (c *conn) WriteRaw(data []byte) { c.wr.WriteRaw(data) }
|
func (c *conn) WriteRaw(data []byte) { c.wr.WriteRaw(data) }
|
||||||
func (c *conn) RemoteAddr() string { return c.addr }
|
func (c *conn) RemoteAddr() string { return c.addr }
|
||||||
|
func (c *conn) ReadPipeline() []Command {
|
||||||
|
cmds := c.cmds
|
||||||
|
c.cmds = nil
|
||||||
|
return cmds
|
||||||
|
}
|
||||||
|
|
||||||
// DetachedConn represents a connection that is detached from the server
|
// DetachedConn represents a connection that is detached from the server
|
||||||
type DetachedConn interface {
|
type DetachedConn interface {
|
||||||
|
@ -281,11 +296,14 @@ type DetachedConn interface {
|
||||||
// until Flush() is called.
|
// until Flush() is called.
|
||||||
func (c *conn) Detach() DetachedConn {
|
func (c *conn) Detach() DetachedConn {
|
||||||
c.detached = true
|
c.detached = true
|
||||||
return &detachedConn{conn: c}
|
cmds := c.cmds
|
||||||
|
c.cmds = nil
|
||||||
|
return &detachedConn{conn: c, cmds: cmds}
|
||||||
}
|
}
|
||||||
|
|
||||||
type detachedConn struct {
|
type detachedConn struct {
|
||||||
*conn
|
*conn
|
||||||
|
cmds []Command
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flush writes and Write* calls to the client.
|
// Flush writes and Write* calls to the client.
|
||||||
|
@ -298,6 +316,15 @@ func (dc *detachedConn) ReadCommand() (Command, error) {
|
||||||
if dc.closed {
|
if dc.closed {
|
||||||
return Command{}, errors.New("closed")
|
return Command{}, errors.New("closed")
|
||||||
}
|
}
|
||||||
|
if len(dc.cmds) > 0 {
|
||||||
|
cmd := dc.cmds[0]
|
||||||
|
if len(dc.cmds) == 1 {
|
||||||
|
dc.cmds = nil
|
||||||
|
} else {
|
||||||
|
dc.cmds = dc.cmds[1:]
|
||||||
|
}
|
||||||
|
return cmd, nil
|
||||||
|
}
|
||||||
cmd, err := dc.rd.ReadCommand()
|
cmd, err := dc.rd.ReadCommand()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Command{}, err
|
return Command{}, err
|
||||||
|
|
Loading…
Reference in New Issue