fix: Avoid closing target in RegisterNotificationTargets (#8966)

This will prevent a double target Close() call when fetchLambdaInfo()
is executed (mc admin info)

This fixes a crash when mc admin info is called.
This commit is contained in:
Anis Elleuch 2020-02-07 23:35:56 +01:00 committed by GitHub
parent de924605a1
commit 502e652b7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 34 deletions

View File

@ -42,7 +42,14 @@ const (
func TestNotificationTargets(cfg config.Config, doneCh <-chan struct{}, transport *http.Transport,
targetIDs []event.TargetID) error {
test := true
_, err := RegisterNotificationTargets(cfg, doneCh, transport, targetIDs, test)
targets, err := RegisterNotificationTargets(cfg, doneCh, transport, targetIDs, test)
if err == nil {
// Close all targets since we are only testing connections.
for _, t := range targets.TargetMap() {
_ = t.Close()
}
}
return err
}
@ -58,8 +65,18 @@ func GetNotificationTargets(cfg config.Config, doneCh <-chan struct{}, transport
// * Add a new target in pkg/event/target package.
// * Add newly added target configuration to serverConfig.Notify.<TARGET_NAME>.
// * Handle the configuration in this function to create/add into TargetList.
func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, transport *http.Transport, targetIDs []event.TargetID, test bool) (*event.TargetList, error) {
targetList := event.NewTargetList()
func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, transport *http.Transport, targetIDs []event.TargetID, test bool) (targetList *event.TargetList, registerErr error) {
targetList = event.NewTargetList()
// Automatially close all connections when an error occur
defer func() {
if registerErr != nil {
for _, t := range targetList.TargetMap() {
_ = t.Close()
}
}
}()
if err := checkValidNotificationKeys(cfg); err != nil {
return nil, err
}
@ -125,9 +142,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran
if err = targetList.Add(newTarget); err != nil {
return nil, err
}
if test {
newTarget.Close()
}
}
for id, args := range esTargets {
@ -142,9 +156,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran
if err = targetList.Add(newTarget); err != nil {
return nil, err
}
if test {
newTarget.Close()
}
}
for id, args := range kafkaTargets {
@ -159,9 +170,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran
if err = targetList.Add(newTarget); err != nil {
return nil, err
}
if test {
newTarget.Close()
}
}
for id, args := range mqttTargets {
@ -176,9 +184,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran
if err = targetList.Add(newTarget); err != nil {
return nil, err
}
if test {
newTarget.Close()
}
}
for id, args := range mysqlTargets {
@ -192,9 +197,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran
if err = targetList.Add(newTarget); err != nil {
return nil, err
}
if test {
newTarget.Close()
}
}
for id, args := range natsTargets {
@ -208,9 +210,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran
if err = targetList.Add(newTarget); err != nil {
return nil, err
}
if test {
newTarget.Close()
}
}
for id, args := range nsqTargets {
@ -224,9 +223,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran
if err = targetList.Add(newTarget); err != nil {
return nil, err
}
if test {
newTarget.Close()
}
}
for id, args := range postgresTargets {
@ -240,9 +236,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran
if err = targetList.Add(newTarget); err != nil {
return nil, err
}
if test {
newTarget.Close()
}
}
for id, args := range redisTargets {
@ -256,9 +249,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran
if err = targetList.Add(newTarget); err != nil {
return nil, err
}
if test {
newTarget.Close()
}
}
for id, args := range webhookTargets {
@ -272,10 +262,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran
if err := targetList.Add(newTarget); err != nil {
return nil, err
}
if test {
newTarget.Close()
continue
}
}
if test {