conn.go 15 KB

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