本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.5KB

  1. package memcached
  2. import (
  3. "errors"
  4. "io"
  5. "github.com/couchbase/gomemcached"
  6. )
  7. var errNoConn = errors.New("no connection")
  8. // UnwrapMemcachedError converts memcached errors to normal responses.
  9. //
  10. // If the error is a memcached response, declare the error to be nil
  11. // so a client can handle the status without worrying about whether it
  12. // indicates success or failure.
  13. func UnwrapMemcachedError(rv *gomemcached.MCResponse,
  14. err error) (*gomemcached.MCResponse, error) {
  15. if rv == err {
  16. return rv, nil
  17. }
  18. return rv, err
  19. }
  20. // ReceiveHook is called after every packet is received (or attempted to be)
  21. var ReceiveHook func(*gomemcached.MCResponse, int, error)
  22. func getResponse(s io.Reader, hdrBytes []byte) (rv *gomemcached.MCResponse, n int, err error) {
  23. if s == nil {
  24. return nil, 0, errNoConn
  25. }
  26. rv = &gomemcached.MCResponse{}
  27. n, err = rv.Receive(s, hdrBytes)
  28. if ReceiveHook != nil {
  29. ReceiveHook(rv, n, err)
  30. }
  31. if err == nil && (rv.Status != gomemcached.SUCCESS && rv.Status != gomemcached.AUTH_CONTINUE) {
  32. err = rv
  33. }
  34. return rv, n, err
  35. }
  36. // TransmitHook is called after each packet is transmitted.
  37. var TransmitHook func(*gomemcached.MCRequest, int, error)
  38. func transmitRequest(o io.Writer, req *gomemcached.MCRequest) (int, error) {
  39. if o == nil {
  40. return 0, errNoConn
  41. }
  42. n, err := req.Transmit(o)
  43. if TransmitHook != nil {
  44. TransmitHook(req, n, err)
  45. }
  46. return n, err
  47. }
  48. func transmitResponse(o io.Writer, res *gomemcached.MCResponse) (int, error) {
  49. if o == nil {
  50. return 0, errNoConn
  51. }
  52. n, err := res.Transmit(o)
  53. return n, err
  54. }
上海开阖软件有限公司 沪ICP备12045867号-1