github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/ratelimiter/example_test.go (about) 1 package ratelimiter_test 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/ydb-platform/ydb-go-sdk/v3" 8 "github.com/ydb-platform/ydb-go-sdk/v3/coordination" 9 "github.com/ydb-platform/ydb-go-sdk/v3/ratelimiter" 10 ) 11 12 func Example() { 13 ctx := context.TODO() 14 db, err := ydb.Open(ctx, "grpc://localhost:2136/local") 15 if err != nil { 16 fmt.Printf("failed to connect: %v", err) 17 18 return 19 } 20 defer db.Close(ctx) // cleanup resources 21 // create node 22 err = db.Coordination().CreateNode(ctx, "/local/ratelimiter_test", coordination.NodeConfig{ 23 Path: "", 24 SelfCheckPeriodMillis: 1000, 25 SessionGracePeriodMillis: 1000, 26 ReadConsistencyMode: coordination.ConsistencyModeRelaxed, 27 AttachConsistencyMode: coordination.ConsistencyModeRelaxed, 28 RatelimiterCountersMode: coordination.RatelimiterCountersModeDetailed, 29 }) 30 if err != nil { 31 fmt.Printf("failed to create node: %v", err) 32 33 return 34 } 35 defer func() { 36 // cleanup node 37 err = db.Coordination().DropNode(ctx, "/local/ratelimiter_test") 38 if err != nil { 39 fmt.Printf("failed to drop node: %v", err) 40 } 41 }() 42 // create resource 43 err = db.Ratelimiter().CreateResource(ctx, "/local/ratelimiter_test", ratelimiter.Resource{ 44 ResourcePath: "test_resource", 45 HierarchicalDrr: ratelimiter.HierarchicalDrrSettings{ 46 MaxUnitsPerSecond: 1, 47 MaxBurstSizeCoefficient: 2, 48 }, 49 }) 50 if err != nil { 51 fmt.Printf("failed to create resource: %v", err) 52 } 53 defer func() { 54 // cleanup resource 55 err = db.Ratelimiter().DropResource(ctx, "/local/ratelimiter_test", "test_resource") 56 if err != nil { 57 fmt.Printf("failed to drop resource: %v", err) 58 } 59 }() 60 // alter resource 61 err = db.Ratelimiter().AlterResource(ctx, "/local/ratelimiter_test", ratelimiter.Resource{ 62 ResourcePath: "test_resource", 63 HierarchicalDrr: ratelimiter.HierarchicalDrrSettings{ 64 MaxUnitsPerSecond: 3, 65 MaxBurstSizeCoefficient: 4, 66 }, 67 }) 68 if err != nil { 69 fmt.Printf("failed to alter resource: %v", err) 70 } 71 // acquire resource amount 1 72 err = db.Ratelimiter().AcquireResource( 73 ctx, 74 "/local/ratelimiter_test", 75 "test_resource", 76 1, 77 ratelimiter.WithAcquire(), 78 ) 79 if err != nil { 80 fmt.Printf("failed to acquire resource: %v", err) 81 } 82 }