Documentation ¶
Overview ¶
Package api provides the topmost fundamental APIs for users using sentinel-golang. Users must initialize Sentinel before loading Sentinel rules. Sentinel support three ways to perform initialization:
- api.InitDefault(), using default config to initialize.
- api.InitWithConfig(confEntity *config.Entity), using customized config Entity to initialize.
- api.InitWithConfigFile(configPath string), using yaml file to initialize.
Here is the example code to use Sentinel:
import sentinel "github.com/brucewangzhihua/sentinel-golang/api" err := sentinel.InitDefault() if err != nil { log.Fatal(err) } //Load sentinel rules _, err = flow.LoadRules([]*flow.Rule{ { Resource: "some-test", MetricType: flow.QPS, Count: 10, ControlBehavior: flow.Reject, }, }) if err != nil { log.Fatalf("Unexpected error: %+v", err) return } ch := make(chan struct{}) for i := 0; i < 10; i++ { go func() { for { e, b := sentinel.Entry("some-test", sentinel.WithTrafficType(base.Inbound)) if b != nil { // Blocked. We could get the block reason from the BlockError. time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) } else { // Passed, wrap the logic here. fmt.Println(util.CurrentTimeMillis(), "passed") time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) // Be sure the entry is exited finally. e.Exit() } } }() } <-ch
Index ¶
- func BuildDefaultSlotChain() *base.SlotChain
- func Entry(resource string, opts ...EntryOption) (*base.SentinelEntry, *base.BlockError)
- func GlobalSlotChain() *base.SlotChain
- func InitDefault() error
- func InitWithConfig(confEntity *config.Entity) (err error)
- func InitWithConfigFile(configPath string) error
- func SetSlotChain(chain *base.SlotChain)
- func TraceError(entry *base.SentinelEntry, err error)
- type EntryOption
- func WithAcquireCount(acquireCount uint32) EntryOption
- func WithArgs(args ...interface{}) EntryOption
- func WithAttachment(key interface{}, value interface{}) EntryOption
- func WithAttachments(data map[interface{}]interface{}) EntryOption
- func WithFlag(flag int32) EntryOption
- func WithResourceType(resourceType base.ResourceType) EntryOption
- func WithSlotChain(chain *base.SlotChain) EntryOption
- func WithTrafficType(entryType base.TrafficType) EntryOption
- type EntryOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildDefaultSlotChain ¶
func Entry ¶
func Entry(resource string, opts ...EntryOption) (*base.SentinelEntry, *base.BlockError)
Entry is the basic API of Sentinel.
func GlobalSlotChain ¶
func InitDefault ¶
func InitDefault() error
InitDefault initializes Sentinel using the configuration from system environment and the default value.
func InitWithConfig ¶
InitWithConfig initializes Sentinel using given config.
func InitWithConfigFile ¶
Init loads Sentinel general configuration from the given YAML file and initializes Sentinel.
func SetSlotChain ¶
SetSlotChain replaces current slot chain with the given one. Note that this operation is not thread-safe, so it should be called when pre-initializing Sentinel.
func TraceError ¶
func TraceError(entry *base.SentinelEntry, err error)
TraceError records the provided error to the given SentinelEntry.
Types ¶
type EntryOption ¶
type EntryOption func(*EntryOptions)
func WithAcquireCount ¶
func WithAcquireCount(acquireCount uint32) EntryOption
WithAcquireCount sets the resource entry with the given batch count (by default 1).
func WithArgs ¶
func WithArgs(args ...interface{}) EntryOption
WithArgs sets the resource entry with the given additional parameters.
func WithAttachment ¶
func WithAttachment(key interface{}, value interface{}) EntryOption
WithAttachment set the resource entry with the given k-v pair
func WithAttachments ¶
func WithAttachments(data map[interface{}]interface{}) EntryOption
WithAttachment set the resource entry with the given k-v pairs
func WithFlag ¶
func WithFlag(flag int32) EntryOption
WithFlag sets the resource entry with the given additional flag.
func WithResourceType ¶
func WithResourceType(resourceType base.ResourceType) EntryOption
WithResourceType sets the resource entry with the given resource type.
func WithSlotChain ¶
func WithSlotChain(chain *base.SlotChain) EntryOption
WithSlotChain sets the slot chain.
func WithTrafficType ¶
func WithTrafficType(entryType base.TrafficType) EntryOption
WithTrafficType sets the resource entry with the given traffic type.
type EntryOptions ¶
type EntryOptions struct {
// contains filtered or unexported fields
}
EntryOptions represents the options of a Sentinel resource entry.
func (*EntryOptions) Reset ¶
func (o *EntryOptions) Reset()