Support endpoint metadata

This commit is contained in:
Asim 2016-05-26 18:01:02 +01:00
parent 1254a87286
commit 8245390b5a
2 changed files with 18 additions and 1 deletions

View File

@ -34,6 +34,7 @@ type Subscriber interface {
type HandlerOptions struct {
Internal bool
Metadata map[string]map[string]string
}
type SubscriberOptions struct {
@ -50,6 +51,14 @@ func InternalHandler(b bool) HandlerOption {
}
}
// EndpointMetadata is a Handler option that allows metadata to be added to
// individual endpoints.
func EndpointMetadata(endpoint string, metadata map[string]string) HandlerOption {
return func(o *HandlerOptions) {
o.Metadata[endpoint] = metadata
}
}
// Internal Subscriber options specifies that a subscriber is not advertised
// to the discovery system.
func InternalSubscriber(b bool) SubscriberOption {

View File

@ -14,7 +14,10 @@ type rpcHandler struct {
}
func newRpcHandler(handler interface{}, opts ...HandlerOption) Handler {
var options HandlerOptions
options := HandlerOptions{
Metadata: make(map[string]map[string]string),
}
for _, o := range opts {
o(&options)
}
@ -28,6 +31,11 @@ func newRpcHandler(handler interface{}, opts ...HandlerOption) Handler {
for m := 0; m < typ.NumMethod(); m++ {
if e := extractEndpoint(typ.Method(m)); e != nil {
e.Name = name + "." + e.Name
for k, v := range options.Metadata[e.Name] {
e.Metadata[k] = v
}
endpoints = append(endpoints, e)
}
}