forked from wangaoone/redeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
124 lines (102 loc) · 3.46 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package client
import (
"io"
"net"
"time"
"github.com/mason-leap-lab/redeo/resp"
)
// Conn wraps a single network connection and exposes
// common read/write methods.
type Conn interface {
resp.ResponseParser
// MarkFailed marks the connection as failed which
// will force it to be closed instead of being returned to the pool
MarkFailed()
// WriteCmd writes a full command as part of a pipeline. To execute the pipeline,
// you must call Flush.
WriteCmd(cmd string, args ...[]byte)
// WriteCmdString writes a full command as part of a pipeline. To execute the pipeline,
// you must call Flush.
WriteCmdString(cmd string, args ...string)
// WriteMultiBulkSize is a low-level method to write a multibulk size.
// For normal operation, use WriteCmd or WriteCmdString.
WriteMultiBulkSize(n int) error
// WriteBulk is a low-level method to write a bulk.
// For normal operation, use WriteCmd or WriteCmdString.
WriteBulk(b []byte)
// WriteBulkString is a low-level method to write a bulk.
// For normal operation, use WriteCmd or WriteCmdString.
WriteBulkString(s string)
// CopyBulk is a low-level method to copy a large bulk of data directly to the writer.
// For normal operation, use WriteCmd or WriteCmdString.
CopyBulk(src io.Reader, n int64) error
// Flush flushes the output buffer. Call this after you have completed your pipeline
Flush() error
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
SetDeadline(time.Time) error
// SetReadDeadline sets the deadline for future Read calls
// and any currently-blocked Read call.
// A zero value for t means Read will not time out.
SetReadDeadline(time.Time) error
// SetWriteDeadline sets the deadline for future Write calls
// and any currently-blocked Write call.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
SetWriteDeadline(time.Time) error
// GetConn gets underline connection
GetConn() net.Conn
// UnreadBytes returns the number of unread bytes.
UnreadBytes() int
// UnflushedBytes returns the number of pending/unflushed bytes.
UnflushedBytes() int
// Close (force) closes the connection.
Close() error
// Release release resource to avoid memory leaks.
Release()
madeByRedeo()
}
// Wrap wraps a single network connection.
func Wrap(cn net.Conn) Conn {
return &conn{
Conn: cn,
RequestWriter: resp.NewRequestWriter(cn),
ResponseReader: resp.NewResponseReader(cn),
}
}
type conn struct {
net.Conn
*resp.RequestWriter
resp.ResponseReader
failed bool
}
// MarkFailed implements Conn interface.
func (c *conn) MarkFailed() { c.failed = true }
// UnreadBytes implements Conn interface.
func (c *conn) UnreadBytes() int { return c.ResponseReader.Buffered() }
// UnflushedBytes implements Conn interface.
func (c *conn) UnflushedBytes() int { return c.RequestWriter.Buffered() }
// GetConn implements Conn interface.
func (c *conn) GetConn() net.Conn {
return c.Conn
}
// Close implements Conn interface.
func (c *conn) Close() error {
c.failed = true
err := c.Conn.Close()
// Close writer and reader
c.Release()
return err
}
// Release implements Conn interface.
func (c *conn) Release() {
var w, r io.Closer
w, c.RequestWriter = c.RequestWriter, nil
r, c.ResponseReader = c.ResponseReader, nil
w.Close()
r.Close()
c.Conn = nil
}
func (c *conn) madeByRedeo() {}