api

package
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

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/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

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

InitDefault initializes Sentinel using the configuration from system environment and the default value.

func InitWithConfig

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

InitWithConfig initializes Sentinel using given config.

func InitWithConfigFile

func InitWithConfigFile(configPath string) error

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

func SetSlotChain

func SetSlotChain(chain *base.SlotChain)

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

Jump to

Keyboard shortcuts

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