-
Notifications
You must be signed in to change notification settings - Fork 1k
/
context.go
34 lines (27 loc) · 950 Bytes
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Package context provides a context for accessing services
package context
import (
"context"
"github.com/micro/micro/v3/service/context/metadata"
"github.com/micro/micro/v3/util/namespace"
)
var (
// DefaultContext is a context which can be used to access micro services
DefaultContext = WithNamespace("micro")
)
// WithNamespace creates a new context with the given namespace
func WithNamespace(ns string) context.Context {
return SetNamespace(context.TODO(), ns)
}
// SetNamespace sets the namespace for a context
func SetNamespace(ctx context.Context, ns string) context.Context {
return namespace.ContextWithNamespace(ctx, ns)
}
// SetMetadata sets the metadata within the context
func SetMetadata(ctx context.Context, k, v string) context.Context {
return metadata.Set(ctx, k, v)
}
// GetMetadata returns metadata from the context
func GetMetadata(ctx context.Context, k string) (string, bool) {
return metadata.Get(ctx, k)
}