473352d207b60c8d6ad83fa0f765cdf2d7a31d8b.svn-base 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package endpoint
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/go-kit/kit/endpoint"
  6. "github.com/longjoy/micro-go-course/section25/goods/service"
  7. "golang.org/x/time/rate"
  8. )
  9. type GoodsEndpoints struct {
  10. GoodsDetailEndpoint endpoint.Endpoint
  11. }
  12. // 服务发现请求结构体
  13. type GoodsDetailRequest struct {
  14. Id string
  15. }
  16. // 服务发现响应结构体
  17. type GoodsDetailResponse struct {
  18. Detail service.GoodsDetailVO `json:"detail"`
  19. Error string `json:"error"`
  20. }
  21. // 创建服务发现的 Endpoint
  22. func MakeGoodsDetailEndpoint(svc service.Service, limiter *rate.Limiter) endpoint.Endpoint {
  23. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  24. if !limiter.Allow() {
  25. // Allow返回false,表示桶内不足一个令牌,应该被限流,默认返回 ErrLimiExceed 异常
  26. return nil, errors.New("ErrLimitExceed")
  27. }
  28. req := request.(GoodsDetailRequest)
  29. detail, err := svc.GetGoodsDetail(ctx, req.Id)
  30. var errString = ""
  31. if err != nil {
  32. errString = err.Error()
  33. return &GoodsDetailResponse{
  34. Detail: detail,
  35. Error: errString,
  36. }, nil
  37. }
  38. return &GoodsDetailResponse{
  39. Detail: detail,
  40. Error: errString,
  41. }, nil
  42. }
  43. }