forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
72 lines (62 loc) · 1.76 KB
/
options.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package nats
import (
"context"
"github.com/micro/go-micro/registry"
"github.com/nats-io/nats.go"
)
type contextQuorumKey struct{}
type optionsKey struct{}
type watchTopicKey struct{}
type queryTopicKey struct{}
var (
DefaultQuorum = 0
)
func getQuorum(o registry.Options) int {
if o.Context == nil {
return DefaultQuorum
}
value := o.Context.Value(contextQuorumKey{})
if v, ok := value.(int); ok {
return v
} else {
return DefaultQuorum
}
}
func Quorum(n int) registry.Option {
return func(o *registry.Options) {
o.Context = context.WithValue(o.Context, contextQuorumKey{}, n)
}
}
// Options allow to inject a nats.Options struct for configuring
// the nats connection
func Options(nopts nats.Options) registry.Option {
return func(o *registry.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, optionsKey{}, nopts)
}
}
// QueryTopic allows to set a custom nats topic on which service registries
// query (survey) other services. All registries listen on this topic and
// then respond to the query message.
func QueryTopic(s string) registry.Option {
return func(o *registry.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, queryTopicKey{}, s)
}
}
// WatchTopic allows to set a custom nats topic on which registries broadcast
// changes (e.g. when services are added, updated or removed). Since we don't
// have a central registry service, each service typically broadcasts in a
// determined frequency on this topic.
func WatchTopic(s string) registry.Option {
return func(o *registry.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, watchTopicKey{}, s)
}
}