update: code clean

This commit is contained in:
Senis John 2022-11-30 09:08:36 +08:00
parent 8d0225bcbb
commit b6600729b2
3 changed files with 10 additions and 14 deletions

View File

@ -2,8 +2,6 @@ package limiter
import "github.com/xtls/xray-core/common/errors" import "github.com/xtls/xray-core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error { func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{}) return errors.New(values...)
} }

View File

@ -183,10 +183,10 @@ func (l *Limiter) GetUserBucket(tag string, email string, ip string) (limiter *r
ipMap.Delete(ip) ipMap.Delete(ip)
return nil, false, true return nil, false, true
} }
go l.pushIP(email, ip, uid) go l.pushIP(email, ip)
} }
} else { } else {
go l.pushIP(email, ip, uid) go l.pushIP(email, ip)
} }
} }
@ -210,18 +210,18 @@ func (l *Limiter) GetUserBucket(tag string, email string, ip string) (limiter *r
} }
// Push new IP to redis // Push new IP to redis
func (l *Limiter) pushIP(email string, ip string, uid int) { func (l *Limiter) pushIP(email string, ip string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(l.g.Timeout)) ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(l.g.Timeout))
defer cancel() defer cancel()
if err := l.g.R.HSet(ctx, email, map[string]any{ip: uid}).Err(); err != nil { if err := l.g.R.HSet(ctx, email, map[string]any{ip: 0}).Err(); err != nil {
newError(fmt.Sprintf("Redis: %v", err)).AtError().WriteToLog() newError(fmt.Errorf("redis: %v", err)).AtError().WriteToLog()
} }
// check ttl, if ttl == -1, then set expire time. // check ttl, if ttl == -1, then set expire time.
if l.g.R.TTL(ctx, email).Val() == -1 { if l.g.R.TTL(ctx, email).Val() == -1 {
if err := l.g.R.Expire(ctx, email, time.Duration(l.g.Expiry)*time.Minute).Err(); err != nil { if err := l.g.R.Expire(ctx, email, time.Duration(l.g.Expiry)*time.Minute).Err(); err != nil {
newError(fmt.Sprintf("Redis: %v", err)).AtError().WriteToLog() newError(fmt.Errorf("redis: %v", err)).AtError().WriteToLog()
} }
} }
} }

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"log" "log"
"reflect" "reflect"
"strconv"
"sync" "sync"
"time" "time"
@ -654,7 +653,7 @@ func (c *Controller) globalLimitFetch() (err error) {
inboundInfo := value.(*limiter.InboundInfo) inboundInfo := value.(*limiter.InboundInfo)
for { for {
if emails, cursor, err = c.g.R.Scan(ctx, cursor, "*", 1000).Result(); err != nil { if emails, cursor, err = c.g.R.Scan(ctx, cursor, "*", 10000).Result(); err != nil {
newError(err).AtError().WriteToLog() newError(err).AtError().WriteToLog()
} }
pipe := c.g.R.Pipeline() pipe := c.g.R.Pipeline()
@ -666,15 +665,14 @@ func (c *Controller) globalLimitFetch() (err error) {
} }
if _, err := pipe.Exec(ctx); err != nil { if _, err := pipe.Exec(ctx); err != nil {
newError(fmt.Sprintf("Redis: %v", err)).AtError().WriteToLog() newError(fmt.Errorf("redis: %v", err)).AtError().WriteToLog()
} else { } else {
inboundInfo.GlobalOnlineIP = new(sync.Map) inboundInfo.GlobalOnlineIP = new(sync.Map)
for k := range cmdMap { for k := range cmdMap {
ips := cmdMap[k].Val() ips := cmdMap[k].Val()
ipMap := new(sync.Map) ipMap := new(sync.Map)
for i := range ips { for i := range ips {
uid, _ := strconv.Atoi(ips[i]) ipMap.Store(i, 0)
ipMap.Store(i, uid)
inboundInfo.GlobalOnlineIP.Store(k, ipMap) inboundInfo.GlobalOnlineIP.Store(k, ipMap)
} }
} }