优化代码

This commit is contained in:
刘祥超
2022-01-11 16:02:41 +08:00
parent e23f4aaee2
commit 2f6414fc55
2 changed files with 6 additions and 11 deletions

View File

@@ -206,6 +206,7 @@ func (this *FileList) Add(hash string, item *Item) error {
return err
}
this.memoryCache.Write(hash, 1, item.ExpiredAt)
atomic.AddInt64(&this.total, 1)
if this.onAdd != nil {

View File

@@ -66,9 +66,9 @@ func (this *ClientConn) Read(b []byte) (n int, err error) {
if synFloodConfig != nil && synFloodConfig.IsOn {
if err != nil && os.IsTimeout(err) {
if !this.hasRead {
this.checkSYNFlood()
this.checkSYNFlood(synFloodConfig)
}
} else {
} else if err == nil {
this.resetSYNFlood()
}
}
@@ -123,23 +123,17 @@ func (this *ClientConn) SetWriteDeadline(t time.Time) error {
}
func (this *ClientConn) resetSYNFlood() {
// 为了不影响性能,暂时不清除状态
//ttlcache.SharedCache.Delete("SYN_FLOOD:" + this.RawIP())
}
func (this *ClientConn) checkSYNFlood() {
var synFloodConfig = sharedNodeConfig.SYNFloodConfig()
if synFloodConfig == nil || !synFloodConfig.IsOn {
return
}
func (this *ClientConn) checkSYNFlood(synFloodConfig *firewallconfigs.SYNFloodConfig) {
var ip = this.RawIP()
if len(ip) > 0 && !iplibrary.IsInWhiteList(ip) && (!synFloodConfig.IgnoreLocal || !utils.IsLocalIP(ip)) {
var timestamp = utils.NextMinuteUnixTime()
var result = ttlcache.SharedCache.IncreaseInt64("SYN_FLOOD:"+ip, 1, timestamp)
var minAttempts = synFloodConfig.MinAttempts
if minAttempts < 3 {
minAttempts = 3
if minAttempts < 5 {
minAttempts = 5
}
if result >= int64(minAttempts) {
var timeout = synFloodConfig.TimeoutSeconds