api

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2021 License: Apache-2.0 Imports: 19 Imported by: 147

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:

  1. api.InitDefault(), using default config to initialize.
  2. api.InitWithConfig(confEntity *config.Entity), using customized config Entity to initialize.
  3. 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDefaultSlotChain

func BuildDefaultSlotChain() *base.SlotChain

func Entry

func Entry(resource string, opts ...EntryOption) (*base.SentinelEntry, *base.BlockError)

Entry is the basic API of Sentinel.

func GlobalSlotChain

func GlobalSlotChain() *base.SlotChain

func InitDefault

func InitDefault() error

Initialization func initialize the Sentinel's runtime environment, including:

  1. override global config, from manually config or yaml file or env variable
  2. override global logger
  3. 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

func InitWithConfig(confEntity *config.Entity) (err error)

InitWithConfig initializes Sentinel using given config.

func InitWithConfigFile added in v0.5.0

func InitWithConfigFile(configPath string) error

Init loads Sentinel general configuration from the given YAML file and initializes Sentinel.

func InitWithParser added in v1.0.0

func InitWithParser(configBytes []byte, parser func([]byte) (*config.Entity, error)) (err error)

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()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL