Skip to content

Commit 18e35b8

Browse files
committed
pkg/settings/limits: fix MultiResourcePoolLimiter; normalize contexts.CRE.Owner
1 parent fbb00f8 commit 18e35b8

11 files changed

+51
-24
lines changed

pkg/contexts/contexts.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package contexts
22

33
import (
44
"context"
5+
"strings"
56
)
67

78
// Value gets a value from the context and casts it to T.
@@ -15,17 +16,15 @@ func Value[T any](ctx context.Context, key any) T {
1516
}
1617

1718
// WithCRE returns a derived context with a cre key/val.
19+
// The values will be normalized via CRE.Normalized.
1820
func WithCRE(ctx context.Context, cre CRE) context.Context {
19-
return context.WithValue(ctx, creCtxKey, &cre)
21+
return context.WithValue(ctx, creCtxKey, cre.Normalized())
2022
}
2123

2224
// CREValue returns the [CRE] key/val, which may be empty.
25+
// If it is not empty, the values will be normalized.
2326
func CREValue(ctx context.Context) CRE {
24-
v := Value[*CRE](ctx, creCtxKey)
25-
if v == nil {
26-
return CRE{}
27-
}
28-
return *v // copy
27+
return Value[CRE](ctx, creCtxKey)
2928
}
3029

3130
// CRE holds contextual Chainlink Runtime Environment metadata.
@@ -37,6 +36,13 @@ type CRE struct {
3736
Owner, Workflow string
3837
}
3938

