Skip to content

Commit

Permalink
add WriteBulkFrom func write bulk from io.Reader, size n
Browse files Browse the repository at this point in the history
Signed-off-by: weedge <[email protected]>
  • Loading branch information
weedge committed Jul 17, 2023
1 parent 77c37f9 commit fcb9bfd
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion redcon.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ type Conn interface {
PeekPipeline() []Command
// NetConn returns the base net.Conn connection
NetConn() net.Conn
// WriteBulkFrom write bulk from io.Reader, size n
WriteBulkFrom(n int64, rb io.Reader)
}

// NewServer returns a new Redcon server configured on "tcp" network net.
Expand Down Expand Up @@ -494,6 +496,9 @@ func (c *conn) PeekPipeline() []Command {
func (c *conn) NetConn() net.Conn {
return c.conn
}
func (c *conn) WriteBulkFrom(n int64, rb io.Reader) {
c.wr.WriteBulkFrom(n, rb)
}

// BaseWriter returns the underlying connection writer, if any
func BaseWriter(c Conn) *Writer {
Expand Down Expand Up @@ -589,13 +594,30 @@ type Writer struct {
w io.Writer
b []byte
err error

// use golang buff io
buff *bufio.Writer
}

// NewWriter creates a new RESP writer.
func NewWriter(wr io.Writer) *Writer {
return &Writer{
w: wr,
w: wr,
buff: bufio.NewWriterSize(wr, maxBufferCap),
}
}

func (w *Writer) WriteBulkFrom(n int64, rb io.Reader) {
if w != nil && w.err != nil {
return
}
AppendBulkFrom(w, n, rb)
}

func AppendBulkFrom(writer *Writer, n int64, rb io.Reader) {
writer.buff.Write(appendPrefix([]byte{}, '$', n))
io.Copy(writer.w, rb)
writer.buff.Write([]byte{'\r', '\n'})
}

// WriteNull writes a null to the client
Expand Down

0 comments on commit fcb9bfd

Please sign in to comment.