Skip to content

Custom key extractor

This direct-API example composes a tenant ID and user ID before calling the limiter.

Prerequisites

  • Go 1.24 or newer
  • no external service

Run

go run ./examples/custom_extractor
examples/custom_extractor/main.go
// Package main demonstrates building a custom application key before calling the limiter.
package main

import (
    "context"
    "fmt"
    "time"

    "github.com/AliRizaAynaci/gorl/v2"
    "github.com/AliRizaAynaci/gorl/v2/core"
)

// main runs a simple demonstration of custom key generation in application code.
func main() {
    limiter, err := gorl.New(core.Config{
        Strategy: core.LeakyBucket,
        Limit:    2,
        Window:   5 * time.Second,
    })
    if err != nil {
        panic(err)
    }
    defer limiter.Close()

    ctx := context.Background()
    tenantUsers := []struct {
        tenant string
        user   string
    }{
        {tenant: "team-a", user: "user-123"},
        {tenant: "team-b", user: "user-456"},
        {tenant: "team-a", user: "user-123"},
        {tenant: "team-a", user: "user-123"},
    }

    for i, item := range tenantUsers {
        key := item.tenant + ":" + item.user
        res, err := limiter.Allow(ctx, key)
        fmt.Printf("Req %d - Key: %s, allowed=%v, remaining=%d, err=%v\n",
            i+1, key, res.Allowed, res.Remaining, err)
    }
}

Expected behavior

team-a:user-123 and team-b:user-456 consume independent leaky-bucket state. The third request for the first composite key is denied after that key has filled its capacity.

Production cautions

Build keys from authenticated, immutable identifiers and define an unambiguous encoding. Avoid logging secrets or raw access tokens. For arbitrary components, prefer a length-prefix or another collision-safe encoding over ambiguous string concatenation.