You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/load/sheddergroup.go

40 lines
789 B
Go

4 years ago
package load
import (
"io"
"github.com/tal-tech/go-zero/core/syncx"
4 years ago
)
// A ShedderGroup is a manager to manage key based shedders.
4 years ago
type ShedderGroup struct {
options []ShedderOption
manager *syncx.ResourceManager
}
// NewShedderGroup returns a ShedderGroup.
4 years ago
func NewShedderGroup(opts ...ShedderOption) *ShedderGroup {
return &ShedderGroup{
options: opts,
manager: syncx.NewResourceManager(),
}
}
// GetShedder gets the Shedder for the given key.
4 years ago
func (g *ShedderGroup) GetShedder(key string) Shedder {
shedder, _ := g.manager.GetResource(key, func() (closer io.Closer, e error) {
return nopCloser{
Shedder: NewAdaptiveShedder(g.options...),
}, nil
})
return shedder.(Shedder)
}
type nopCloser struct {
Shedder
}
func (c nopCloser) Close() error {
return nil
}