memcache.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. Copyright 2011 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package memcache provides a client for the memcached cache server.
  14. package memcache
  15. import (
  16. "bufio"
  17. "bytes"
  18. "errors"
  19. "fmt"
  20. "io"
  21. "io/ioutil"
  22. "net"
  23. "strconv"
  24. "strings"
  25. "sync"
  26. "time"
  27. )
  28. // Similar to:
  29. // http://code.google.com/appengine/docs/go/memcache/reference.html
  30. var (
  31. // ErrCacheMiss means that a Get failed because the item wasn't present.
  32. ErrCacheMiss = errors.New("memcache: cache miss")
  33. // ErrCASConflict means that a CompareAndSwap call failed due to the
  34. // cached value being modified between the Get and the CompareAndSwap.
  35. // If the cached value was simply evicted rather than replaced,
  36. // ErrNotStored will be returned instead.
  37. ErrCASConflict = errors.New("memcache: compare-and-swap conflict")
  38. // ErrNotStored means that a conditional write operation (i.e. Add or
  39. // CompareAndSwap) failed because the condition was not satisfied.
  40. ErrNotStored = errors.New("memcache: item not stored")
  41. // ErrServer means that a server error occurred.
  42. ErrServerError = errors.New("memcache: server error")
  43. // ErrNoStats means that no statistics were available.
  44. ErrNoStats = errors.New("memcache: no statistics available")
  45. // ErrMalformedKey is returned when an invalid key is used.
  46. // Keys must be at maximum 250 bytes long, ASCII, and not
  47. // contain whitespace or control characters.
  48. ErrMalformedKey = errors.New("malformed: key is too long or contains invalid characters")
  49. // ErrNoServers is returned when no servers are configured or available.
  50. ErrNoServers = errors.New("memcache: no servers configured or available")
  51. )
  52. // DefaultTimeout is the default socket read/write timeout.
  53. const DefaultTimeout = time.Duration(100) * time.Millisecond
  54. const (
  55. buffered = 8 // arbitrary buffered channel size, for readability
  56. maxIdleConnsPerAddr = 2 // TODO(bradfitz): make this configurable?
  57. )
  58. // resumableError returns true if err is only a protocol-level cache error.
  59. // This is used to determine whether or not a server connection should
  60. // be re-used or not. If an error occurs, by default we don't reuse the
  61. // connection, unless it was just a cache error.
  62. func resumableError(err error) bool {
  63. switch err {
  64. case ErrCacheMiss, ErrCASConflict, ErrNotStored, ErrMalformedKey:
  65. return true
  66. }
  67. return false
  68. }
  69. func legalKey(key string) bool {
  70. if len(key) > 250 {
  71. return false
  72. }
  73. for i := 0; i < len(key); i++ {
  74. if key[i] <= ' ' || key[i] > 0x7e {
  75. return false
  76. }
  77. }
  78. return true
  79. }
  80. var (
  81. crlf = []byte("\r\n")
  82. space = []byte(" ")
  83. resultOK = []byte("OK\r\n")
  84. resultStored = []byte("STORED\r\n")
  85. resultNotStored = []byte("NOT_STORED\r\n")
  86. resultExists = []byte("EXISTS\r\n")
  87. resultNotFound = []byte("NOT_FOUND\r\n")
  88. resultDeleted = []byte("DELETED\r\n")
  89. resultEnd = []byte("END\r\n")
  90. resultOk = []byte("OK\r\n")
  91. resultTouched = []byte("TOUCHED\r\n")
  92. resultClientErrorPrefix = []byte("CLIENT_ERROR ")
  93. )
  94. // New returns a memcache client using the provided server(s)
  95. // with equal weight. If a server is listed multiple times,
  96. // it gets a proportional amount of weight.
  97. func New(server ...string) *Client {
  98. ss := new(ServerList)
  99. ss.SetServers(server...)
  100. return NewFromSelector(ss)
  101. }
  102. // NewFromSelector returns a new Client using the provided ServerSelector.
  103. func NewFromSelector(ss ServerSelector) *Client {
  104. return &Client{selector: ss}
  105. }
  106. // Client is a memcache client.
  107. // It is safe for unlocked use by multiple concurrent goroutines.
  108. type Client struct {
  109. // Timeout specifies the socket read/write timeout.
  110. // If zero, DefaultTimeout is used.
  111. Timeout time.Duration
  112. selector ServerSelector
  113. lk sync.Mutex
  114. freeconn map[string][]*conn
  115. }
  116. // Item is an item to be got or stored in a memcached server.
  117. type Item struct {
  118. // Key is the Item's key (250 bytes maximum).
  119. Key string
  120. // Value is the Item's value.
  121. Value []byte
  122. // Object is the Item's value for use with a Codec.
  123. Object interface{}
  124. // Flags are server-opaque flags whose semantics are entirely
  125. // up to the app.
  126. Flags uint32
  127. // Expiration is the cache expiration time, in seconds: either a relative
  128. // time from now (up to 1 month), or an absolute Unix epoch time.
  129. // Zero means the Item has no expiration time.
  130. Expiration int32
  131. // Compare and swap ID.
  132. casid uint64
  133. }
  134. // conn is a connection to a server.
  135. type conn struct {
  136. nc net.Conn
  137. rw *bufio.ReadWriter
  138. addr net.Addr
  139. c *Client
  140. }
  141. // release returns this connection back to the client's free pool
  142. func (cn *conn) release() {
  143. cn.c.putFreeConn(cn.addr, cn)
  144. }
  145. func (cn *conn) extendDeadline() {
  146. cn.nc.SetDeadline(time.Now().Add(cn.c.netTimeout()))
  147. }
  148. // condRelease releases this connection if the error pointed to by err
  149. // is nil (not an error) or is only a protocol level error (e.g. a
  150. // cache miss). The purpose is to not recycle TCP connections that
  151. // are bad.
  152. func (cn *conn) condRelease(err *error) {
  153. if *err == nil || resumableError(*err) {
  154. cn.release()
  155. } else {
  156. cn.nc.Close()
  157. }
  158. }
  159. func (c *Client) putFreeConn(addr net.Addr, cn *conn) {
  160. c.lk.Lock()
  161. defer c.lk.Unlock()
  162. if c.freeconn == nil {
  163. c.freeconn = make(map[string][]*conn)
  164. }
  165. freelist := c.freeconn[addr.String()]
  166. if len(freelist) >= maxIdleConnsPerAddr {
  167. cn.nc.Close()
  168. return
  169. }
  170. c.freeconn[addr.String()] = append(freelist, cn)
  171. }
  172. func (c *Client) getFreeConn(addr net.Addr) (cn *conn, ok bool) {
  173. c.lk.Lock()
  174. defer c.lk.Unlock()
  175. if c.freeconn == nil {
  176. return nil, false
  177. }
  178. freelist, ok := c.freeconn[addr.String()]
  179. if !ok || len(freelist) == 0 {
  180. return nil, false
  181. }
  182. cn = freelist[len(freelist)-1]
  183. c.freeconn[addr.String()] = freelist[:len(freelist)-1]
  184. return cn, true
  185. }
  186. func (c *Client) netTimeout() time.Duration {
  187. if c.Timeout != 0 {
  188. return c.Timeout
  189. }
  190. return DefaultTimeout
  191. }
  192. // ConnectTimeoutError is the error type used when it takes
  193. // too long to connect to the desired host. This level of
  194. // detail can generally be ignored.
  195. type ConnectTimeoutError struct {
  196. Addr net.Addr
  197. }
  198. func (cte *ConnectTimeoutError) Error() string {
  199. return "memcache: connect timeout to " + cte.Addr.String()
  200. }
  201. func (c *Client) dial(addr net.Addr) (net.Conn, error) {
  202. type connError struct {
  203. cn net.Conn
  204. err error
  205. }
  206. nc, err := net.DialTimeout(addr.Network(), addr.String(), c.netTimeout())
  207. if err == nil {
  208. return nc, nil
  209. }
  210. if ne, ok := err.(net.Error); ok && ne.Timeout() {
  211. return nil, &ConnectTimeoutError{addr}
  212. }
  213. return nil, err
  214. }
  215. func (c *Client) getConn(addr net.Addr) (*conn, error) {
  216. cn, ok := c.getFreeConn(addr)
  217. if ok {
  218. cn.extendDeadline()
  219. return cn, nil
  220. }
  221. nc, err := c.dial(addr)
  222. if err != nil {
  223. return nil, err
  224. }
  225. cn = &conn{
  226. nc: nc,
  227. addr: addr,
  228. rw: bufio.NewReadWriter(bufio.NewReader(nc), bufio.NewWriter(nc)),
  229. c: c,
  230. }
  231. cn.extendDeadline()
  232. return cn, nil
  233. }
  234. func (c *Client) onItem(item *Item, fn func(*Client, *bufio.ReadWriter, *Item) error) error {
  235. addr, err := c.selector.PickServer(item.Key)
  236. if err != nil {
  237. return err
  238. }
  239. cn, err := c.getConn(addr)
  240. if err != nil {
  241. return err
  242. }
  243. defer cn.condRelease(&err)
  244. if err = fn(c, cn.rw, item); err != nil {
  245. return err
  246. }
  247. return nil
  248. }
  249. func (c *Client) FlushAll() error {
  250. return c.selector.Each(c.flushAllFromAddr)
  251. }
  252. // Get gets the item for the given key. ErrCacheMiss is returned for a
  253. // memcache cache miss. The key must be at most 250 bytes in length.
  254. func (c *Client) Get(key string) (item *Item, err error) {
  255. err = c.withKeyAddr(key, func(addr net.Addr) error {
  256. return c.getFromAddr(addr, []string{key}, func(it *Item) { item = it })
  257. })
  258. if err == nil && item == nil {
  259. err = ErrCacheMiss
  260. }
  261. return
  262. }
  263. // Touch updates the expiry for the given key. The seconds parameter is either
  264. // a Unix timestamp or, if seconds is less than 1 month, the number of seconds
  265. // into the future at which time the item will expire. ErrCacheMiss is returned if the
  266. // key is not in the cache. The key must be at most 250 bytes in length.
  267. func (c *Client) Touch(key string, seconds int32) (err error) {
  268. return c.withKeyAddr(key, func(addr net.Addr) error {
  269. return c.touchFromAddr(addr, []string{key}, seconds)
  270. })
  271. }
  272. func (c *Client) withKeyAddr(key string, fn func(net.Addr) error) (err error) {
  273. if !legalKey(key) {
  274. return ErrMalformedKey
  275. }
  276. addr, err := c.selector.PickServer(key)
  277. if err != nil {
  278. return err
  279. }
  280. return fn(addr)
  281. }
  282. func (c *Client) withAddrRw(addr net.Addr, fn func(*bufio.ReadWriter) error) (err error) {
  283. cn, err := c.getConn(addr)
  284. if err != nil {
  285. return err
  286. }
  287. defer cn.condRelease(&err)
  288. return fn(cn.rw)
  289. }
  290. func (c *Client) withKeyRw(key string, fn func(*bufio.ReadWriter) error) error {
  291. return c.withKeyAddr(key, func(addr net.Addr) error {
  292. return c.withAddrRw(addr, fn)
  293. })
  294. }
  295. func (c *Client) getFromAddr(addr net.Addr, keys []string, cb func(*Item)) error {
  296. return c.withAddrRw(addr, func(rw *bufio.ReadWriter) error {
  297. if _, err := fmt.Fprintf(rw, "gets %s\r\n", strings.Join(keys, " ")); err != nil {
  298. return err
  299. }
  300. if err := rw.Flush(); err != nil {
  301. return err
  302. }
  303. if err := parseGetResponse(rw.Reader, cb); err != nil {
  304. return err
  305. }
  306. return nil
  307. })
  308. }
  309. // flushAllFromAddr send the flush_all command to the given addr
  310. func (c *Client) flushAllFromAddr(addr net.Addr) error {
  311. return c.withAddrRw(addr, func(rw *bufio.ReadWriter) error {
  312. if _, err := fmt.Fprintf(rw, "flush_all\r\n"); err != nil {
  313. return err
  314. }
  315. if err := rw.Flush(); err != nil {
  316. return err
  317. }
  318. line, err := rw.ReadSlice('\n')
  319. if err != nil {
  320. return err
  321. }
  322. switch {
  323. case bytes.Equal(line, resultOk):
  324. break
  325. default:
  326. return fmt.Errorf("memcache: unexpected response line from flush_all: %q", string(line))
  327. }
  328. return nil
  329. })
  330. }
  331. func (c *Client) touchFromAddr(addr net.Addr, keys []string, expiration int32) error {
  332. return c.withAddrRw(addr, func(rw *bufio.ReadWriter) error {
  333. for _, key := range keys {
  334. if _, err := fmt.Fprintf(rw, "touch %s %d\r\n", key, expiration); err != nil {
  335. return err
  336. }
  337. if err := rw.Flush(); err != nil {
  338. return err
  339. }
  340. line, err := rw.ReadSlice('\n')
  341. if err != nil {
  342. return err
  343. }
  344. switch {
  345. case bytes.Equal(line, resultTouched):
  346. break
  347. case bytes.Equal(line, resultNotFound):
  348. return ErrCacheMiss
  349. default:
  350. return fmt.Errorf("memcache: unexpected response line from touch: %q", string(line))
  351. }
  352. }
  353. return nil
  354. })
  355. }
  356. // GetMulti is a batch version of Get. The returned map from keys to
  357. // items may have fewer elements than the input slice, due to memcache
  358. // cache misses. Each key must be at most 250 bytes in length.
  359. // If no error is returned, the returned map will also be non-nil.
  360. func (c *Client) GetMulti(keys []string) (map[string]*Item, error) {
  361. var lk sync.Mutex
  362. m := make(map[string]*Item)
  363. addItemToMap := func(it *Item) {
  364. lk.Lock()
  365. defer lk.Unlock()
  366. m[it.Key] = it
  367. }
  368. keyMap := make(map[net.Addr][]string)
  369. for _, key := range keys {
  370. if !legalKey(key) {
  371. return nil, ErrMalformedKey
  372. }
  373. addr, err := c.selector.PickServer(key)
  374. if err != nil {
  375. return nil, err
  376. }
  377. keyMap[addr] = append(keyMap[addr], key)
  378. }
  379. ch := make(chan error, buffered)
  380. for addr, keys := range keyMap {
  381. go func(addr net.Addr, keys []string) {
  382. ch <- c.getFromAddr(addr, keys, addItemToMap)
  383. }(addr, keys)
  384. }
  385. var err error
  386. for _ = range keyMap {
  387. if ge := <-ch; ge != nil {
  388. err = ge
  389. }
  390. }
  391. return m, err
  392. }
  393. // parseGetResponse reads a GET response from r and calls cb for each
  394. // read and allocated Item
  395. func parseGetResponse(r *bufio.Reader, cb func(*Item)) error {
  396. for {
  397. line, err := r.ReadSlice('\n')
  398. if err != nil {
  399. return err
  400. }
  401. if bytes.Equal(line, resultEnd) {
  402. return nil
  403. }
  404. it := new(Item)
  405. size, err := scanGetResponseLine(line, it)
  406. if err != nil {
  407. return err
  408. }
  409. it.Value, err = ioutil.ReadAll(io.LimitReader(r, int64(size)+2))
  410. if err != nil {
  411. return err
  412. }
  413. if !bytes.HasSuffix(it.Value, crlf) {
  414. return fmt.Errorf("memcache: corrupt get result read")
  415. }
  416. it.Value = it.Value[:size]
  417. cb(it)
  418. }
  419. panic("unreached")
  420. }
  421. // scanGetResponseLine populates it and returns the declared size of the item.
  422. // It does not read the bytes of the item.
  423. func scanGetResponseLine(line []byte, it *Item) (size int, err error) {
  424. pattern := "VALUE %s %d %d %d\r\n"
  425. dest := []interface{}{&it.Key, &it.Flags, &size, &it.casid}
  426. if bytes.Count(line, space) == 3 {
  427. pattern = "VALUE %s %d %d\r\n"
  428. dest = dest[:3]
  429. }
  430. n, err := fmt.Sscanf(string(line), pattern, dest...)
  431. if err != nil || n != len(dest) {
  432. return -1, fmt.Errorf("memcache: unexpected line in get response: %q", line)
  433. }
  434. return size, nil
  435. }
  436. // Set writes the given item, unconditionally.
  437. func (c *Client) Set(item *Item) error {
  438. return c.onItem(item, (*Client).set)
  439. }
  440. func (c *Client) set(rw *bufio.ReadWriter, item *Item) error {
  441. return c.populateOne(rw, "set", item)
  442. }
  443. // Add writes the given item, if no value already exists for its
  444. // key. ErrNotStored is returned if that condition is not met.
  445. func (c *Client) Add(item *Item) error {
  446. return c.onItem(item, (*Client).add)
  447. }
  448. func (c *Client) add(rw *bufio.ReadWriter, item *Item) error {
  449. return c.populateOne(rw, "add", item)
  450. }
  451. // Replace writes the given item, but only if the server *does*
  452. // already hold data for this key
  453. func (c *Client) Replace(item *Item) error {
  454. return c.onItem(item, (*Client).replace)
  455. }
  456. func (c *Client) replace(rw *bufio.ReadWriter, item *Item) error {
  457. return c.populateOne(rw, "replace", item)
  458. }
  459. // CompareAndSwap writes the given item that was previously returned
  460. // by Get, if the value was neither modified or evicted between the
  461. // Get and the CompareAndSwap calls. The item's Key should not change
  462. // between calls but all other item fields may differ. ErrCASConflict
  463. // is returned if the value was modified in between the
  464. // calls. ErrNotStored is returned if the value was evicted in between
  465. // the calls.
  466. func (c *Client) CompareAndSwap(item *Item) error {
  467. return c.onItem(item, (*Client).cas)
  468. }
  469. func (c *Client) cas(rw *bufio.ReadWriter, item *Item) error {
  470. return c.populateOne(rw, "cas", item)
  471. }
  472. func (c *Client) populateOne(rw *bufio.ReadWriter, verb string, item *Item) error {
  473. if !legalKey(item.Key) {
  474. return ErrMalformedKey
  475. }
  476. var err error
  477. if verb == "cas" {
  478. _, err = fmt.Fprintf(rw, "%s %s %d %d %d %d\r\n",
  479. verb, item.Key, item.Flags, item.Expiration, len(item.Value), item.casid)
  480. } else {
  481. _, err = fmt.Fprintf(rw, "%s %s %d %d %d\r\n",
  482. verb, item.Key, item.Flags, item.Expiration, len(item.Value))
  483. }
  484. if err != nil {
  485. return err
  486. }
  487. if _, err = rw.Write(item.Value); err != nil {
  488. return err
  489. }
  490. if _, err := rw.Write(crlf); err != nil {
  491. return err
  492. }
  493. if err := rw.Flush(); err != nil {
  494. return err
  495. }
  496. line, err := rw.ReadSlice('\n')
  497. if err != nil {
  498. return err
  499. }
  500. switch {
  501. case bytes.Equal(line, resultStored):
  502. return nil
  503. case bytes.Equal(line, resultNotStored):
  504. return ErrNotStored
  505. case bytes.Equal(line, resultExists):
  506. return ErrCASConflict
  507. case bytes.Equal(line, resultNotFound):
  508. return ErrCacheMiss
  509. }
  510. return fmt.Errorf("memcache: unexpected response line from %q: %q", verb, string(line))
  511. }
  512. func writeReadLine(rw *bufio.ReadWriter, format string, args ...interface{}) ([]byte, error) {
  513. _, err := fmt.Fprintf(rw, format, args...)
  514. if err != nil {
  515. return nil, err
  516. }
  517. if err := rw.Flush(); err != nil {
  518. return nil, err
  519. }
  520. line, err := rw.ReadSlice('\n')
  521. return line, err
  522. }
  523. func writeExpectf(rw *bufio.ReadWriter, expect []byte, format string, args ...interface{}) error {
  524. line, err := writeReadLine(rw, format, args...)
  525. if err != nil {
  526. return err
  527. }
  528. switch {
  529. case bytes.Equal(line, resultOK):
  530. return nil
  531. case bytes.Equal(line, expect):
  532. return nil
  533. case bytes.Equal(line, resultNotStored):
  534. return ErrNotStored
  535. case bytes.Equal(line, resultExists):
  536. return ErrCASConflict
  537. case bytes.Equal(line, resultNotFound):
  538. return ErrCacheMiss
  539. }
  540. return fmt.Errorf("memcache: unexpected response line: %q", string(line))
  541. }
  542. // Delete deletes the item with the provided key. The error ErrCacheMiss is
  543. // returned if the item didn't already exist in the cache.
  544. func (c *Client) Delete(key string) error {
  545. return c.withKeyRw(key, func(rw *bufio.ReadWriter) error {
  546. return writeExpectf(rw, resultDeleted, "delete %s\r\n", key)
  547. })
  548. }
  549. // DeleteAll deletes all items in the cache.
  550. func (c *Client) DeleteAll() error {
  551. return c.withKeyRw("", func(rw *bufio.ReadWriter) error {
  552. return writeExpectf(rw, resultDeleted, "flush_all\r\n")
  553. })
  554. }
  555. // Increment atomically increments key by delta. The return value is
  556. // the new value after being incremented or an error. If the value
  557. // didn't exist in memcached the error is ErrCacheMiss. The value in
  558. // memcached must be an decimal number, or an error will be returned.
  559. // On 64-bit overflow, the new value wraps around.
  560. func (c *Client) Increment(key string, delta uint64) (newValue uint64, err error) {
  561. return c.incrDecr("incr", key, delta)
  562. }
  563. // Decrement atomically decrements key by delta. The return value is
  564. // the new value after being decremented or an error. If the value
  565. // didn't exist in memcached the error is ErrCacheMiss. The value in
  566. // memcached must be an decimal number, or an error will be returned.
  567. // On underflow, the new value is capped at zero and does not wrap
  568. // around.
  569. func (c *Client) Decrement(key string, delta uint64) (newValue uint64, err error) {
  570. return c.incrDecr("decr", key, delta)
  571. }
  572. func (c *Client) incrDecr(verb, key string, delta uint64) (uint64, error) {
  573. var val uint64
  574. err := c.withKeyRw(key, func(rw *bufio.ReadWriter) error {
  575. line, err := writeReadLine(rw, "%s %s %d\r\n", verb, key, delta)
  576. if err != nil {
  577. return err
  578. }
  579. switch {
  580. case bytes.Equal(line, resultNotFound):
  581. return ErrCacheMiss
  582. case bytes.HasPrefix(line, resultClientErrorPrefix):
  583. errMsg := line[len(resultClientErrorPrefix) : len(line)-2]
  584. return errors.New("memcache: client error: " + string(errMsg))
  585. }
  586. val, err = strconv.ParseUint(string(line[:len(line)-2]), 10, 64)
  587. if err != nil {
  588. return err
  589. }
  590. return nil
  591. })
  592. return val, err
  593. }