fix dldcounter bug

This commit is contained in:
gzliuxin
2018-11-26 18:24:44 +08:00
parent 1f68fcb45f
commit 033f0a9271
2 changed files with 12 additions and 6 deletions

16
db.go
View File

@@ -79,20 +79,26 @@ func (model *Model) AutoSave() bool {
return false
}
type dldEntry struct {
ip string
path string
}
// DldCounter is used to validate duplicated download requests
type DldCounter struct {
IPHistory map[string]time.Time
IPHistory map[dldEntry]time.Time
}
var DCounter DldCounter = DldCounter{}
var DldInterval float64 = 60.0 // seconds
func (counter *DldCounter) Validate(ip string) bool {
func (counter *DldCounter) Validate(ip string, path string) bool {
if counter.IPHistory == nil {
counter.IPHistory = make(map[string]time.Time)
counter.IPHistory = make(map[dldEntry]time.Time)
}
if time.Since(counter.IPHistory[ip]).Seconds() > DldInterval {
counter.IPHistory[ip] = time.Now()
entry := dldEntry{ip, path}
if time.Since(counter.IPHistory[entry]).Seconds() > DldInterval {
counter.IPHistory[entry] = time.Now()
// fmt.Println("valid", ip)
return true
}

View File

@@ -770,7 +770,7 @@ func checkFilename(name string) error {
func DldIncre(model Model, rAddress string, path string) {
ip, _, _ := net.SplitHostPort(rAddress)
if DCounter.Validate(ip) {
if DCounter.Validate(ip, path) {
model.Incre(path)
}
}