Skip to content

Commit

Permalink
Clean up clients
Browse files Browse the repository at this point in the history
  • Loading branch information
espebra committed Jun 8, 2024
1 parent 048728b commit 57f3e40
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
14 changes: 13 additions & 1 deletion dbl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"net"
//"net/http"
"time"

"github.com/espebra/filebin2/ds"
Expand Down Expand Up @@ -132,3 +131,16 @@ func (c *ClientDao) Ban(IPsToBan []string, banByRemoteAddr string) (err error) {
}
return err
}

func (c *ClientDao) Cleanup(days uint64) (count int64, err error) {
sqlStatement := fmt.Sprintf("DELETE FROM client WHERE last_active_at < CURRENT_DATE - CAST('%d days' AS interval) AND banned_at IS NULL", days)
res, err := c.db.Exec(sqlStatement)
if err != nil {
return 0, err
}
count, err = res.RowsAffected()
if err != nil {
return 0, err
}
return count, nil
}
12 changes: 12 additions & 0 deletions lurker.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (l *Lurker) Run() {
l.DeletePendingFiles()
l.DeletePendingBins()
l.CleanTransactions()
l.CleanClients()
fmt.Printf("Lurker completed run in %.3fs\n", time.Since(t0).Seconds())
}
}
Expand Down Expand Up @@ -115,3 +116,14 @@ func (l *Lurker) CleanTransactions() {
fmt.Printf("Removed %d log transactions.\n", count)
}
}

func (l *Lurker) CleanClients() {
count, err := l.dao.Client().Cleanup(l.retention)
if err != nil {
fmt.Printf("Unable to Client().Cleanup(): %s\n", err.Error())
return
}
if count > 0 {
fmt.Printf("Removed %d client entries.\n", count)
}
}
1 change: 1 addition & 0 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ CREATE INDEX idx_transaction_timestamp ON transaction(timestamp);
CREATE INDEX idx_file_deleted_at_in_storage ON file(deleted_at, in_storage);
CREATE INDEX idx_bin_deleted_at_expired_at ON bin(expired_at, deleted_at);
CREATE INDEX idx_sha256 ON file(sha256);
CREATE INDEX idx_client_banned_at ON client(banned_at, last_active_at);

0 comments on commit 57f3e40

Please sign in to comment.