This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
/
egress_reverse_proxy.go
160 lines (134 loc) · 3.73 KB
/
egress_reverse_proxy.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package routing
import (
"context"
"errors"
"log"
"math/rand"
"sync/atomic"
"time"
"unsafe"
rpc "code.cloudfoundry.org/log-cache/pkg/rpc/logcache_v1"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
// EgressReverseProxy is a reverse proxy for Egress requests.
type EgressReverseProxy struct {
clients []rpc.EgressClient
l Lookup
localIdx int
log *log.Logger
remoteMetaCache unsafe.Pointer
localMetaCache unsafe.Pointer
metaCacheDuration time.Duration
}
// NewEgressReverseProxy returns a new EgressReverseProxy. LocalIdx is
// required to know where to find the local node for meta lookups.
func NewEgressReverseProxy(
l Lookup,
clients []rpc.EgressClient,
localIdx int,
log *log.Logger,
opts ...EgressReverseProxyOption,
) *EgressReverseProxy {
e := &EgressReverseProxy{
l: l,
clients: clients,
localIdx: localIdx,
log: log,
metaCacheDuration: time.Second,
}
for _, o := range opts {
o(e)
}
return e
}
// Read will either read from the local node or remote nodes.
func (e *EgressReverseProxy) Read(ctx context.Context, in *rpc.ReadRequest) (*rpc.ReadResponse, error) {
idx := e.l(in.GetSourceId())
if len(idx) == 0 {
return nil, grpc.Errorf(codes.Unavailable, "failed to find route for request. please try again")
}
for _, i := range idx {
if i == e.localIdx {
return e.clients[e.localIdx].Read(ctx, in)
}
}
return e.clients[idx[rand.Intn(len(idx))]].Read(ctx, in)
}
// Meta will gather meta from the local store and remote nodes.
func (e *EgressReverseProxy) Meta(ctx context.Context, in *rpc.MetaRequest) (*rpc.MetaResponse, error) {
if in.LocalOnly {
return e.localMeta(ctx, in)
}
return e.remoteMeta(ctx, in)
}
func (e *EgressReverseProxy) localMeta(ctx context.Context, in *rpc.MetaRequest) (*rpc.MetaResponse, error) {
cache := (*metaCache)(atomic.LoadPointer(&e.localMetaCache))
if !cache.expired() {
return cache.metaResp, nil
}
metaInfo, err := e.clients[e.localIdx].Meta(ctx, in)
if err != nil {
return nil, err
}
atomic.StorePointer(&e.localMetaCache, unsafe.Pointer(&metaCache{
duration: e.metaCacheDuration,
timestamp: time.Now(),
metaResp: metaInfo,
}))
return metaInfo, nil
}
func (e *EgressReverseProxy) remoteMeta(ctx context.Context, in *rpc.MetaRequest) (*rpc.MetaResponse, error) {
cache := (*metaCache)(atomic.LoadPointer(&e.remoteMetaCache))
if !cache.expired() {
return cache.metaResp, nil
}
// Each remote should only fetch their local meta data.
req := &rpc.MetaRequest{
LocalOnly: true,
}
result := &rpc.MetaResponse{
Meta: make(map[string]*rpc.MetaInfo),
}
var errs []error
for _, c := range e.clients {
resp, err := c.Meta(ctx, req)
if err != nil {
// TODO: Metric
e.log.Printf("failed to read meta data from remote node: %s", err)
errs = append(errs, err)
continue
}
for sourceID, mi := range resp.Meta {
result.Meta[sourceID] = mi
}
}
if len(errs) == len(e.clients) {
return nil, errors.New("failed to read meta data from remote node")
}
atomic.StorePointer(&e.remoteMetaCache, unsafe.Pointer(&metaCache{
duration: e.metaCacheDuration,
timestamp: time.Now(),
metaResp: result,
}))
return result, nil
}
type EgressReverseProxyOption func(e *EgressReverseProxy)
// WithMetaCacheDuration is a EgressReverseProxyOption to configure how long
// to cache results from the Meta endpoint.
func WithMetaCacheDuration(d time.Duration) EgressReverseProxyOption {
return func(e *EgressReverseProxy) {
e.metaCacheDuration = d
}
}
type metaCache struct {
duration time.Duration
timestamp time.Time
metaResp *rpc.MetaResponse
}
func (c *metaCache) expired() bool {
if c == nil {
return true
}
return time.Now().After(c.timestamp.Add(c.duration))
}