-
Notifications
You must be signed in to change notification settings - Fork 24
/
concurrency-limiter.go
91 lines (83 loc) · 3.1 KB
/
concurrency-limiter.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
package concurrency
import (
"context"
"errors"
"path"
"go.uber.org/fx"
"google.golang.org/protobuf/proto"
policylangv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/policy/language/v1"
policysyncv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/policy/sync/v1"
etcdclient "github.com/fluxninja/aperture/pkg/etcd/client"
"github.com/fluxninja/aperture/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/pkg/policies/paths"
clientv3 "go.etcd.io/etcd/client/v3"
)
type concurrencyLimiterConfigSync struct {
policyBaseAPI iface.Policy
concurrencyLimiterProto *policylangv1.ConcurrencyLimiter
etcdPath string
componentID string
}
// NewConcurrencyLimiterOptions creates fx options for ConcurrencyLimiter and also returns the agent group name associated with it.
func NewConcurrencyLimiterOptions(
concurrencyLimiterProto *policylangv1.ConcurrencyLimiter,
componentStackID string,
policyReadAPI iface.Policy,
) (fx.Option, string, error) {
// Get Agent Group Name from ConcurrencyLimiter.FlowSelector.ServiceSelector.AgentGroup
flowSelectorProto := concurrencyLimiterProto.GetFlowSelector()
if flowSelectorProto == nil {
return fx.Options(), "", errors.New("concurrencyLimiter.Selector is nil")
}
agentGroup := flowSelectorProto.ServiceSelector.GetAgentGroup()
etcdPath := path.Join(paths.ConcurrencyLimiterConfigPath,
paths.AgentComponentKey(agentGroup, policyReadAPI.GetPolicyName(), componentStackID))
configSync := &concurrencyLimiterConfigSync{
concurrencyLimiterProto: concurrencyLimiterProto,
policyBaseAPI: policyReadAPI,
etcdPath: etcdPath,
componentID: componentStackID,
}
return fx.Options(
fx.Invoke(
configSync.doSync,
),
), agentGroup, nil
}
func (configSync *concurrencyLimiterConfigSync) doSync(etcdClient *etcdclient.Client, lifecycle fx.Lifecycle) error {
logger := configSync.policyBaseAPI.GetStatusRegistry().GetLogger()
// Add/remove file in lifecycle hooks in order to sync with etcd.
lifecycle.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
wrapper := &policysyncv1.ConcurrencyLimiterWrapper{
ConcurrencyLimiter: configSync.concurrencyLimiterProto,
CommonAttributes: &policysyncv1.CommonAttributes{
PolicyName: configSync.policyBaseAPI.GetPolicyName(),
PolicyHash: configSync.policyBaseAPI.GetPolicyHash(),
ComponentId: configSync.componentID,
},
}
dat, err := proto.Marshal(wrapper)
if err != nil {
logger.Error().Err(err).Msg("Failed to marshal flux meter config")
return err
}
_, err = etcdClient.KV.Put(clientv3.WithRequireLeader(ctx),
configSync.etcdPath, string(dat), clientv3.WithLease(etcdClient.LeaseID))
if err != nil {
logger.Error().Err(err).Msg("Failed to put flux meter config")
return err
}
return nil
},
OnStop: func(ctx context.Context) error {
_, err := etcdClient.KV.Delete(clientv3.WithRequireLeader(ctx), configSync.etcdPath)
if err != nil {
logger.Error().Err(err).Msg("Failed to delete flux meter config")
return err
}
return nil
},
})
return nil
}