conn.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redis
  15. import (
  16. "bufio"
  17. "bytes"
  18. "errors"
  19. "fmt"
  20. "io"
  21. "net"
  22. "strconv"
  23. "sync"
  24. "time"
  25. )
  26. // conn is the low-level implementation of Conn
  27. type conn struct {
  28. // Shared
  29. mu sync.Mutex
  30. pending int
  31. err error
  32. conn net.Conn
  33. // Read
  34. readTimeout time.Duration
  35. br *bufio.Reader
  36. // Write
  37. writeTimeout time.Duration
  38. bw *bufio.Writer
  39. // Scratch space for formatting argument length.
  40. // '*' or '$', length, "\r\n"
  41. lenScratch [32]byte
  42. // Scratch space for formatting integers and floats.
  43. numScratch [40]byte
  44. }
  45. // Dial connects to the Redis server at the given network and address.
  46. func Dial(network, address string) (Conn, error) {
  47. c, err := net.Dial(network, address)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return NewConn(c, 0, 0), nil
  52. }
  53. // DialTimeout acts like Dial but takes timeouts for establishing the
  54. // connection to the server, writing a command and reading a reply.
  55. func DialTimeout(network, address string, connectTimeout, readTimeout, writeTimeout time.Duration) (Conn, error) {
  56. var c net.Conn
  57. var err error
  58. if connectTimeout > 0 {
  59. c, err = net.DialTimeout(network, address, connectTimeout)
  60. } else {
  61. c, err = net.Dial(network, address)
  62. }
  63. if err != nil {
  64. return nil, err
  65. }
  66. return NewConn(c, readTimeout, writeTimeout), nil
  67. }
  68. // NewConn returns a new Redigo connection for the given net connection.
  69. func NewConn(netConn net.Conn, readTimeout, writeTimeout time.Duration) Conn {
  70. return &conn{
  71. conn: netConn,
  72. bw: bufio.NewWriter(netConn),
  73. br: bufio.NewReader(netConn),
  74. readTimeout: readTimeout,
  75. writeTimeout: writeTimeout,
  76. }
  77. }
  78. func (c *conn) Close() error {
  79. c.mu.Lock()
  80. err := c.err
  81. if c.err == nil {
  82. c.err = errors.New("redigo: closed")
  83. err = c.conn.Close()
  84. }
  85. c.mu.Unlock()
  86. return err
  87. }
  88. func (c *conn) fatal(err error) error {
  89. c.mu.Lock()
  90. if c.err == nil {
  91. c.err = err
  92. // Close connection to force errors on subsequent calls and to unblock
  93. // other reader or writer.
  94. c.conn.Close()
  95. }
  96. c.mu.Unlock()
  97. return err
  98. }
  99. func (c *conn) Err() error {
  100. c.mu.Lock()
  101. err := c.err
  102. c.mu.Unlock()
  103. return err
  104. }
  105. func (c *conn) writeLen(prefix byte, n int) error {
  106. c.lenScratch[len(c.lenScratch)-1] = '\n'
  107. c.lenScratch[len(c.lenScratch)-2] = '\r'
  108. i := len(c.lenScratch) - 3
  109. for {
  110. c.lenScratch[i] = byte('0' + n%10)
  111. i -= 1
  112. n = n / 10
  113. if n == 0 {
  114. break
  115. }
  116. }
  117. c.lenScratch[i] = prefix
  118. _, err := c.bw.Write(c.lenScratch[i:])
  119. return err
  120. }
  121. func (c *conn) writeString(s string) error {
  122. c.writeLen('$', len(s))
  123. c.bw.WriteString(s)
  124. _, err := c.bw.WriteString("\r\n")
  125. return err
  126. }
  127. func (c *conn) writeBytes(p []byte) error {
  128. c.writeLen('$', len(p))
  129. c.bw.Write(p)
  130. _, err := c.bw.WriteString("\r\n")
  131. return err
  132. }
  133. func (c *conn) writeInt64(n int64) error {
  134. return c.writeBytes(strconv.AppendInt(c.numScratch[:0], n, 10))
  135. }
  136. func (c *conn) writeFloat64(n float64) error {
  137. return c.writeBytes(strconv.AppendFloat(c.numScratch[:0], n, 'g', -1, 64))
  138. }
  139. func (c *conn) writeCommand(cmd string, args []interface{}) (err error) {
  140. c.writeLen('*', 1+len(args))
  141. err = c.writeString(cmd)
  142. for _, arg := range args {
  143. if err != nil {
  144. break
  145. }
  146. switch arg := arg.(type) {
  147. case string:
  148. err = c.writeString(arg)
  149. case []byte:
  150. err = c.writeBytes(arg)
  151. case int:
  152. err = c.writeInt64(int64(arg))
  153. case int64:
  154. err = c.writeInt64(arg)
  155. case float64:
  156. err = c.writeFloat64(arg)
  157. case bool:
  158. if arg {
  159. err = c.writeString("1")
  160. } else {
  161. err = c.writeString("0")
  162. }
  163. case nil:
  164. err = c.writeString("")
  165. default:
  166. var buf bytes.Buffer
  167. fmt.Fprint(&buf, arg)
  168. err = c.writeBytes(buf.Bytes())
  169. }
  170. }
  171. return err
  172. }
  173. type protocolError string
  174. func (pe protocolError) Error() string {
  175. return fmt.Sprintf("redigo: %s (possible server error or unsupported concurrent read by application)", string(pe))
  176. }
  177. func (c *conn) readLine() ([]byte, error) {
  178. p, err := c.br.ReadSlice('\n')
  179. if err == bufio.ErrBufferFull {
  180. return nil, protocolError("long response line")
  181. }
  182. if err != nil {
  183. return nil, err
  184. }
  185. i := len(p) - 2
  186. if i < 0 || p[i] != '\r' {
  187. return nil, protocolError("bad response line terminator")
  188. }
  189. return p[:i], nil
  190. }
  191. // parseLen parses bulk string and array lengths.
  192. func parseLen(p []byte) (int, error) {
  193. if len(p) == 0 {
  194. return -1, protocolError("malformed length")
  195. }
  196. if p[0] == '-' && len(p) == 2 && p[1] == '1' {
  197. // handle $-1 and $-1 null replies.
  198. return -1, nil
  199. }
  200. var n int
  201. for _, b := range p {
  202. n *= 10
  203. if b < '0' || b > '9' {
  204. return -1, protocolError("illegal bytes in length")
  205. }
  206. n += int(b - '0')
  207. }
  208. return n, nil
  209. }
  210. // parseInt parses an integer reply.
  211. func parseInt(p []byte) (interface{}, error) {
  212. if len(p) == 0 {
  213. return 0, protocolError("malformed integer")
  214. }
  215. var negate bool
  216. if p[0] == '-' {
  217. negate = true
  218. p = p[1:]
  219. if len(p) == 0 {
  220. return 0, protocolError("malformed integer")
  221. }
  222. }
  223. var n int64
  224. for _, b := range p {
  225. n *= 10
  226. if b < '0' || b > '9' {
  227. return 0, protocolError("illegal bytes in length")
  228. }
  229. n += int64(b - '0')
  230. }
  231. if negate {
  232. n = -n
  233. }
  234. return n, nil
  235. }
  236. var (
  237. okReply interface{} = "OK"
  238. pongReply interface{} = "PONG"
  239. )
  240. func (c *conn) readReply() (interface{}, error) {
  241. line, err := c.readLine()
  242. if err != nil {
  243. return nil, err
  244. }
  245. if len(line) == 0 {
  246. return nil, protocolError("short response line")
  247. }
  248. switch line[0] {
  249. case '+':
  250. switch {
  251. case len(line) == 3 && line[1] == 'O' && line[2] == 'K':
  252. // Avoid allocation for frequent "+OK" response.
  253. return okReply, nil
  254. case len(line) == 5 && line[1] == 'P' && line[2] == 'O' && line[3] == 'N' && line[4] == 'G':
  255. // Avoid allocation in PING command benchmarks :)
  256. return pongReply, nil
  257. default:
  258. return string(line[1:]), nil
  259. }
  260. case '-':
  261. return Error(string(line[1:])), nil
  262. case ':':
  263. return parseInt(line[1:])
  264. case '$':
  265. n, err := parseLen(line[1:])
  266. if n < 0 || err != nil {
  267. return nil, err
  268. }
  269. p := make([]byte, n)
  270. _, err = io.ReadFull(c.br, p)
  271. if err != nil {
  272. return nil, err
  273. }
  274. if line, err := c.readLine(); err != nil {
  275. return nil, err
  276. } else if len(line) != 0 {
  277. return nil, protocolError("bad bulk string format")
  278. }
  279. return p, nil
  280. case '*':
  281. n, err := parseLen(line[1:])
  282. if n < 0 || err != nil {
  283. return nil, err
  284. }
  285. r := make([]interface{}, n)
  286. for i := range r {
  287. r[i], err = c.readReply()
  288. if err != nil {
  289. return nil, err
  290. }
  291. }
  292. return r, nil
  293. }
  294. return nil, protocolError("unexpected response line")
  295. }
  296. func (c *conn) Send(cmd string, args ...interface{}) error {
  297. c.mu.Lock()
  298. c.pending += 1
  299. c.mu.Unlock()
  300. if c.writeTimeout != 0 {
  301. c.conn.SetWriteDeadline(time.Now().Add(c.writeTimeout))
  302. }
  303. if err := c.writeCommand(cmd, args); err != nil {
  304. return c.fatal(err)
  305. }
  306. return nil
  307. }
  308. func (c *conn) Flush() error {
  309. if c.writeTimeout != 0 {
  310. c.conn.SetWriteDeadline(time.Now().Add(c.writeTimeout))
  311. }
  312. if err := c.bw.Flush(); err != nil {
  313. return c.fatal(err)
  314. }
  315. return nil
  316. }
  317. func (c *conn) Receive() (reply interface{}, err error) {
  318. if c.readTimeout != 0 {
  319. c.conn.SetReadDeadline(time.Now().Add(c.readTimeout))
  320. }
  321. if reply, err = c.readReply(); err != nil {
  322. return nil, c.fatal(err)
  323. }
  324. // When using pub/sub, the number of receives can be greater than the
  325. // number of sends. To enable normal use of the connection after
  326. // unsubscribing from all channels, we do not decrement pending to a
  327. // negative value.
  328. //
  329. // The pending field is decremented after the reply is read to handle the
  330. // case where Receive is called before Send.
  331. c.mu.Lock()
  332. if c.pending > 0 {
  333. c.pending -= 1
  334. }
  335. c.mu.Unlock()
  336. if err, ok := reply.(Error); ok {
  337. return nil, err
  338. }
  339. return
  340. }
  341. func (c *conn) Do(cmd string, args ...interface{}) (interface{}, error) {
  342. c.mu.Lock()
  343. pending := c.pending
  344. c.pending = 0
  345. c.mu.Unlock()
  346. if cmd == "" && pending == 0 {
  347. return nil, nil
  348. }
  349. if c.writeTimeout != 0 {
  350. c.conn.SetWriteDeadline(time.Now().Add(c.writeTimeout))
  351. }
  352. if cmd != "" {
  353. c.writeCommand(cmd, args)
  354. }
  355. if err := c.bw.Flush(); err != nil {
  356. return nil, c.fatal(err)
  357. }
  358. if c.readTimeout != 0 {
  359. c.conn.SetReadDeadline(time.Now().Add(c.readTimeout))
  360. }
  361. if cmd == "" {
  362. reply := make([]interface{}, pending)
  363. for i := range reply {
  364. r, e := c.readReply()
  365. if e != nil {
  366. return nil, c.fatal(e)
  367. }
  368. reply[i] = r
  369. }
  370. return reply, nil
  371. }
  372. var err error
  373. var reply interface{}
  374. for i := 0; i <= pending; i++ {
  375. var e error
  376. if reply, e = c.readReply(); e != nil {
  377. return nil, c.fatal(e)
  378. }
  379. if e, ok := reply.(Error); ok && err == nil {
  380. err = e
  381. }
  382. }
  383. return reply, err
  384. }