forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
127 lines (105 loc) · 2.94 KB
/
registry.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package registry
import (
"context"
"time"
ss "github.com/pydio/cells/common/micro/selector"
"github.com/pydio/cells/common/micro/registry/cluster"
"github.com/pydio/cells/common/micro/registry/service"
"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"
"github.com/micro/go-plugins/registry/memory"
"github.com/pydio/cells/common/micro/client/grpc"
defaults "github.com/pydio/cells/common/micro"
cs "github.com/pydio/cells/common/micro/selector/cache"
rs "github.com/pydio/cells/common/micro/selector/registry"
"github.com/spf13/viper"
)
func EnableService(hostname string, port string) {
r := service.NewRegistry(
service.WithClient(
grpc.NewClient(
client.RequestTimeout(10*time.Minute),
client.Selector(rs.NewSelector(hostname, port)),
client.Retries(20),
client.Retry(func(ctx context.Context, req client.Request, retryCount int, err error) (bool, error) {
if errors.Parse(err.Error()).Detail == "not found" {
return false, err
}
return true, nil
}),
),
),
)
r = NewRegistryWithExpiry(r, 20*time.Minute)
r = NewRegistryWithUnique(r)
r = NewRegistryWithPeers(r)
r = NewRegistryWithProcesses(r)
s := cs.NewSelector(selector.Registry(r))
s = ss.NewSelectorWithMaxRetries(s, 5)
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
}
func EnableMemory() {
// addr := "127.0.0.1:8000"
r := memory.NewRegistry()
r = cluster.NewRegistry(r,
registry.Addrs(viper.GetString("nats_address")),
cluster.ClusterID(viper.GetString("nats_streaming_cluster_id")),
)
r = NewRegistryWithExpiry(r, 20*time.Minute)
r = NewRegistryWithUnique(r)
r = NewRegistryWithPeers(r)
r = NewRegistryWithProcesses(r)
s := selector.NewSelector(selector.Registry(r))
s = ss.NewSelectorWithMaxRetries(s, 5)
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
}
}