fix concurrent map usage

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-10-01 16:00:01 +03:00
parent 8532ccebba
commit 9475003059

View File

@ -40,8 +40,8 @@ func (md Metadata) Del(key string) {
// Copy makes a copy of the metadata
func Copy(md Metadata) Metadata {
nmd := make(Metadata, len(md))
for k, v := range md {
nmd[k] = v
for key, val := range md {
nmd.Set(key, val)
}
return nmd
}
@ -77,7 +77,11 @@ func Get(ctx context.Context, key string) (string, bool) {
// FromContext returns metadata from the given context
func FromContext(ctx context.Context) (Metadata, bool) {
md, ok := ctx.Value(metadataKey{}).(Metadata)
return md, ok
if !ok {
return nil, ok
}
nmd := Copy(md)
return nmd, ok
}
// NewContext creates a new context with the given metadata
@ -92,12 +96,9 @@ func MergeContext(ctx context.Context, pmd Metadata, overwrite bool) context.Con
}
md, ok := FromContext(ctx)
if !ok {
md = make(Metadata)
}
nmd := make(Metadata, len(md))
for key, val := range md {
nmd.Set(key, val)
md = make(Metadata, len(pmd))
}
nmd := Copy(md)
for key, val := range pmd {
if _, ok := nmd[key]; ok && !overwrite {
// skip