Skip to content

Commit 757c323

Browse files
authored
refact: don't call defer in loops, enable linter (#3907)
1 parent 83d4a7a commit 757c323

File tree

4 files changed

+77
-75
lines changed

4 files changed

+77
-75
lines changed

.golangci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ linters:
157157
- sloppyReassign
158158
- appendCombine
159159
- commentFormatting
160-
- deferInLoop #
161160
- whyNoLint
162161
- equalFold #
163162
- unnecessaryBlock #
@@ -220,8 +219,6 @@ linters:
220219
arguments:
221220
# lower this after refactoring
222221
- 39
223-
- name: defer
224-
disabled: true
225222
- name: empty-block
226223
disabled: true
227224
- name: empty-lines

pkg/apiserver/apic.go

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -742,102 +742,102 @@ func (a *apic) PullAllowlist(ctx context.Context, allowlist *modelscapi.Allowlis
742742
return nil
743743
}
744744

745-
func (a *apic) UpdateAllowlists(ctx context.Context, allowlistsLinks []*modelscapi.AllowlistLink, forcePull bool) error {
746-
if len(allowlistsLinks) == 0 {
745+
func (a *apic) updateOneAllowlist(ctx context.Context, client *apiclient.ApiClient, link *modelscapi.AllowlistLink) error {
746+
if log.IsLevelEnabled(log.TraceLevel) {
747+
log.Tracef("allowlist body: %+v", spew.Sdump(link))
748+
}
749+
750+
if link.Name == nil {
751+
log.Warn("allowlist has no name")
747752
return nil
748753
}
749754

750-
defaultClient, err := apiclient.NewDefaultClient(a.apiClient.BaseURL, "", "", nil)
755+
if link.URL == nil {
756+
log.Warnf("allowlist %s has no URL", *link.Name)
757+
return nil
758+
}
759+
760+
if link.ID == nil {
761+
return fmt.Errorf("allowlist %s has no ID", *link.Name)
762+
}
763+
764+
description := ""
765+
if link.Description != nil {
766+
description = *link.Description
767+
}
768+
769+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, *link.URL, http.NoBody)
751770
if err != nil {
752-
return fmt.Errorf("while creating default client: %w", err)
771+
return fmt.Errorf("while pulling allowlist: %s", err)
753772
}
754773

755-
for _, link := range allowlistsLinks {
756-
if log.IsLevelEnabled(log.TraceLevel) {
757-
log.Tracef("allowlist body: %+v", spew.Sdump(link))
758-
}
774+
resp, err := client.GetClient().Do(req)
775+
if err != nil {
776+
return fmt.Errorf("while pulling allowlist: %s", err)
777+
}
778+
defer resp.Body.Close()
759779

760-
if link.Name == nil {
761-
log.Warningf("allowlist has no name")
762-
continue
763-
}
780+
scanner := bufio.NewScanner(resp.Body)
781+
items := make([]*models.AllowlistItem, 0)
764782

765-
if link.URL == nil {
766-
log.Warningf("allowlist %s has no URL", *link.Name)
767-
continue
768-
}
783+
for scanner.Scan() {
784+
item := scanner.Text()
785+
j := &models.AllowlistItem{}
769786

770-
if link.ID == nil {
771-
log.Warningf("allowlist %s has no ID", *link.Name)
772-
continue
787+
if err := json.Unmarshal([]byte(item), j); err != nil {
788+
return fmt.Errorf("while unmarshalling allowlist item: %s", err)
773789
}
774790

775-
description := ""
776-
if link.Description != nil {
777-
description = *link.Description
778-
}
791+
items = append(items, j)
792+
}
779793

780-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, *link.URL, http.NoBody)
781-
if err != nil {
782-
log.Errorf("while pulling allowlist: %s", err)
783-
continue
794+
list, err := a.dbClient.GetAllowListByID(ctx, *link.ID, false)
795+
if err != nil {
796+
if !ent.IsNotFound(err) {
797+
return fmt.Errorf("while getting allowlist %s: %s", *link.Name, err)
784798
}
799+
}
785800

786-
resp, err := defaultClient.GetClient().Do(req)
801+
if list == nil {
802+
list, err = a.dbClient.CreateAllowList(ctx, *link.Name, description, *link.ID, true)
787803
if err != nil {
788-
log.Errorf("while pulling allowlist: %s", err)
789-
continue
804+
return fmt.Errorf("while creating allowlist %s: %s", *link.Name, err)
790805
}
791-
defer resp.Body.Close()
792-
793-
scanner := bufio.NewScanner(resp.Body)
794-
items := make([]*models.AllowlistItem, 0)
795-
796-
for scanner.Scan() {
797-
item := scanner.Text()
798-
j := &models.AllowlistItem{}
806+
}
799807

800-
if err := json.Unmarshal([]byte(item), j); err != nil {
801-
log.Errorf("while unmarshalling allowlist item: %s", err)
802-
continue
803-
}
808+
added, err := a.dbClient.ReplaceAllowlist(ctx, list, items, true)
809+
if err != nil {
810+
return fmt.Errorf("while replacing allowlist %s: %s", *link.Name, err)
811+
}
804812

805-
items = append(items, j)
806-
}
813+
log.Infof("added %d values to allowlist %s", added, list.Name)
807814

808-
list, err := a.dbClient.GetAllowListByID(ctx, *link.ID, false)
815+
if list.Name != *link.Name || list.Description != description {
816+
err = a.dbClient.UpdateAllowlistMeta(ctx, *link.ID, *link.Name, description)
809817
if err != nil {
810-
if !ent.IsNotFound(err) {
811-
log.Errorf("while getting allowlist %s: %s", *link.Name, err)
812-
continue
813-
}
818+
return fmt.Errorf("while updating allowlist meta %s: %s", *link.Name, err)
814819
}
820+
}
815821

816-
if list == nil {
817-
list, err = a.dbClient.CreateAllowList(ctx, *link.Name, description, *link.ID, true)
818-
if err != nil {
819-
log.Errorf("while creating allowlist %s: %s", *link.Name, err)
820-
continue
821-
}
822-
}
822+
log.Infof("Allowlist %s updated", *link.Name)
823823

824-
added, err := a.dbClient.ReplaceAllowlist(ctx, list, items, true)
825-
if err != nil {
826-
log.Errorf("while replacing allowlist %s: %s", *link.Name, err)
827-
continue
828-
}
824+
return nil
825+
}
829826

830-
log.Infof("added %d values to allowlist %s", added, list.Name)
827+
func (a *apic) UpdateAllowlists(ctx context.Context, allowlistsLinks []*modelscapi.AllowlistLink, forcePull bool) error {
828+
if len(allowlistsLinks) == 0 {
829+
return nil
830+
}
831831

832-
if list.Name != *link.Name || list.Description != description {
833-
err = a.dbClient.UpdateAllowlistMeta(ctx, *link.ID, *link.Name, description)
834-
if err != nil {
835-
log.Errorf("while updating allowlist meta %s: %s", *link.Name, err)
836-
continue
837-
}
838-
}
832+
client, err := apiclient.NewDefaultClient(a.apiClient.BaseURL, "", "", nil)
833+
if err != nil {
834+
return fmt.Errorf("while creating default client: %w", err)
835+
}
839836

840-
log.Infof("Allowlist %s updated", *link.Name)
837+
for _, link := range allowlistsLinks {
838+
if err := a.updateOneAllowlist(ctx, client, link); err != nil {
839+
log.Errorf("updating allowlists from CAPI: %s", err)
840+
}
841841
}
842842

843843
return nil

pkg/apiserver/apiserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func isBrokenConnection(maybeError any) bool {
8484
}
8585

8686
func recoverFromPanic(c *gin.Context) {
87-
err := recover()
87+
err := recover() //nolint:revive
8888
if err == nil {
8989
return
9090
}

pkg/leakybucket/bucket.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ func LeakRoutine(leaky *Leaky) error {
163163
firstEvent = true
164164
)
165165

166+
defer func() {
167+
if durationTicker != nil {
168+
durationTicker.Stop()
169+
}
170+
}()
171+
166172
defer trace.CatchPanic(fmt.Sprintf("crowdsec/LeakRoutine/%s", leaky.Name))
167173

168174
metrics.BucketsCurrentCount.With(prometheus.Labels{"name": leaky.Name}).Inc()
@@ -232,7 +238,6 @@ func LeakRoutine(leaky *Leaky) error {
232238
if firstEvent {
233239
durationTicker = time.NewTicker(leaky.Duration)
234240
durationTickerChan = durationTicker.C
235-
defer durationTicker.Stop()
236241
} else {
237242
durationTicker.Reset(leaky.Duration)
238243
}

0 commit comments

Comments
 (0)