This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
retry.go
174 lines (151 loc) · 5 KB
/
retry.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package handler
import (
"context"
"net/http"
"sync"
"go.uber.org/zap"
"knative.dev/eventing/pkg/logging"
"cloud.google.com/go/pubsub"
"github.com/google/knative-gcp/pkg/broker/config"
handlerctx "github.com/google/knative-gcp/pkg/broker/handler/context"
"github.com/google/knative-gcp/pkg/broker/handler/processors"
"github.com/google/knative-gcp/pkg/broker/handler/processors/deliver"
"github.com/google/knative-gcp/pkg/broker/handler/processors/filter"
"github.com/google/knative-gcp/pkg/metrics"
)
// RetryPool is the sync pool for retry handlers.
// For each trigger in the config, it will attempt to create a handler.
// It will also stop/delete the handler if the corresponding trigger is deleted
// in the config.
type RetryPool struct {
options *Options
targets config.ReadonlyTargets
pool sync.Map
// Pubsub client used to pull events from decoupling topics.
pubsubClient *pubsub.Client
// For initial events delivery. We only need a shared client.
// And we can set target address dynamically.
deliverClient *http.Client
statsReporter *metrics.DeliveryReporter
}
type retryHandlerCache struct {
Handler
t *config.Target
}
// If somehow the existing handler's setting has deviated from the current target config,
// we need to renew the handler.
func (hc *retryHandlerCache) shouldRenew(t *config.Target) bool {
if !hc.IsAlive() {
return true
}
// If this really happens, it means a data corruption.
// The handler creation will fail (which is expected).
if t == nil || t.RetryQueue == nil {
return true
}
if t.RetryQueue.Topic != hc.t.RetryQueue.Topic ||
t.RetryQueue.Subscription != hc.t.RetryQueue.Subscription {
return true
}
return false
}
// NewRetryPool creates a new retry handler pool.
func NewRetryPool(
targets config.ReadonlyTargets,
pubsubClient *pubsub.Client,
deliverClient *http.Client,
statsReporter *metrics.DeliveryReporter,
opts ...Option) (*RetryPool, error) {
options, err := NewOptions(opts...)
if err != nil {
return nil, err
}
p := &RetryPool{
targets: targets,
options: options,
pubsubClient: pubsubClient,
deliverClient: deliverClient,
statsReporter: statsReporter,
}
return p, nil
}
// SyncOnce syncs once the handler pool based on the targets config.
func (p *RetryPool) SyncOnce(ctx context.Context) error {
ctx, err := p.statsReporter.AddTags(ctx)
if err != nil {
logging.FromContext(ctx).Error("failed to add tags to context", zap.Error(err))
}
p.pool.Range(func(key, value interface{}) bool {
// Each target represents a trigger.
if _, ok := p.targets.GetTargetByKey(key.(string)); !ok {
value.(*retryHandlerCache).Stop()
p.pool.Delete(key)
}
return true
})
p.targets.RangeAllTargets(func(t *config.Target) bool {
if value, ok := p.pool.Load(t.Key()); ok {
// Skip if we don't need to renew the handler.
if !value.(*retryHandlerCache).shouldRenew(t) {
return true
}
// Stop and clean up the old handler before we start a new one.
value.(*retryHandlerCache).Stop()
p.pool.Delete(t.Key())
}
// Don't start the handler if the target is not ready.
// The retry topic/sub might not be ready at this point.
if t.State != config.State_READY {
return true
}
sub := p.pubsubClient.Subscription(t.RetryQueue.Subscription)
sub.ReceiveSettings = p.options.PubsubReceiveSettings
h := NewHandler(
sub,
processors.ChainProcessors(
&filter.Processor{Targets: p.targets},
&deliver.Processor{
DeliverClient: p.deliverClient,
Targets: p.targets,
StatsReporter: p.statsReporter,
},
),
p.options.TimeoutPerEvent,
)
hc := &retryHandlerCache{
Handler: *h,
t: t,
}
ctx, err := metrics.AddTargetTags(ctx, t)
if err != nil {
logging.FromContext(ctx).Error("failed to add target tags to context", zap.Error(err))
}
// Deliver processor needs the broker in the context for reply.
ctx = handlerctx.WithBrokerKey(ctx, config.BrokerKey(t.Namespace, t.Broker))
ctx = handlerctx.WithTargetKey(ctx, t.Key())
// Start the handler with target in context.
hc.Start(ctx, func(err error) {
// We will anyway get an error because of https://github.com/cloudevents/sdk-go/issues/470
if err != nil {
logging.FromContext(ctx).Error("handler for trigger has stopped with error", zap.String("trigger", t.Key()), zap.Error(err))
} else {
logging.FromContext(ctx).Info("handler for trigger has stopped", zap.String("trigger", t.Key()))
}
})
p.pool.Store(t.Key(), hc)
return true
})
return nil
}