sheddergroup.go 789 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package load
  2. import (
  3. "io"
  4. "github.com/tal-tech/go-zero/core/syncx"
  5. )
  6. // A ShedderGroup is a manager to manage key based shedders.
  7. type ShedderGroup struct {
  8. options []ShedderOption
  9. manager *syncx.ResourceManager
  10. }
  11. // NewShedderGroup returns a ShedderGroup.
  12. func NewShedderGroup(opts ...ShedderOption) *ShedderGroup {
  13. return &ShedderGroup{
  14. options: opts,
  15. manager: syncx.NewResourceManager(),
  16. }
  17. }
  18. // GetShedder gets the Shedder for the given key.
  19. func (g *ShedderGroup) GetShedder(key string) Shedder {
  20. shedder, _ := g.manager.GetResource(key, func() (closer io.Closer, e error) {
  21. return nopCloser{
  22. Shedder: NewAdaptiveShedder(g.options...),
  23. }, nil
  24. })
  25. return shedder.(Shedder)
  26. }
  27. type nopCloser struct {
  28. Shedder
  29. }
  30. func (c nopCloser) Close() error {
  31. return nil
  32. }