mirror of
https://github.com/restic/rest-server.git
synced 2025-12-07 09:36:13 -08:00
Limit htpasswd checks to once per 30s
This commit is contained in:
committed by
Zlatko Čalušić
parent
67a0f63773
commit
526a2b3837
10
htpasswd.go
10
htpasswd.go
@@ -34,6 +34,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const CheckInterval = 30 * time.Second
|
||||||
|
|
||||||
// Lookup passwords in a htpasswd file. The entries must have been created with -s for SHA encryption.
|
// Lookup passwords in a htpasswd file. The entries must have been created with -s for SHA encryption.
|
||||||
|
|
||||||
// HtpasswdFile is a map for usernames to passwords.
|
// HtpasswdFile is a map for usernames to passwords.
|
||||||
@@ -64,17 +66,17 @@ func NewHtpasswdFromFile(path string) (*HtpasswdFile, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start a goroutine that limits reload checks to once a second at most
|
// Start a goroutine that limits reload checks to once per CheckInterval
|
||||||
go h.throttleTimer()
|
go h.throttleTimer()
|
||||||
|
|
||||||
return h, nil
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// throttleTimer sends at most one message per second to throttle file change checks.
|
// throttleTimer sends at most one message per CheckInterval to throttle file change checks.
|
||||||
func (h *HtpasswdFile) throttleTimer() {
|
func (h *HtpasswdFile) throttleTimer() {
|
||||||
var check struct{}
|
var check struct{}
|
||||||
for {
|
for {
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(CheckInterval)
|
||||||
h.throttle <- check
|
h.throttle <- check
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,7 +110,7 @@ func (h *HtpasswdFile) Reload() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReloadCheck checks at most once per second if the file changed and will reload the file if it did.
|
// ReloadCheck checks at most once per CheckInterval if the file changed and will reload the file if it did.
|
||||||
// It logs errors and successful reloads, and returns an error if any was encountered.
|
// It logs errors and successful reloads, and returns an error if any was encountered.
|
||||||
func (h *HtpasswdFile) ReloadCheck() error {
|
func (h *HtpasswdFile) ReloadCheck() error {
|
||||||
select {
|
select {
|
||||||
|
|||||||
Reference in New Issue
Block a user