Skip to content

Commit

Permalink
Use context instead of explicit shutdown channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Javex committed May 25, 2024
1 parent dbfadff commit 28d7ff2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 44 deletions.
25 changes: 6 additions & 19 deletions banlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package caddy_fail2ban

import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"sync"

"github.com/caddyserver/caddy/v2"
"github.com/fsnotify/fsnotify"
"go.uber.org/zap"
)

type Banlist struct {
ctx caddy.Context
bannedIps []string
shutdown chan bool
lock *sync.RWMutex
Expand All @@ -22,10 +23,10 @@ type Banlist struct {
reloadSubs []chan bool
}

func NewBanlist(logger *zap.Logger, banfile *string) Banlist {
func NewBanlist(ctx caddy.Context, logger *zap.Logger, banfile *string) Banlist {
banlist := Banlist{
ctx: ctx,
bannedIps: make([]string, 0),
shutdown: make(chan bool),
lock: new(sync.RWMutex),
logger: logger,
banfile: banfile,
Expand All @@ -52,18 +53,6 @@ func (b *Banlist) IsBanned(remote_ip string) bool {
return false
}

func (b *Banlist) Stop() error {
if b.shutdown != nil {
b.shutdown <- true
_, ok := <-b.shutdown
if ok {
b.logger.Error("Failed to shutdown monitor goroutine")
return errors.New("shutdown of monitor failed")
}
}
return nil
}

func (b *Banlist) Reload() {
resp := make(chan bool)

Expand All @@ -75,7 +64,6 @@ func (b *Banlist) monitorBannedIps() {
b.logger.Info("Starting monitor for banned IPs")
defer func() {
b.logger.Info("Shutting down monitor for banned IPs")
close(b.shutdown)
}()

// Load initial list
Expand Down Expand Up @@ -131,9 +119,8 @@ func (b *Banlist) monitorBannedIps() {
return
}
}
case <-b.shutdown:
// Receive signal to finish
b.logger.Debug("Received shutdown signal")
case <-b.ctx.Done():
b.logger.Debug("Context finished, shutting down")
return
}
}
Expand Down
9 changes: 2 additions & 7 deletions fail2ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ func (Fail2Ban) CaddyModule() caddy.ModuleInfo {
// Provision implements caddy.Provisioner.
func (m *Fail2Ban) Provision(ctx caddy.Context) error {
m.logger = ctx.Logger()
m.banlist = NewBanlist(m.logger, &m.Banfile)
m.banlist = NewBanlist(ctx, m.logger, &m.Banfile)
m.banlist.Start()
return nil
}

func (m *Fail2Ban) Cleanup() error {
return m.banlist.Stop()
}

// Validate implements caddy.Validator.
// func (m *Fail2Ban) Validate() error {
// // if m.w == nil {
Expand Down Expand Up @@ -102,8 +98,7 @@ func (m *Fail2Ban) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {

// Interface guards
var (
_ caddy.Provisioner = (*Fail2Ban)(nil)
_ caddy.CleanerUpper = (*Fail2Ban)(nil)
_ caddy.Provisioner = (*Fail2Ban)(nil)
// _ caddy.Validator = (*Fail2Ban)(nil)
_ caddyhttp.RequestMatcher = (*Fail2Ban)(nil)
_ caddyfile.Unmarshaler = (*Fail2Ban)(nil)
Expand Down
18 changes: 0 additions & 18 deletions fail2ban_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ func TestModule(t *testing.T) {
if err != nil {
t.Errorf("error provisioning: %v", err)
}
defer func() {
err := m.Cleanup()
if err != nil {
t.Fatalf("unexpected error on cleanup: %v", err)
}
}()

req := httptest.NewRequest("GET", "https://127.0.0.1", strings.NewReader(""))

Expand Down Expand Up @@ -93,12 +87,6 @@ func TestHeaderBan(t *testing.T) {
if err != nil {
t.Errorf("error provisioning: %v", err)
}
defer func() {
err := m.Cleanup()
if err != nil {
t.Fatalf("unexpected error on cleanup: %v", err)
}
}()

req := httptest.NewRequest("GET", "https://127.0.0.1", strings.NewReader(""))
req.Header.Add("X-Caddy-Ban", "1")
Expand Down Expand Up @@ -127,12 +115,6 @@ func TestBanIp(t *testing.T) {
if err != nil {
t.Errorf("error provisioning: %v", err)
}
defer func() {
err := m.Cleanup()
if err != nil {
t.Fatalf("unexpected error on cleanup: %v", err)
}
}()

req := httptest.NewRequest("GET", "https://127.0.0.1", strings.NewReader(""))
req.RemoteAddr = "127.0.0.1:1337"
Expand Down

0 comments on commit 28d7ff2

Please sign in to comment.