sheddergroup.go 635 B

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