Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ if err := c.TrySet("job", result, nil); err != nil {

`Set` panics when the key is `nil` or not comparable, similar to how an invalid insertion into a regular `map` would fail at runtime.

`Get` returns a deep copy of the stored value, so goroutines can modify what they receive without racing. Do not modify a value after storing it. Limits of the copy:

- unexported struct fields are copied shallowly, including exported fields promoted from an embedded unexported struct (the embedded field itself is unexported);
- pointers to structs with unexported fields are shared as-is, since such types usually own internal state (locks, buffers) that a copy would break;
- map keys, channels, functions and unsafe pointers are shared;
- when the stored value is neither assignable nor convertible to the destination type, it is decoded through a JSON round trip instead.

### `txml`

Normalize first when the payload is escaped or declares a Latin-1 header:
Expand Down Expand Up @@ -77,6 +84,7 @@ Recent changes to consider if the library already has consumers:
- `info.Info.Get` returns `nil` when the map itself is `nil`.
- `info.Info` now provides `Lookup*` helpers for explicit contracts.
- `async.Container` now exposes `TrySet` to avoid panics on invalid keys.
- `async.Container.Get` returns a deep copy of the stored value (v0.2.0); before, slices, maps and pointers were shared between the container and every caller.
- `async.Container.Get` quotes the key in its not-found error (`key ("missing")`), so control characters in untrusted keys are escaped.
- `txml.XMLString.Decode` now decodes directly into the caller-provided destination pointer.

Expand Down
38 changes: 27 additions & 11 deletions async/async_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Container struct {
}

// Set stores a value and optional error for a key.
// The container keeps value as given, so the caller must not modify it after
// storing it.
// It panics if the key is nil or not comparable; prefer TrySet when the key is not trusted.
func (c *Container) Set(key interface{}, value interface{}, err error) {
if err := c.TrySet(key, value, err); err != nil {
Expand All @@ -23,6 +25,8 @@ func (c *Container) Set(key interface{}, value interface{}, err error) {
}

// TrySet stores a value and optional error for a key and returns validation errors.
// The container keeps value as given, so the caller must not modify it after
// storing it.
func (c *Container) TrySet(key interface{}, value interface{}, err error) error {
if err := validateKey(key); err != nil {
return err
Expand All @@ -46,30 +50,42 @@ func (c *Container) TrySet(key interface{}, value interface{}, err error) error
return nil
}

// Get copies the stored value into value and returns any error associated with the key.
// value must be a non-nil pointer.
// Get stores a deep copy of the value stored for key into value and returns
// any error associated with the key. value must be a non-nil pointer.
//
// The copy shares no pointed-to, slice, or map data with the stored value or
// with other callers, so callers may modify what they receive. Limits:
// - unexported struct fields are copied shallowly, so data they reference
// stays shared; this includes exported fields promoted from an embedded
// unexported struct, since the embedded field itself is unexported;
// - pointers to structs with unexported fields are shared as-is, since such
// types usually own internal state (locks, buffers) that a copy would
// break;
// - map keys, channels, functions, and unsafe pointers are shared;
// - when the stored value is neither assignable nor convertible to the
// type value points to, it is decoded through a JSON round trip instead.
func (c *Container) Get(key interface{}, value interface{}) error {
if err := validateKey(key); err != nil {
return err
}

c.mu.RLock()
defer c.mu.RUnlock()

content, ok := c.content[key]
storedErr := c.errors[key]
c.mu.RUnlock()

if !ok {
return notFoundError(key)
}

// The copy runs outside the lock so that a slow copy does not block
// writers; this is safe because the container never mutates a stored
// value, it only replaces map entries.
if err := assignValue(value, content); err != nil {
return err
}

if c.errors != nil && c.errors[key] != nil {
return c.errors[key]
}

return nil
return storedErr
}

// notFoundError quotes the key so that a key from untrusted input cannot
Expand Down Expand Up @@ -111,15 +127,15 @@ func assignValue(dst interface{}, src interface{}) error {

srcValue := reflect.ValueOf(src)
if srcValue.Type().AssignableTo(dstElem.Type()) {
dstElem.Set(srcValue)
dstElem.Set(deepCopy(srcValue))
return nil
}

if srcValue.Type().ConvertibleTo(dstElem.Type()) {
if err := checkSliceToArrayLength(srcValue, dstElem.Type()); err != nil {
return err
}
dstElem.Set(srcValue.Convert(dstElem.Type()))
dstElem.Set(deepCopy(srcValue).Convert(dstElem.Type()))
return nil
}

Expand Down
60 changes: 60 additions & 0 deletions async/async_container_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package async

import (
"strconv"
"testing"
)

type benchFlatStruct struct {
ID int
Name string
Score float64
}

type benchNestedStruct struct {
Name string
Tags []string
Attrs map[string]int
Parent *benchFlatStruct
}

func BenchmarkContainerGet(b *testing.B) {
attrs := make(map[string]int, 100)
for i := 0; i < 100; i++ {
attrs[strconv.Itoa(i)] = i
}

b.Run("int", func(b *testing.B) {
benchmarkGet[int](b, 42)
})
b.Run("flat_struct", func(b *testing.B) {
benchmarkGet[benchFlatStruct](b, benchFlatStruct{ID: 1, Name: "alice", Score: 9.5})
})
b.Run("slice_1k_int", func(b *testing.B) {
benchmarkGet[[]int](b, make([]int, 1000))
})
b.Run("map_100", func(b *testing.B) {
benchmarkGet[map[string]int](b, attrs)
})
b.Run("nested_struct", func(b *testing.B) {
benchmarkGet[benchNestedStruct](b, benchNestedStruct{
Name: "node",
Tags: []string{"a", "b", "c"},
Attrs: map[string]int{"x": 1, "y": 2},
Parent: &benchFlatStruct{ID: 1, Name: "root"},
})
})
}

func benchmarkGet[T any](b *testing.B, stored T) {
container := &Container{}
container.Set("key", stored, nil)
var got T

b.ReportAllocs()
for b.Loop() {
if err := container.Get("key", &got); err != nil {
b.Fatal(err)
}
}
}
Loading
Loading