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/alibaba/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, Threshold: 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 InitWithParser(configBytes []byte, parser func([]byte) (*config.Entity, error)) (err error)
- 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 WithBatchCount(batchCount uint32) 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
Initialization func initialize the Sentinel's runtime environment, including:
- override global config, from manually config or yaml file or env variable
- override global logger
- initiate core component async task, including: metric log, system statistic...
InitDefault initializes Sentinel using the configuration from system environment and the default value.
func InitWithConfig ¶ added in v0.5.0
InitWithConfig initializes Sentinel using given config.
func InitWithConfigFile ¶ added in v0.5.0
Init loads Sentinel general configuration from the given YAML file and initializes Sentinel.
func InitWithParser ¶ added in v1.0.0
InitWithParser initializes Sentinel using given config parser parser deserializes the configBytes and return config.Entity
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
DEPRECATED: use WithBatchCount instead. 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 ¶ added in v0.3.0
func WithAttachment(key interface{}, value interface{}) EntryOption
WithAttachment set the resource entry with the given k-v pair
func WithAttachments ¶ added in v0.3.0
func WithAttachments(data map[interface{}]interface{}) EntryOption
WithAttachment set the resource entry with the given k-v pairs
func WithBatchCount ¶ added in v1.0.0
func WithBatchCount(batchCount uint32) EntryOption
WithBatchCount sets the resource entry with the given batch count (by default 1).
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 ¶ added in v0.4.0
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 ¶ added in v0.4.0
func (o *EntryOptions) Reset()