Skip to content

Commit

Permalink
chore: Improve performance of TenantID
Browse files Browse the repository at this point in the history
  • Loading branch information
cyriltovena committed Jul 3, 2024
1 parent 73feada commit 79a5c45
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tenant/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ import (
//
//nolint:revive
func TenantID(ctx context.Context) (string, error) {
//lint:ignore faillint wrapper around upstream method
orgID, err := user.ExtractOrgID(ctx)
if err != nil {
return "", err
}
if !strings.Contains(orgID, tenantIDsSeparator) {
if err := ValidTenantID(orgID); err != nil {
return "", err
}
return orgID, nil
}
orgIDs, err := TenantIDs(ctx)
if err != nil {
return "", err
Expand Down
22 changes: 22 additions & 0 deletions tenant/tenant_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package tenant

import (
"context"
"strings"
"testing"

"github.com/grafana/dskit/user"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -48,3 +50,23 @@ func TestValidTenantIDs(t *testing.T) {
})
}
}

func BenchmarkTenantID(b *testing.B) {
singleCtx := context.Background()
singleCtx = user.InjectOrgID(singleCtx, "tenant-a")
multiCtx := context.Background()
multiCtx = user.InjectOrgID(multiCtx, "tenant-a|tenant-b|tenant-c")

b.ResetTimer()
b.ReportAllocs()
b.Run("single", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = TenantID(singleCtx)
}
})
b.Run("multi", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = TenantID(multiCtx)
}
})
}

0 comments on commit 79a5c45

Please sign in to comment.