conn.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. "net/url"
  23. "regexp"
  24. "strconv"
  25. "sync"
  26. "time"
  27. )
  28. // conn is the low-level implementation of Conn
  29. type conn struct {
  30. // Shared
  31. mu sync.Mutex
  32. pending int
  33. err error
  34. conn net.Conn
  35. // Read
  36. readTimeout time.Duration
  37. br *bufio.Reader
  38. // Write
  39. writeTimeout time.Duration
  40. bw *bufio.Writer
  41. // Scratch space for formatting argument length.
  42. // '*' or '$', length, "\r\n"
  43. lenScratch [32]byte
  44. // Scratch space for formatting integers and floats.
  45. numScratch [40]byte
  46. }
  47. // DialTimeout acts like Dial but takes timeouts for establishing the
  48. // connection to the server, writing a command and reading a reply.
  49. //
  50. // Deprecated: Use Dial with options instead.
  51. func DialTimeout(network, address string, connectTimeout, readTimeout, writeTimeout time.Duration) (Conn, error) {
  52. return Dial(network, address,
  53. DialConnectTimeout(connectTimeout),
  54. DialReadTimeout(readTimeout),
  55. DialWriteTimeout(writeTimeout))
  56. }
  57. // DialOption specifies an option for dialing a Redis server.
  58. type DialOption struct {
  59. f func(*dialOptions)
  60. }
  61. type dialOptions struct {
  62. readTimeout time.Duration
  63. writeTimeout time.Duration
  64. dial func(network, addr string) (net.Conn, error)
  65. db int
  66. password string
  67. }
  68. // DialReadTimeout specifies the timeout for reading a single command reply.
  69. func DialReadTimeout(d time.Duration) DialOption {
  70. return DialOption{func(do *dialOptions) {
  71. do.readTimeout = d
  72. }}
  73. }
  74. // DialWriteTimeout specifies the timeout for writing a single command.
  75. func DialWriteTimeout(d time.Duration) DialOption {
  76. return DialOption{func(do *dialOptions) {
  77. do.writeTimeout = d
  78. }}
  79. }
  80. // DialConnectTimeout specifies the timeout for connecting to the Redis server.
  81. func DialConnectTimeout(d time.Duration) DialOption {
  82. return DialOption{func(do *dialOptions) {
  83. dialer := net.Dialer{Timeout: d}
  84. do.dial = dialer.Dial
  85. }}
  86. }
  87. // DialNetDial specifies a custom dial function for creating TCP
  88. // connections. If this option is left out, then net.Dial is
  89. // used. DialNetDial overrides DialConnectTimeout.
  90. func DialNetDial(dial func(network, addr string) (net.Conn, error)) DialOption {
  91. return DialOption{func(do *dialOptions) {
  92. do.dial = dial
  93. }}
  94. }
  95. // DialDatabase specifies the database to select when dialing a connection.
  96. func DialDatabase(db int) DialOption {
  97. return DialOption{func(do *dialOptions) {
  98. do.db = db
  99. }}
  100. }
  101. // DialPassword specifies the password to use when connecting to
  102. // the Redis server.
  103. func DialPassword(password string) DialOption {
  104. return DialOption{func(do *dialOptions) {
  105. do.password = password
  106. }}
  107. }
  108. // Dial connects to the Redis server at the given network and
  109. // address using the specified options.
  110. func Dial(network, address string, options ...DialOption) (Conn, error) {
  111. do := dialOptions{
  112. dial: net.Dial,
  113. }
  114. for _, option := range options {
  115. option.f(&do)
  116. }
  117. netConn, err := do.dial(network, address)
  118. if err != nil {
  119. return nil, err
  120. }
  121. c := &conn{
  122. conn: netConn,
  123. bw: bufio.NewWriter(netConn),
  124. br: bufio.NewReader(netConn),
  125. readTimeout: do.readTimeout,
  126. writeTimeout: do.writeTimeout,
  127. }
  128. if do.password != "" {
  129. if _, err := c.Do("AUTH", do.password); err != nil {
  130. netConn.Close()
  131. return nil, err
  132. }
  133. }
  134. if do.db != 0 {
  135. if _, err := c.Do("SELECT", do.db); err != nil {
  136. netConn.Close()
  137. return nil, err
  138. }
  139. }
  140. return c, nil
  141. }
  142. var pathDBRegexp = regexp.MustCompile(`/(\d+)\z`)
  143. // DialURL connects to a Redis server at the given URL using the Redis
  144. // URI scheme. URLs should follow the draft IANA specification for the
  145. // scheme (https://www.iana.org/assignments/uri-schemes/prov/redis).
  146. func DialURL(rawurl string, options ...DialOption) (Conn, error) {
  147. u, err := url.Parse(rawurl)
  148. if err != nil {
  149. return nil, err
  150. }
  151. if u.Scheme != "redis" {
  152. return nil, fmt.Errorf("invalid redis URL scheme: %s", u.Scheme)
  153. }
  154. // As per the IANA draft spec, the host defaults to localhost and
  155. // the port defaults to 6379.
  156. host, port, err := net.SplitHostPort(u.Host)
  157. if err != nil {
  158. // assume port is missing
  159. host = u.Host
  160. port = "6379"
  161. }
  162. if host == "" {
  163. host = "localhost"
  164. }
  165. address := net.JoinHostPort(host, port)
  166. if u.User != nil {
  167. password, isSet := u.User.Password()
  168. if isSet {
  169. options = append(options, DialPassword(password))
  170. }
  171. }
  172. match := pathDBRegexp.FindStringSubmatch(u.Path)
  173. if len(match) == 2 {
  174. db, err := strconv.Atoi(match[1])
  175. if err != nil {
  176. return nil, fmt.Errorf("invalid database: %s", u.Path[1:])
  177. }
  178. if db != 0 {
  179. options = append(options, DialDatabase(db))
  180. }
  181. } else if u.Path != "" {
  182. return nil, fmt.Errorf("invalid database: %s", u.Path[1:])
  183. }
  184. return Dial("tcp", address, options...)
  185. }
  186. // NewConn returns a new Redigo connection for the given net connection.
  187. func NewConn(netConn net.Conn, readTimeout, writeTimeout time.Duration) Conn {
  188. return &conn{
  189. conn: netConn,
  190. bw: bufio.NewWriter(netConn),
  191. br: bufio.NewReader(netConn),
  192. readTimeout: readTimeout,
  193. writeTimeout: writeTimeout,
  194. }
  195. }
  196. func (c *conn) Close() error {
  197. c.mu.Lock()
  198. err := c.err
  199. if c.err == nil {
  200. c.err = errors.New("redigo: closed")
  201. err = c.conn.Close()
  202. }
  203. c.mu.Unlock()
  204. return err
  205. }
  206. func (c *conn) fatal(err error) error {
  207. c.mu.Lock()
  208. if c.err == nil {
  209. c.err = err
  210. // Close connection to force errors on subsequent calls and to unblock
  211. // other reader or writer.
  212. c.conn.Close()
  213. }
  214. c.mu.Unlock()
  215. return err
  216. }
  217. func (c *conn) Err() error {
  218. c.mu.Lock()
  219. err := c.err
  220. c.mu.Unlock()
  221. return err
  222. }
  223. func (c *conn) writeLen(prefix byte, n int) error {
  224. c.lenScratch[len(c.lenScratch)-1] = '\n'
  225. c.lenScratch[len(c.lenScratch)-2] = '\r'
  226. i := len(c.lenScratch) - 3
  227. for {
  228. c.lenScratch[i] = byte('0' + n%10)
  229. i -= 1
  230. n = n / 10
  231. if n == 0 {
  232. break
  233. }
  234. }
  235. c.lenScratch[i] = prefix
  236. _, err := c.bw.Write(c.lenScratch[i:])
  237. return err
  238. }
  239. func (c *conn) writeString(s string) error {
  240. c.writeLen('$', len(s))
  241. c.bw.WriteString(s)
  242. _, err := c.bw.WriteString("\r\n")
  243. return err
  244. }
  245. func (c *conn) writeBytes(p []byte) error {
  246. c.writeLen('$', len(p))
  247. c.bw.Write(p)
  248. _, err := c.bw.WriteString("\r\n")
  249. return err
  250. }
  251. func (c *conn) writeInt64(n int64) error {
  252. return c.writeBytes(strconv.AppendInt(c.numScratch[:0], n, 10))
  253. }
  254. func (c *conn) writeFloat64(n float64) error {
  255. return c.writeBytes(strconv.AppendFloat(c.numScratch[:0], n, 'g', -1, 64))
  256. }
  257. func (c *conn) writeCommand(cmd string, args []interface{}) (err error) {
  258. c.writeLen('*', 1+len(args))
  259. err = c.writeString(cmd)
  260. for _, arg := range args {
  261. if err != nil {
  262. break
  263. }
  264. switch arg := arg.(type) {
  265. case string:
  266. err = c.writeString(arg)
  267. case []byte:
  268. err = c.writeBytes(arg)
  269. case int:
  270. err = c.writeInt64(int64(arg))
  271. case int64:
  272. err = c.writeInt64(arg)
  273. case float64:
  274. err = c.writeFloat64(arg)
  275. case bool:
  276. if arg {
  277. err = c.writeString("1")
  278. } else {
  279. err = c.writeString("0")
  280. }
  281. case nil:
  282. err = c.writeString("")
  283. default:
  284. var buf bytes.Buffer
  285. fmt.Fprint(&buf, arg)
  286. err = c.writeBytes(buf.Bytes())
  287. }
  288. }
  289. return err
  290. }
  291. type protocolError string
  292. func (pe protocolError) Error() string {
  293. return fmt.Sprintf("redigo: %s (possible server error or unsupported concurrent read by application)", string(pe))
  294. }
  295. func (c *conn) readLine() ([]byte, error) {
  296. p, err := c.br.ReadSlice('\n')
  297. if err == bufio.ErrBufferFull {
  298. return nil, protocolError("long response line")
  299. }
  300. if err != nil {
  301. return nil, err
  302. }
  303. i := len(p) - 2
  304. if i < 0 || p[i] != '\r' {
  305. return nil, protocolError("bad response line terminator")
  306. }
  307. return p[:i], nil
  308. }
  309. // parseLen parses bulk string and array lengths.
  310. func parseLen(p []byte) (int, error) {
  311. if len(p) == 0 {
  312. return -1, protocolError("malformed length")
  313. }
  314. if p[0] == '-' && len(p) == 2 && p[1] == '1' {
  315. // handle $-1 and $-1 null replies.
  316. return -1, nil
  317. }
  318. var n int
  319. for _, b := range p {
  320. n *= 10
  321. if b < '0' || b > '9' {
  322. return -1, protocolError("illegal bytes in length")
  323. }
  324. n += int(b - '0')
  325. }
  326. return n, nil
  327. }
  328. // parseInt parses an integer reply.
  329. func parseInt(p []byte) (interface{}, error) {
  330. if len(p) == 0 {
  331. return 0, protocolError("malformed integer")
  332. }
  333. var negate bool
  334. if p[0] == '-' {
  335. negate = true
  336. p = p[1:]
  337. if len(p) == 0 {
  338. return 0, protocolError("malformed integer")
  339. }
  340. }
  341. var n int64
  342. for _, b := range p {
  343. n *= 10
  344. if b < '0' || b > '9' {
  345. return 0, protocolError("illegal bytes in length")
  346. }
  347. n += int64(b - '0')
  348. }
  349. if negate {
  350. n = -n
  351. }
  352. return n, nil
  353. }
  354. var (
  355. okReply interface{} = "OK"
  356. pongReply interface{} = "PONG"
  357. )
  358. func (c *conn) readReply() (interface{}, error) {
  359. line, err := c.readLine()
  360. if err != nil {
  361. return nil, err
  362. }
  363. if len(line) == 0 {
  364. return nil, protocolError("short response line")
  365. }
  366. switch line[0] {
  367. case '+':
  368. switch {
  369. case len(line) == 3 && line[1] == 'O' && line[2] == 'K':
  370. // Avoid allocation for frequent "+OK" response.
  371. return okReply, nil
  372. case len(line) == 5 && line[1] == 'P' && line[2] == 'O' && line[3] == 'N' && line[4] == 'G':
  373. // Avoid allocation in PING command benchmarks :)
  374. return pongReply, nil
  375. default:
  376. return string(line[1:]), nil
  377. }
  378. case '-':
  379. return Error(string(line[1:])), nil
  380. case ':':
  381. return parseInt(line[1:])
  382. case '$':
  383. n, err := parseLen(line[1:])
  384. if n < 0 || err != nil {
  385. return nil, err
  386. }
  387. p := make([]byte, n)
  388. _, err = io.ReadFull(c.br, p)
  389. if err != nil {
  390. return nil, err
  391. }
  392. if line, err := c.readLine(); err != nil {
  393. return nil, err
  394. } else if len(line) != 0 {
  395. return nil, protocolError("bad bulk string format")
  396. }
  397. return p, nil
  398. case '*':
  399. n, err := parseLen(line[1:])
  400. if n < 0 || err != nil {
  401. return nil, err
  402. }
  403. r := make([]interface{}, n)
  404. for i := range r {
  405. r[i], err = c.readReply()
  406. if err != nil {
  407. return nil, err
  408. }
  409. }
  410. return r, nil
  411. }
  412. return nil, protocolError("unexpected response line")
  413. }
  414. func (c *conn) Send(cmd string, args ...interface{}) error {
  415. c.mu.Lock()
  416. c.pending += 1
  417. c.mu.Unlock()
  418. if c.writeTimeout != 0 {
  419. c.conn.SetWriteDeadline(time.Now().Add(c.writeTimeout))
  420. }
  421. if err := c.writeCommand(cmd, args); err != nil {
  422. return c.fatal(err)
  423. }
  424. return nil
  425. }
  426. func (c *conn) Flush() error {
  427. if c.writeTimeout != 0 {
  428. c.conn.SetWriteDeadline(time.Now().Add(c.writeTimeout))
  429. }
  430. if err := c.bw.Flush(); err != nil {
  431. return c.fatal(err)
  432. }
  433. return nil
  434. }
  435. func (c *conn) Receive() (reply interface{}, err error) {
  436. if c.readTimeout != 0 {
  437. c.conn.SetReadDeadline(time.Now().Add(c.readTimeout))
  438. }
  439. if reply, err = c.readReply(); err != nil {
  440. return nil, c.fatal(err)
  441. }
  442. // When using pub/sub, the number of receives can be greater than the
  443. // number of sends. To enable normal use of the connection after
  444. // unsubscribing from all channels, we do not decrement pending to a
  445. // negative value.
  446. //
  447. // The pending field is decremented after the reply is read to handle the
  448. // case where Receive is called before Send.
  449. c.mu.Lock()
  450. if c.pending > 0 {
  451. c.pending -= 1
  452. }
  453. c.mu.Unlock()
  454. if err, ok := reply.(Error); ok {
  455. return nil, err
  456. }
  457. return
  458. }
  459. func (c *conn) Do(cmd string, args ...interface{}) (interface{}, error) {
  460. c.mu.Lock()
  461. pending := c.pending
  462. c.pending = 0
  463. c.mu.Unlock()
  464. if cmd == "" && pending == 0 {
  465. return nil, nil
  466. }
  467. if c.writeTimeout != 0 {
  468. c.conn.SetWriteDeadline(time.Now().Add(c.writeTimeout))
  469. }
  470. if cmd != "" {
  471. if err := c.writeCommand(cmd, args); err != nil {
  472. return nil, c.fatal(err)
  473. }
  474. }
  475. if err := c.bw.Flush(); err != nil {
  476. return nil, c.fatal(err)
  477. }
  478. if c.readTimeout != 0 {
  479. c.conn.SetReadDeadline(time.Now().Add(c.readTimeout))
  480. }
  481. if cmd == "" {
  482. reply := make([]interface{}, pending)
  483. for i := range reply {
  484. r, e := c.readReply()
  485. if e != nil {
  486. return nil, c.fatal(e)
  487. }
  488. reply[i] = r
  489. }
  490. return reply, nil
  491. }
  492. var err error
  493. var reply interface{}
  494. for i := 0; i <= pending; i++ {
  495. var e error
  496. if reply, e = c.readReply(); e != nil {
  497. return nil, c.fatal(e)
  498. }
  499. if e, ok := reply.(Error); ok && err == nil {
  500. err = e
  501. }
  502. }
  503. return reply, err
  504. }