provider_api_hystrix.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package provider
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/common.in/cache"
  6. "gd_management/common.in/config"
  7. "gd_management/common.in/utils"
  8. "gd_management/errors"
  9. "github.com/astaxie/beego/orm"
  10. "go.uber.org/zap"
  11. )
  12. func GetHystrix(
  13. ctx context.Context,
  14. req *apis.GetProviderApiHystrixReq,
  15. reply *apis.GetProviderApiHystrixReply,
  16. ) (err error) {
  17. o := orm.NewOrm()
  18. err = o.Raw("select id, max_concurrent_requests, request_volume_threshold, sleep_window, error_percent_threshold, period, is_on from t_gd_provider_api_hystrix where provider_api_code = ?", req.ProviderApiCode).
  19. QueryRow(&reply.ID, &reply.MaxConcurrentRequests, &reply.RequestVolumeThreshold, &reply.SleepWindow, &reply.ErrorPercentThreshold, &reply.Period, &reply.IsOn)
  20. if err != nil && err != orm.ErrNoRows {
  21. l.Error("mysql",
  22. zap.String("sql", "select * from t_gd_provider_api_hystrix where provider_api_code"),
  23. zap.String("fields", utils.MarshalJsonString(req)),
  24. zap.String("error", err.Error()))
  25. return errors.DataBaseError
  26. }
  27. return nil
  28. }
  29. func UpdateHystrix(
  30. ctx context.Context,
  31. req *apis.UpdateProviderApiHystrixReq,
  32. reply *apis.UpdateProviderApiHystrixReply,
  33. ) (err error) {
  34. if req.ID == 0 {
  35. // 创建
  36. if _, err = orm.NewOrm().Insert(req); err != nil {
  37. l.Error("mysql",
  38. zap.String("sql", "insert t_gd_provider_api_hystrix"),
  39. zap.String("fields", utils.MarshalJsonString(req)),
  40. zap.String("error", err.Error()))
  41. return errors.DataBaseError
  42. }
  43. } else {
  44. // 更新
  45. if _, err = orm.NewOrm().Raw("UPDATE t_gd_provider_api_hystrix SET max_concurrent_requests=?,request_volume_threshold=?,sleep_window=?,error_percent_threshold=?,period=?,is_on=? WHERE id=?", req.MaxConcurrentRequests, req.RequestVolumeThreshold, req.SleepWindow, req.ErrorPercentThreshold, req.Period, req.IsOn, req.ID).Exec(); err != nil {
  46. l.Error("mysql",
  47. zap.String("sql", "update t_gd_provider_api_hystrix"),
  48. zap.String("fields", utils.MarshalJsonString(req)),
  49. zap.String("error", err.Error()))
  50. return errors.DataBaseError
  51. }
  52. }
  53. for i := 0; i < 3; i++ {
  54. if _, err = cache.Redis.Publish(config.Conf.ThirdPart.HystrixPublishChannel, "hystrix-update"); err == nil {
  55. break
  56. }
  57. }
  58. return nil
  59. }