ratelimiter

package
v3.38.1 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Example
ctx := context.TODO()
db, err := ydb.Open(ctx, "grpcs://localhost:2135/local")
if err != nil {
	fmt.Printf("failed to connect: %v", err)
	return
}
defer db.Close(ctx) // cleanup resources
// create node
err = db.Coordination().CreateNode(ctx, "/local/ratelimiter_test", coordination.NodeConfig{
	Path:                     "",
	SelfCheckPeriodMillis:    1000,
	SessionGracePeriodMillis: 1000,
	ReadConsistencyMode:      coordination.ConsistencyModeRelaxed,
	AttachConsistencyMode:    coordination.ConsistencyModeRelaxed,
	RatelimiterCountersMode:  coordination.RatelimiterCountersModeDetailed,
})
if err != nil {
	fmt.Printf("failed to create node: %v", err)
	return
}
defer func() {
	// cleanup node
	err = db.Coordination().DropNode(ctx, "/local/ratelimiter_test")
	if err != nil {
		fmt.Printf("failed to drop node: %v", err)
	}
}()
// create resource
err = db.Ratelimiter().CreateResource(ctx, "/local/ratelimiter_test", ratelimiter.Resource{
	ResourcePath: "test_resource",
	HierarchicalDrr: ratelimiter.HierarchicalDrrSettings{
		MaxUnitsPerSecond:       1,
		MaxBurstSizeCoefficient: 2,
	},
})
if err != nil {
	fmt.Printf("failed to create resource: %v", err)
}
defer func() {
	// cleanup resource
	err = db.Ratelimiter().DropResource(ctx, "/local/ratelimiter_test", "test_resource")
	if err != nil {
		fmt.Printf("failed to drop resource: %v", err)
	}
}()
// alter resource
err = db.Ratelimiter().AlterResource(ctx, "/local/ratelimiter_test", ratelimiter.Resource{
	ResourcePath: "test_resource",
	HierarchicalDrr: ratelimiter.HierarchicalDrrSettings{
		MaxUnitsPerSecond:       3,
		MaxBurstSizeCoefficient: 4,
	},
})
if err != nil {
	fmt.Printf("failed to alter resource: %v", err)
}
// acquire resource amount 1
err = db.Ratelimiter().AcquireResource(
	ctx,
	"/local/ratelimiter_test",
	"test_resource",
	1,
	ratelimiter.WithAcquire(),
)
if err != nil {
	fmt.Printf("failed to acquire resource: %v", err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithAcquire added in v3.11.0

func WithAcquire() options.AcquireOption

func WithOperationCancelAfter added in v3.11.0

func WithOperationCancelAfter(operationCancelAfter time.Duration) options.AcquireOption

func WithOperationTimeout added in v3.11.0

func WithOperationTimeout(operationTimeout time.Duration) options.AcquireOption

func WithReport added in v3.11.0

func WithReport() options.AcquireOption

Types

type AcquireError added in v3.11.0

type AcquireError interface {
	error

	Amount() uint64
	Unwrap() error
}

type Client added in v3.7.0

type Client interface {
	CreateResource(
		ctx context.Context,
		coordinationNodePath string,
		resource Resource,
	) (err error)
	AlterResource(
		ctx context.Context,
		coordinationNodePath string,
		resource Resource,
	) (err error)
	DropResource(
		ctx context.Context,
		coordinationNodePath string,
		resourcePath string,
	) (err error)
	ListResource(
		ctx context.Context,
		coordinationNodePath string,
		resourcePath string,
		recursive bool,
	) (_ []string, err error)
	DescribeResource(
		ctx context.Context,
		coordinationNodePath string,
		resourcePath string,
	) (_ *Resource, err error)
	AcquireResource(
		ctx context.Context,
		coordinationNodePath string,
		resourcePath string,
		amount uint64,
		opts ...options.AcquireOption,
	) (err error)
}

type HierarchicalDrrSettings

type HierarchicalDrrSettings struct {
	MaxUnitsPerSecond       float64
	MaxBurstSizeCoefficient float64
	PrefetchCoefficient     float64
	PrefetchWatermark       float64
}

type Resource

type Resource struct {
	ResourcePath    string
	HierarchicalDrr HierarchicalDrrSettings
}

Jump to

Keyboard shortcuts

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