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.
|
// from Transport headers.
|
||||||
type Metadata map[string]string
|
type Metadata map[string]string
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultMetadataSize = 6
|
||||||
|
)
|
||||||
|
|
||||||
func (md Metadata) Get(key string) (string, bool) {
|
func (md Metadata) Get(key string) (string, bool) {
|
||||||
// fast path
|
// fast path
|
||||||
val, ok := md[key]
|
val, ok := md[key]
|
||||||
@ -39,6 +43,9 @@ func (md Metadata) Del(key string) {
|
|||||||
|
|
||||||
// Copy makes a copy of the metadata
|
// Copy makes a copy of the metadata
|
||||||
func Copy(md Metadata) Metadata {
|
func Copy(md Metadata) Metadata {
|
||||||
|
if len(md) == 0 {
|
||||||
|
return make(Metadata, DefaultMetadataSize)
|
||||||
|
}
|
||||||
nmd := make(Metadata, len(md))
|
nmd := make(Metadata, len(md))
|
||||||
for key, val := range md {
|
for key, val := range md {
|
||||||
nmd.Set(key, val)
|
nmd.Set(key, val)
|
||||||
@ -49,7 +56,7 @@ func Copy(md Metadata) Metadata {
|
|||||||
func Del(ctx context.Context, key string) context.Context {
|
func Del(ctx context.Context, key string) context.Context {
|
||||||
md, ok := FromContext(ctx)
|
md, ok := FromContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
md = make(Metadata)
|
md = make(Metadata, DefaultMetadataSize)
|
||||||
}
|
}
|
||||||
md.Del(key)
|
md.Del(key)
|
||||||
return context.WithValue(ctx, metadataKey{}, md)
|
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 {
|
func Set(ctx context.Context, key, val string) context.Context {
|
||||||
md, ok := FromContext(ctx)
|
md, ok := FromContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
md = make(Metadata)
|
md = make(Metadata, DefaultMetadataSize)
|
||||||
}
|
}
|
||||||
md.Set(key, val)
|
md.Set(key, val)
|
||||||
return context.WithValue(ctx, metadataKey{}, md)
|
return context.WithValue(ctx, metadataKey{}, md)
|
||||||
@ -84,9 +91,17 @@ func FromContext(ctx context.Context) (Metadata, bool) {
|
|||||||
return nmd, ok
|
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
|
// NewContext creates a new context with the given metadata
|
||||||
func NewContext(ctx context.Context, md Metadata) context.Context {
|
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
|
// 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)
|
md, ok := FromContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
md = make(Metadata, len(pmd))
|
return context.WithValue(ctx, metadataKey{}, Copy(pmd))
|
||||||
}
|
}
|
||||||
nmd := Copy(md)
|
nmd := Copy(md)
|
||||||
for key, val := range pmd {
|
for key, val := range pmd {
|
||||||
|
@ -6,6 +6,30 @@ import (
|
|||||||
"testing"
|
"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) {
|
func TestMetadataSet(t *testing.T) {
|
||||||
ctx := Set(context.TODO(), "Key", "val")
|
ctx := Set(context.TODO(), "Key", "val")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user