39+
// Normalized returns a possibly modified CRE with normalized values.
40+
func (c CRE) Normalized() CRE {
41+
c.Owner = strings.TrimPrefix(c.Owner, "0x")
42+
c.Owner = strings.ToLower(c.Owner)
43+
return c
44+
}
45+
4046
func (c CRE) LoggerKVs() []any {
4147
return []any{
4248
"org", c.Org,

pkg/settings/json_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func Test_jsonSettings_GetScoped(t *testing.T) {
3838

3939
ctx := contexts.WithCRE(t.Context(), contexts.CRE{
4040
Org: "123",
41-
Owner: "0x8bd112d3f8f92e41c861939545ad387307af9703",
41+
Owner: "8bd112d3f8f92e41c861939545ad387307af9703",
4242
Workflow: "15c631d295ef5e32deb99a10ee6804bc4af1385568f9b3363f6552ac6dbb2cef",
4343
})
4444
gotValue, err := r.GetScoped(ctx, ScopeGlobal, `Foo`)

pkg/settings/keys_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func TestTenant_rawKeys(t *testing.T) {
1212
const (
1313
org = "AcmeCorporation"
14-
owner = "0x1234abcd"
14+
owner = "1234abcd"
1515
workflow = "ABCDEFGH"
1616
key = "foo"
1717
)

pkg/settings/limits/resource.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,20 @@ func (u *unscopedResourcePoolLimiter[N]) Free(_ context.Context, amount N) error
296296
return nil
297297
}
298298

299+
var _ ResourcePoolLimiter[int] = MultiResourcePoolLimiter[int]{}
300+
299301
// MultiResourcePoolLimiter is a ResourcePoolLimiter backed by other limiters, which are each called in order.
300302
type MultiResourcePoolLimiter[N Number] []ResourcePoolLimiter[N]
301303

304+
func (m MultiResourcePoolLimiter[N]) Close() (errs error) {
305+
for _, l := range m {
306+
if err := l.Close(); err != nil {
307+
errs = errors.Join(errs, err)
308+
}
309+
}
310+
return
311+
}
312+
302313
func (m MultiResourcePoolLimiter[N]) Wait(ctx context.Context, amount N) (func(), error) {
303314
var frees freeFns
304315
for _, l := range m {
@@ -312,17 +323,26 @@ func (m MultiResourcePoolLimiter[N]) Wait(ctx context.Context, amount N) (func()
312323
return frees.freeAll, nil
313324
}
314325

315-
func (m MultiResourcePoolLimiter[N]) Use(ctx context.Context, amount N) (func(), error) {
326+
func (m MultiResourcePoolLimiter[N]) Use(ctx context.Context, amount N) error {
316327
var frees freeFns
317328
for _, l := range m {
318329
err := l.Use(ctx, amount)
319330
if err != nil {
320331
frees.freeAll()
321-
return nil, err
332+
return err
322333
}
323334
frees = append(frees, func() { l.Free(ctx, amount) })
324335
}
325-
return frees.freeAll, nil
336+
return nil
337+
}
338+
339+
func (m MultiResourcePoolLimiter[N]) Free(ctx context.Context, amount N) (errs error) {
340+
for _, l := range m {
341+
if err := l.Free(ctx, amount); err != nil {
342+
errs = errors.Join(errs, err)
343+
}
344+
}
345+
return
326346
}
327347

328348
type freeFns []func()

pkg/settings/limits/resource_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ func ExampleResourceLimiter_Use() {
7070
}
7171

7272
func ExampleMultiResourcePoolLimiter() {
73-
ctx := contexts.WithCRE(context.Background(), contexts.CRE{Org: "orgID", Owner: "ownerID", Workflow: "workflowID"})
73+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
74+
defer cancel()
75+
ctx = contexts.WithCRE(ctx, contexts.CRE{Org: "orgID", Owner: "owner-id", Workflow: "workflowID"})
7476
global := GlobalResourcePoolLimiter[int](100)
7577
freeGlobal, err := global.Wait(ctx, 95)
7678
if err != nil {
@@ -93,12 +95,11 @@ func ExampleMultiResourcePoolLimiter() {
9395
}
9496
multi := MultiResourcePoolLimiter[int]{global, org, user, workflow}
9597
tryWork := func() error {
96-
free, err := multi.Use(ctx, 10)
98+
err := multi.Use(ctx, 10)
9799
if err != nil {
98100
return err
99101
}
100-
defer free()
101-
return nil
102+
return multi.Free(ctx, 10)
102103
}
103104

104105
fmt.Println(tryWork())
@@ -110,15 +111,15 @@ func ExampleMultiResourcePoolLimiter() {
110111
fmt.Println(tryWork())
111112
freeWorkflow()
112113
fmt.Println(tryWork())
113-
done, err := multi.Wait(ctx, 10)
114+
free, err := multi.Wait(ctx, 10)
114115
if err != nil {
115116
log.Fatal(err)
116117
}
117-
done()
118+
free()
118119
// Output:
119120
// resource limited: cannot use 10, already using 95/100
120121
// resource limited for org[orgID]: cannot use 10, already using 45/50
121-
// resource limited for owner[ownerID]: cannot use 10, already using 15/20
122+
// resource limited for owner[owner-id]: cannot use 10, already using 15/20
122123
// resource limited for workflow[workflowID]: cannot use 10, already using 5/10
123124
// <nil>
124125
}

pkg/settings/testdata/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
}
2121
},
2222
"owner": {
23-
"0x00026b4aa7e57ca7b68ae1bf45653f56b656fd3a": {
23+
"00026b4aa7e57ca7b68ae1bf45653f56b656fd3a": {
2424
"Bar": {
2525
"Baz": "200"
2626
},
2727
"Foo": "75"
2828
},
29-
"0x8bd112d3f8f92e41c861939545ad387307af9703": {
29+
"8bd112d3f8f92e41c861939545ad387307af9703": {
3030
"Bar": {
3131
"Baz": "43"
3232
},

pkg/settings/testdata/config.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ Baz = "500"
2121

2222
[owner]
2323

24-
[owner.0x00026b4aa7e57ca7b68ae1bf45653f56b656fd3a]
24+
[owner.00026b4aa7e57ca7b68ae1bf45653f56b656fd3a]
2525
Foo = "75"
2626

27-
[owner.0x00026b4aa7e57ca7b68ae1bf45653f56b656fd3a.Bar]
27+
[owner.00026b4aa7e57ca7b68ae1bf45653f56b656fd3a.Bar]
2828
Baz = "200"
2929

30-
[owner.0x8bd112d3f8f92e41c861939545ad387307af9703]
30+
[owner.8bd112d3f8f92e41c861939545ad387307af9703]
3131
Foo = "13"
3232

33-
[owner.0x8bd112d3f8f92e41c861939545ad387307af9703.Bar]
33+
[owner.8bd112d3f8f92e41c861939545ad387307af9703.Bar]
3434
Baz = "43"
3535

3636
[workflow]

pkg/settings/testdata/json/owner/0x00026b4aa7e57ca7b68ae1bf45653f56b656fd3a.json renamed to pkg/settings/testdata/json/owner/00026b4aa7e57ca7b68ae1bf45653f56b656fd3a.json

File renamed without changes.

pkg/settings/testdata/json/owner/0x8bd112d3f8f92e41c861939545ad387307af9703.json renamed to pkg/settings/testdata/json/owner/8bd112d3f8f92e41c861939545ad387307af9703.json

File renamed without changes.

pkg/settings/testdata/toml/owner/0x00026b4aa7e57ca7b68ae1bf45653f56b656fd3a.toml renamed to pkg/settings/testdata/toml/owner/00026b4aa7e57ca7b68ae1bf45653f56b656fd3a.toml

File renamed without changes.

0 commit comments

Comments
 (0)