-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
catalog_datacenters.go
79 lines (65 loc) · 1.86 KB
/
catalog_datacenters.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
package cachetype
import (
"context"
"fmt"
"github.com/hashicorp/consul/agent/cache"
"github.com/hashicorp/consul/agent/structs"
)
// Recommended name for registration.
const CatalogDatacentersName = "catalog-datacenters"
// Datacenters supports fetching discovering all the known datacenters
type CatalogDatacenters struct {
RegisterOptionsNoRefresh
RPC RPC
}
func (c *CatalogDatacenters) Fetch(opts cache.FetchOptions, req cache.Request) (cache.FetchResult, error) {
var result cache.FetchResult
// The request should be a CatalogDatacentersRequest.
reqReal, ok := req.(*structs.DatacentersRequest)
if !ok {
return result, fmt.Errorf(
"Internal cache failure: request wrong type: %T", req)
}
// Lightweight copy this object so that manipulating QueryOptions doesn't race.
dup := *reqReal
reqReal = &dup
// Always allow stale - there's no point in hitting leader if the request is
// going to be served from cache and endup arbitrarily stale anyway. This
// allows cached service-discover to automatically read scale across all
// servers too.
reqReal.AllowStale = true
// Fetch
var reply []string
if err := c.RPC.RPC(context.Background(), "Catalog.ListDatacenters", reqReal, &reply); err != nil {
return result, err
}
result.Value = &reply
// this is a purely synthetic index to keep the caching happy.
if opts.LastResult != nil {
equal := true
previousDCs, ok := opts.LastResult.Value.(*[]string)
if ok && previousDCs == nil {
ok = false
}
if ok {
if len(reply) != len(*previousDCs) {
equal = false
} else {
// ordering matters as they should be sorted based on distance
for i, dc := range reply {
if dc != (*previousDCs)[i] {
equal = false
break
}
}
}
}
result.Index = opts.LastResult.Index
if !equal || !ok {
result.Index += 1
}
} else {
result.Index = 1
}
return result, nil
}