forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nats.go
73 lines (62 loc) · 1.51 KB
/
nats.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
73
package registry
import (
"context"
"time"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/selector"
"github.com/micro/go-micro/server"
defaults "github.com/pydio/cells/common/micro"
"github.com/pydio/cells/common/micro/registry/nats"
"github.com/pydio/cells/common/micro/selector/cache"
"github.com/spf13/viper"
)
func EnableNats() {
addr := viper.GetString("registry_address")
r := nats.NewRegistry(
registry.Addrs(addr),
)
q := 1
if defaults.RuntimeIsCluster() {
q = 0
}
defaults.DefaultStartupRegistry = nats.NewRegistry(
registry.Addrs(addr),
registry.Timeout(4*time.Second),
nats.Quorum(q),
)
s := cache.NewSelector(selector.Registry(r))
defaults.InitServer(func() server.Option {
return server.Registry(r)
})
defaults.InitClient(
func() client.Option {
return client.Selector(s)
}, func() client.Option {
return client.Registry(r)
}, func() client.Option {
return client.Retries(5)
}, func() client.Option {
return client.Retry(RetryOnError)
},
)
registry.DefaultRegistry = r
}
// RetryOnError retries a request on a 500 or timeout error
func RetryOnError(ctx context.Context, req client.Request, retryCount int, err error) (bool, error) {
if err == nil {
return false, nil
}
e := errors.Parse(err.Error())
if e == nil {
return false, nil
}
switch e.Code {
// retry on timeout or internal server error
case 408, 500:
return true, nil
default:
return false, nil
}
}