http urls monitor.

utils.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "crypto/tls"
  11. "database/sql/driver"
  12. "encoding/binary"
  13. "fmt"
  14. "io"
  15. "strings"
  16. "sync"
  17. "sync/atomic"
  18. "time"
  19. )
  20. // Registry for custom tls.Configs
  21. var (
  22. tlsConfigLock sync.RWMutex
  23. tlsConfigRegistry map[string]*tls.Config
  24. )
  25. // RegisterTLSConfig registers a custom tls.Config to be used with sql.Open.
  26. // Use the key as a value in the DSN where tls=value.
  27. //
  28. // Note: The provided tls.Config is exclusively owned by the driver after
  29. // registering it.
  30. //
  31. // rootCertPool := x509.NewCertPool()
  32. // pem, err := ioutil.ReadFile("/path/ca-cert.pem")
  33. // if err != nil {
  34. // log.Fatal(err)
  35. // }
  36. // if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
  37. // log.Fatal("Failed to append PEM.")
  38. // }
  39. // clientCert := make([]tls.Certificate, 0, 1)
  40. // certs, err := tls.LoadX509KeyPair("/path/client-cert.pem", "/path/client-key.pem")
  41. // if err != nil {
  42. // log.Fatal(err)
  43. // }
  44. // clientCert = append(clientCert, certs)
  45. // mysql.RegisterTLSConfig("custom", &tls.Config{
  46. // RootCAs: rootCertPool,
  47. // Certificates: clientCert,
  48. // })
  49. // db, err := sql.Open("mysql", "user@tcp(localhost:3306)/test?tls=custom")
  50. //
  51. func RegisterTLSConfig(key string, config *tls.Config) error {
  52. if _, isBool := readBool(key); isBool || strings.ToLower(key) == "skip-verify" {
  53. return fmt.Errorf("key '%s' is reserved", key)
  54. }
  55. tlsConfigLock.Lock()
  56. if tlsConfigRegistry == nil {
  57. tlsConfigRegistry = make(map[string]*tls.Config)
  58. }
  59. tlsConfigRegistry[key] = config
  60. tlsConfigLock.Unlock()
  61. return nil
  62. }
  63. // DeregisterTLSConfig removes the tls.Config associated with key.
  64. func DeregisterTLSConfig(key string) {
  65. tlsConfigLock.Lock()
  66. if tlsConfigRegistry != nil {
  67. delete(tlsConfigRegistry, key)
  68. }
  69. tlsConfigLock.Unlock()
  70. }
  71. func getTLSConfigClone(key string) (config *tls.Config) {
  72. tlsConfigLock.RLock()
  73. if v, ok := tlsConfigRegistry[key]; ok {
  74. config = cloneTLSConfig(v)
  75. }
  76. tlsConfigLock.RUnlock()
  77. return
  78. }
  79. // Returns the bool value of the input.
  80. // The 2nd return value indicates if the input was a valid bool value
  81. func readBool(input string) (value bool, valid bool) {
  82. switch input {
  83. case "1", "true", "TRUE", "True":
  84. return true, true
  85. case "0", "false", "FALSE", "False":
  86. return false, true
  87. }
  88. // Not a valid bool value
  89. return
  90. }
  91. /******************************************************************************
  92. * Time related utils *
  93. ******************************************************************************/
  94. // NullTime represents a time.Time that may be NULL.
  95. // NullTime implements the Scanner interface so
  96. // it can be used as a scan destination:
  97. //
  98. // var nt NullTime
  99. // err := db.QueryRow("SELECT time FROM foo WHERE id=?", id).Scan(&nt)
  100. // ...
  101. // if nt.Valid {
  102. // // use nt.Time
  103. // } else {
  104. // // NULL value
  105. // }
  106. //
  107. // This NullTime implementation is not driver-specific
  108. type NullTime struct {
  109. Time time.Time
  110. Valid bool // Valid is true if Time is not NULL
  111. }
  112. // Scan implements the Scanner interface.
  113. // The value type must be time.Time or string / []byte (formatted time-string),
  114. // otherwise Scan fails.
  115. func (nt *NullTime) Scan(value interface{}) (err error) {
  116. if value == nil {
  117. nt.Time, nt.Valid = time.Time{}, false
  118. return
  119. }
  120. switch v := value.(type) {
  121. case time.Time:
  122. nt.Time, nt.Valid = v, true
  123. return
  124. case []byte:
  125. nt.Time, err = parseDateTime(string(v), time.UTC)
  126. nt.Valid = (err == nil)
  127. return
  128. case string:
  129. nt.Time, err = parseDateTime(v, time.UTC)
  130. nt.Valid = (err == nil)
  131. return
  132. }
  133. nt.Valid = false
  134. return fmt.Errorf("Can't convert %T to time.Time", value)
  135. }
  136. // Value implements the driver Valuer interface.
  137. func (nt NullTime) Value() (driver.Value, error) {
  138. if !nt.Valid {
  139. return nil, nil
  140. }
  141. return nt.Time, nil
  142. }
  143. func parseDateTime(str string, loc *time.Location) (t time.Time, err error) {
  144. base := "0000-00-00 00:00:00.0000000"
  145. switch len(str) {
  146. case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
  147. if str == base[:len(str)] {
  148. return
  149. }
  150. t, err = time.Parse(timeFormat[:len(str)], str)
  151. default:
  152. err = fmt.Errorf("invalid time string: %s", str)
  153. return
  154. }
  155. // Adjust location
  156. if err == nil && loc != time.UTC {
  157. y, mo, d := t.Date()
  158. h, mi, s := t.Clock()
  159. t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
  160. }
  161. return
  162. }
  163. func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (driver.Value, error) {
  164. switch num {
  165. case 0:
  166. return time.Time{}, nil
  167. case 4:
  168. return time.Date(
  169. int(binary.LittleEndian.Uint16(data[:2])), // year
  170. time.Month(data[2]), // month
  171. int(data[3]), // day
  172. 0, 0, 0, 0,
  173. loc,
  174. ), nil
  175. case 7:
  176. return time.Date(
  177. int(binary.LittleEndian.Uint16(data[:2])), // year
  178. time.Month(data[2]), // month
  179. int(data[3]), // day
  180. int(data[4]), // hour
  181. int(data[5]), // minutes
  182. int(data[6]), // seconds
  183. 0,
  184. loc,
  185. ), nil
  186. case 11:
  187. return time.Date(
  188. int(binary.LittleEndian.Uint16(data[:2])), // year
  189. time.Month(data[2]), // month
  190. int(data[3]), // day
  191. int(data[4]), // hour
  192. int(data[5]), // minutes
  193. int(data[6]), // seconds
  194. int(binary.LittleEndian.Uint32(data[7:11]))*1000, // nanoseconds
  195. loc,
  196. ), nil
  197. }
  198. return nil, fmt.Errorf("invalid DATETIME packet length %d", num)
  199. }
  200. // zeroDateTime is used in formatBinaryDateTime to avoid an allocation
  201. // if the DATE or DATETIME has the zero value.
  202. // It must never be changed.
  203. // The current behavior depends on database/sql copying the result.
  204. var zeroDateTime = []byte("0000-00-00 00:00:00.000000")
  205. const digits01 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  206. const digits10 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
  207. func formatBinaryDateTime(src []byte, length uint8, justTime bool) (driver.Value, error) {
  208. // length expects the deterministic length of the zero value,
  209. // negative time and 100+ hours are automatically added if needed
  210. if len(src) == 0 {
  211. if justTime {
  212. return zeroDateTime[11 : 11+length], nil
  213. }
  214. return zeroDateTime[:length], nil
  215. }
  216. var dst []byte // return value
  217. var pt, p1, p2, p3 byte // current digit pair
  218. var zOffs byte // offset of value in zeroDateTime
  219. if justTime {
  220. switch length {
  221. case
  222. 8, // time (can be up to 10 when negative and 100+ hours)
  223. 10, 11, 12, 13, 14, 15: // time with fractional seconds
  224. default:
  225. return nil, fmt.Errorf("illegal TIME length %d", length)
  226. }
  227. switch len(src) {
  228. case 8, 12:
  229. default:
  230. return nil, fmt.Errorf("invalid TIME packet length %d", len(src))
  231. }
  232. // +2 to enable negative time and 100+ hours
  233. dst = make([]byte, 0, length+2)
  234. if src[0] == 1 {
  235. dst = append(dst, '-')
  236. }
  237. if src[1] != 0 {
  238. hour := uint16(src[1])*24 + uint16(src[5])
  239. pt = byte(hour / 100)
  240. p1 = byte(hour - 100*uint16(pt))
  241. dst = append(dst, digits01[pt])
  242. } else {
  243. p1 = src[5]
  244. }
  245. zOffs = 11
  246. src = src[6:]
  247. } else {
  248. switch length {
  249. case 10, 19, 21, 22, 23, 24, 25, 26:
  250. default:
  251. t := "DATE"
  252. if length > 10 {
  253. t += "TIME"
  254. }
  255. return nil, fmt.Errorf("illegal %s length %d", t, length)
  256. }
  257. switch len(src) {
  258. case 4, 7, 11:
  259. default:
  260. t := "DATE"
  261. if length > 10 {
  262. t += "TIME"
  263. }
  264. return nil, fmt.Errorf("illegal %s packet length %d", t, len(src))
  265. }
  266. dst = make([]byte, 0, length)
  267. // start with the date
  268. year := binary.LittleEndian.Uint16(src[:2])
  269. pt = byte(year / 100)
  270. p1 = byte(year - 100*uint16(pt))
  271. p2, p3 = src[2], src[3]
  272. dst = append(dst,
  273. digits10[pt], digits01[pt],
  274. digits10[p1], digits01[p1], '-',
  275. digits10[p2], digits01[p2], '-',
  276. digits10[p3], digits01[p3],
  277. )
  278. if length == 10 {
  279. return dst, nil
  280. }
  281. if len(src) == 4 {
  282. return append(dst, zeroDateTime[10:length]...), nil
  283. }
  284. dst = append(dst, ' ')
  285. p1 = src[4] // hour
  286. src = src[5:]
  287. }
  288. // p1 is 2-digit hour, src is after hour
  289. p2, p3 = src[0], src[1]
  290. dst = append(dst,
  291. digits10[p1], digits01[p1], ':',
  292. digits10[p2], digits01[p2], ':',
  293. digits10[p3], digits01[p3],
  294. )
  295. if length <= byte(len(dst)) {
  296. return dst, nil
  297. }
  298. src = src[2:]
  299. if len(src) == 0 {
  300. return append(dst, zeroDateTime[19:zOffs+length]...), nil
  301. }
  302. microsecs := binary.LittleEndian.Uint32(src[:4])
  303. p1 = byte(microsecs / 10000)
  304. microsecs -= 10000 * uint32(p1)
  305. p2 = byte(microsecs / 100)
  306. microsecs -= 100 * uint32(p2)
  307. p3 = byte(microsecs)
  308. switch decimals := zOffs + length - 20; decimals {
  309. default:
  310. return append(dst, '.',
  311. digits10[p1], digits01[p1],
  312. digits10[p2], digits01[p2],
  313. digits10[p3], digits01[p3],
  314. ), nil
  315. case 1:
  316. return append(dst, '.',
  317. digits10[p1],
  318. ), nil
  319. case 2:
  320. return append(dst, '.',
  321. digits10[p1], digits01[p1],
  322. ), nil
  323. case 3:
  324. return append(dst, '.',
  325. digits10[p1], digits01[p1],
  326. digits10[p2],
  327. ), nil
  328. case 4:
  329. return append(dst, '.',
  330. digits10[p1], digits01[p1],
  331. digits10[p2], digits01[p2],
  332. ), nil
  333. case 5:
  334. return append(dst, '.',
  335. digits10[p1], digits01[p1],
  336. digits10[p2], digits01[p2],
  337. digits10[p3],
  338. ), nil
  339. }
  340. }
  341. /******************************************************************************
  342. * Convert from and to bytes *
  343. ******************************************************************************/
  344. func uint64ToBytes(n uint64) []byte {
  345. return []byte{
  346. byte(n),
  347. byte(n >> 8),
  348. byte(n >> 16),
  349. byte(n >> 24),
  350. byte(n >> 32),
  351. byte(n >> 40),
  352. byte(n >> 48),
  353. byte(n >> 56),
  354. }
  355. }
  356. func uint64ToString(n uint64) []byte {
  357. var a [20]byte
  358. i := 20
  359. // U+0030 = 0
  360. // ...
  361. // U+0039 = 9
  362. var q uint64
  363. for n >= 10 {
  364. i--
  365. q = n / 10
  366. a[i] = uint8(n-q*10) + 0x30
  367. n = q
  368. }
  369. i--
  370. a[i] = uint8(n) + 0x30
  371. return a[i:]
  372. }
  373. // treats string value as unsigned integer representation
  374. func stringToInt(b []byte) int {
  375. val := 0
  376. for i := range b {
  377. val *= 10
  378. val += int(b[i] - 0x30)
  379. }
  380. return val
  381. }
  382. // returns the string read as a bytes slice, wheter the value is NULL,
  383. // the number of bytes read and an error, in case the string is longer than
  384. // the input slice
  385. func readLengthEncodedString(b []byte) ([]byte, bool, int, error) {
  386. // Get length
  387. num, isNull, n := readLengthEncodedInteger(b)
  388. if num < 1 {
  389. return b[n:n], isNull, n, nil
  390. }
  391. n += int(num)
  392. // Check data length
  393. if len(b) >= n {
  394. return b[n-int(num) : n : n], false, n, nil
  395. }
  396. return nil, false, n, io.EOF
  397. }
  398. // returns the number of bytes skipped and an error, in case the string is
  399. // longer than the input slice
  400. func skipLengthEncodedString(b []byte) (int, error) {
  401. // Get length
  402. num, _, n := readLengthEncodedInteger(b)
  403. if num < 1 {
  404. return n, nil
  405. }
  406. n += int(num)
  407. // Check data length
  408. if len(b) >= n {
  409. return n, nil
  410. }
  411. return n, io.EOF
  412. }
  413. // returns the number read, whether the value is NULL and the number of bytes read
  414. func readLengthEncodedInteger(b []byte) (uint64, bool, int) {
  415. // See issue #349
  416. if len(b) == 0 {
  417. return 0, true, 1
  418. }
  419. switch b[0] {
  420. // 251: NULL
  421. case 0xfb:
  422. return 0, true, 1
  423. // 252: value of following 2
  424. case 0xfc:
  425. return uint64(b[1]) | uint64(b[2])<<8, false, 3
  426. // 253: value of following 3
  427. case 0xfd:
  428. return uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16, false, 4
  429. // 254: value of following 8
  430. case 0xfe:
  431. return uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16 |
  432. uint64(b[4])<<24 | uint64(b[5])<<32 | uint64(b[6])<<40 |
  433. uint64(b[7])<<48 | uint64(b[8])<<56,
  434. false, 9
  435. }
  436. // 0-250: value of first byte
  437. return uint64(b[0]), false, 1
  438. }
  439. // encodes a uint64 value and appends it to the given bytes slice
  440. func appendLengthEncodedInteger(b []byte, n uint64) []byte {
  441. switch {
  442. case n <= 250:
  443. return append(b, byte(n))
  444. case n <= 0xffff:
  445. return append(b, 0xfc, byte(n), byte(n>>8))
  446. case n <= 0xffffff:
  447. return append(b, 0xfd, byte(n), byte(n>>8), byte(n>>16))
  448. }
  449. return append(b, 0xfe, byte(n), byte(n>>8), byte(n>>16), byte(n>>24),
  450. byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
  451. }
  452. // reserveBuffer checks cap(buf) and expand buffer to len(buf) + appendSize.
  453. // If cap(buf) is not enough, reallocate new buffer.
  454. func reserveBuffer(buf []byte, appendSize int) []byte {
  455. newSize := len(buf) + appendSize
  456. if cap(buf) < newSize {
  457. // Grow buffer exponentially
  458. newBuf := make([]byte, len(buf)*2+appendSize)
  459. copy(newBuf, buf)
  460. buf = newBuf
  461. }
  462. return buf[:newSize]
  463. }
  464. // escapeBytesBackslash escapes []byte with backslashes (\)
  465. // This escapes the contents of a string (provided as []byte) by adding backslashes before special
  466. // characters, and turning others into specific escape sequences, such as
  467. // turning newlines into \n and null bytes into \0.
  468. // https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L823-L932
  469. func escapeBytesBackslash(buf, v []byte) []byte {
  470. pos := len(buf)
  471. buf = reserveBuffer(buf, len(v)*2)
  472. for _, c := range v {
  473. switch c {
  474. case '\x00':
  475. buf[pos] = '\\'
  476. buf[pos+1] = '0'
  477. pos += 2
  478. case '\n':
  479. buf[pos] = '\\'
  480. buf[pos+1] = 'n'
  481. pos += 2
  482. case '\r':
  483. buf[pos] = '\\'
  484. buf[pos+1] = 'r'
  485. pos += 2
  486. case '\x1a':
  487. buf[pos] = '\\'
  488. buf[pos+1] = 'Z'
  489. pos += 2
  490. case '\'':
  491. buf[pos] = '\\'
  492. buf[pos+1] = '\''
  493. pos += 2
  494. case '"':
  495. buf[pos] = '\\'
  496. buf[pos+1] = '"'
  497. pos += 2
  498. case '\\':
  499. buf[pos] = '\\'
  500. buf[pos+1] = '\\'
  501. pos += 2
  502. default:
  503. buf[pos] = c
  504. pos++
  505. }
  506. }
  507. return buf[:pos]
  508. }
  509. // escapeStringBackslash is similar to escapeBytesBackslash but for string.
  510. func escapeStringBackslash(buf []byte, v string) []byte {
  511. pos := len(buf)
  512. buf = reserveBuffer(buf, len(v)*2)
  513. for i := 0; i < len(v); i++ {
  514. c := v[i]
  515. switch c {
  516. case '\x00':
  517. buf[pos] = '\\'
  518. buf[pos+1] = '0'
  519. pos += 2
  520. case '\n':
  521. buf[pos] = '\\'
  522. buf[pos+1] = 'n'
  523. pos += 2
  524. case '\r':
  525. buf[pos] = '\\'
  526. buf[pos+1] = 'r'
  527. pos += 2
  528. case '\x1a':
  529. buf[pos] = '\\'
  530. buf[pos+1] = 'Z'
  531. pos += 2
  532. case '\'':
  533. buf[pos] = '\\'
  534. buf[pos+1] = '\''
  535. pos += 2
  536. case '"':
  537. buf[pos] = '\\'
  538. buf[pos+1] = '"'
  539. pos += 2
  540. case '\\':
  541. buf[pos] = '\\'
  542. buf[pos+1] = '\\'
  543. pos += 2
  544. default:
  545. buf[pos] = c
  546. pos++
  547. }
  548. }
  549. return buf[:pos]
  550. }
  551. // escapeBytesQuotes escapes apostrophes in []byte by doubling them up.
  552. // This escapes the contents of a string by doubling up any apostrophes that
  553. // it contains. This is used when the NO_BACKSLASH_ESCAPES SQL_MODE is in
  554. // effect on the server.
  555. // https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L963-L1038
  556. func escapeBytesQuotes(buf, v []byte) []byte {
  557. pos := len(buf)
  558. buf = reserveBuffer(buf, len(v)*2)
  559. for _, c := range v {
  560. if c == '\'' {
  561. buf[pos] = '\''
  562. buf[pos+1] = '\''
  563. pos += 2
  564. } else {
  565. buf[pos] = c
  566. pos++
  567. }
  568. }
  569. return buf[:pos]
  570. }
  571. // escapeStringQuotes is similar to escapeBytesQuotes but for string.
  572. func escapeStringQuotes(buf []byte, v string) []byte {
  573. pos := len(buf)
  574. buf = reserveBuffer(buf, len(v)*2)
  575. for i := 0; i < len(v); i++ {
  576. c := v[i]
  577. if c == '\'' {
  578. buf[pos] = '\''
  579. buf[pos+1] = '\''
  580. pos += 2
  581. } else {
  582. buf[pos] = c
  583. pos++
  584. }
  585. }
  586. return buf[:pos]
  587. }
  588. /******************************************************************************
  589. * Sync utils *
  590. ******************************************************************************/
  591. // noCopy may be embedded into structs which must not be copied
  592. // after the first use.
  593. //
  594. // See https://github.com/golang/go/issues/8005#issuecomment-190753527
  595. // for details.
  596. type noCopy struct{}
  597. // Lock is a no-op used by -copylocks checker from `go vet`.
  598. func (*noCopy) Lock() {}
  599. // atomicBool is a wrapper around uint32 for usage as a boolean value with
  600. // atomic access.
  601. type atomicBool struct {
  602. _noCopy noCopy
  603. value uint32
  604. }
  605. // IsSet returns wether the current boolean value is true
  606. func (ab *atomicBool) IsSet() bool {
  607. return atomic.LoadUint32(&ab.value) > 0
  608. }
  609. // Set sets the value of the bool regardless of the previous value
  610. func (ab *atomicBool) Set(value bool) {
  611. if value {
  612. atomic.StoreUint32(&ab.value, 1)
  613. } else {
  614. atomic.StoreUint32(&ab.value, 0)
  615. }
  616. }
  617. // TrySet sets the value of the bool and returns wether the value changed
  618. func (ab *atomicBool) TrySet(value bool) bool {
  619. if value {
  620. return atomic.SwapUint32(&ab.value, 1) == 0
  621. }
  622. return atomic.SwapUint32(&ab.value, 0) > 0
  623. }
  624. // atomicError is a wrapper for atomically accessed error values
  625. type atomicError struct {
  626. _noCopy noCopy
  627. value atomic.Value
  628. }
  629. // Set sets the error value regardless of the previous value.
  630. // The value must not be nil
  631. func (ae *atomicError) Set(value error) {
  632. ae.value.Store(value)
  633. }
  634. // Value returns the current error value
  635. func (ae *atomicError) Value() error {
  636. if v := ae.value.Load(); v != nil {
  637. // this will panic if the value doesn't implement the error interface
  638. return v.(error)
  639. }
  640. return nil
  641. }