metadata: small optimization and export default metadata size
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
62502ad720
commit
0a5b34a07b
@ -13,6 +13,10 @@ type metadataKey struct{}
|
||||
// from Transport headers.
|
||||
type Metadata map[string]string
|
||||
|
||||
var (
|
||||
DefaultMetadataSize = 6
|
||||
)
|
||||
|
||||
func (md Metadata) Get(key string) (string, bool) {
|
||||
// fast path
|
||||
val, ok := md[key]
|
||||
@ -39,6 +43,9 @@ func (md Metadata) Del(key string) {
|
||||
|
||||
// Copy makes a copy of the metadata
|
||||
func Copy(md Metadata) Metadata {
|
||||
if len(md) == 0 {
|
||||
return make(Metadata, DefaultMetadataSize)
|
||||
}
|
||||
nmd := make(Metadata, len(md))
|
||||
for key, val := range md {
|
||||
nmd.Set(key, val)
|
||||
@ -49,7 +56,7 @@ func Copy(md Metadata) Metadata {
|
||||
func Del(ctx context.Context, key string) context.Context {
|
||||
md, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
md = make(Metadata)
|
||||
md = make(Metadata, DefaultMetadataSize)
|
||||
}
|
||||
md.Del(key)
|
||||
return context.WithValue(ctx, metadataKey{}, md)
|
||||
@ -59,7 +66,7 @@ func Del(ctx context.Context, key string) context.Context {
|
||||
func Set(ctx context.Context, key, val string) context.Context {
|
||||
md, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
md = make(Metadata)
|
||||
md = make(Metadata, DefaultMetadataSize)
|
||||
}
|
||||
md.Set(key, val)
|
||||
return context.WithValue(ctx, metadataKey{}, md)
|
||||
@ -84,9 +91,17 @@ func FromContext(ctx context.Context) (Metadata, bool) {
|
||||
return nmd, ok
|
||||
}
|
||||
|
||||
// New return new sized metadata
|
||||
func New(size int) Metadata {
|
||||
if size == 0 {
|
||||
size = DefaultMetadataSize
|
||||
}
|
||||
return make(Metadata, size)
|
||||
}
|
||||
|
||||
// NewContext creates a new context with the given metadata
|
||||
func NewContext(ctx context.Context, md Metadata) context.Context {
|
||||
return context.WithValue(ctx, metadataKey{}, md)
|
||||
return context.WithValue(ctx, metadataKey{}, Copy(md))
|
||||
}
|
||||
|
||||
// MergeContext merges metadata to existing metadata, overwriting if specified
|
||||
@ -96,7 +111,7 @@ func MergeContext(ctx context.Context, pmd Metadata, overwrite bool) context.Con
|
||||
}
|
||||
md, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
md = make(Metadata, len(pmd))
|
||||
return context.WithValue(ctx, metadataKey{}, Copy(pmd))
|
||||
}
|
||||
nmd := Copy(md)
|
||||
for key, val := range pmd {
|
||||
|
@ -6,6 +6,30 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMedataCanonicalKey(t *testing.T) {
|
||||
ctx := Set(context.TODO(), "x-request-id", "12345")
|
||||
v, ok := Get(ctx, "x-request-id")
|
||||
if !ok {
|
||||
t.Fatalf("failed to get x-request-id")
|
||||
} else if v != "12345" {
|
||||
t.Fatalf("invalid metadata value: %s != %s", "12345", v)
|
||||
}
|
||||
|
||||
v, ok = Get(ctx, "X-Request-Id")
|
||||
if !ok {
|
||||
t.Fatalf("failed to get x-request-id")
|
||||
} else if v != "12345" {
|
||||
t.Fatalf("invalid metadata value: %s != %s", "12345", v)
|
||||
}
|
||||
v, ok = Get(ctx, "X-Request-ID")
|
||||
if !ok {
|
||||
t.Fatalf("failed to get x-request-id")
|
||||
} else if v != "12345" {
|
||||
t.Fatalf("invalid metadata value: %s != %s", "12345", v)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMetadataSet(t *testing.T) {
|
||||
ctx := Set(context.TODO(), "Key", "val")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user