优化代码

This commit is contained in:
GoEdgeLab
2022-04-04 18:25:54 +08:00
parent 4aeb3cc7b0
commit 2df6e5a51b
4 changed files with 31 additions and 13 deletions

View File

@@ -338,6 +338,11 @@ func (this *FileReader) ContainsRange(r rangeutils.Range) (r2 rangeutils.Range,
return r, true return r, true
} }
// FP 原始的文件句柄
func (this *FileReader) FP() *os.File {
return this.fp
}
func (this *FileReader) Close() error { func (this *FileReader) Close() error {
if this.openFileCache != nil { if this.openFileCache != nil {
if this.isClosed { if this.isClosed {

View File

@@ -3,9 +3,9 @@
package metrics package metrics
import ( import (
"encoding/json" "github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
"github.com/cespare/xxhash"
"strconv" "strconv"
"strings"
) )
type Stat struct { type Stat struct {
@@ -17,6 +17,6 @@ type Stat struct {
} }
func SumStat(serverId int64, keys []string, time string, version int32, itemId int64) string { func SumStat(serverId int64, keys []string, time string, version int32, itemId int64) string {
keysData, _ := json.Marshal(keys) keysData := strings.Join(keys, "$EDGE$")
return strconv.FormatUint(xxhash.Sum64String(strconv.FormatInt(serverId, 10)+"@"+string(keysData)+"@"+time+"@"+strconv.Itoa(int(version))+"@"+strconv.FormatInt(itemId, 10)), 10) return strconv.FormatUint(fnv.HashString(strconv.FormatInt(serverId, 10)+"@"+keysData+"@"+time+"@"+strconv.Itoa(int(version))+"@"+strconv.FormatInt(itemId, 10)), 10)
} }

View File

@@ -3,9 +3,11 @@
package re package re
import ( import (
"github.com/iwind/TeaGo/types"
"regexp" "regexp"
"regexp/syntax" "regexp/syntax"
"strings" "strings"
"sync/atomic"
) )
var prefixReg = regexp.MustCompile(`^\(\?([\w\s]+)\)`) // (?x) var prefixReg = regexp.MustCompile(`^\(\?([\w\s]+)\)`) // (?x)
@@ -13,6 +15,8 @@ var prefixReg2 = regexp.MustCompile(`^\(\?([\w\s]*:)`) // (?x: ...
var braceZeroReg = regexp.MustCompile(`^{\s*0*\s*}`) // {0} var braceZeroReg = regexp.MustCompile(`^{\s*0*\s*}`) // {0}
var braceZeroReg2 = regexp.MustCompile(`^{\s*0*\s*,`) // {0, x} var braceZeroReg2 = regexp.MustCompile(`^{\s*0*\s*,`) // {0, x}
var lastId uint64
type Regexp struct { type Regexp struct {
exp string exp string
rawRegexp *regexp.Regexp rawRegexp *regexp.Regexp
@@ -21,6 +25,9 @@ type Regexp struct {
isCaseInsensitive bool isCaseInsensitive bool
keywords []string keywords []string
keywordsMap RuneMap keywordsMap RuneMap
id uint64
idString string
} }
func MustCompile(exp string) *Regexp { func MustCompile(exp string) *Regexp {
@@ -50,6 +57,9 @@ func NewRegexp(rawRegexp *regexp.Regexp) *Regexp {
} }
func (this *Regexp) init() { func (this *Regexp) init() {
this.id = atomic.AddUint64(&lastId, 1)
this.idString = "re:" + types.String(this.id)
if len(this.exp) == 0 { if len(this.exp) == 0 {
return return
} }
@@ -202,6 +212,10 @@ func (this *Regexp) ParseKeywords(exp string) (keywords []string) {
return return
} }
func (this *Regexp) IdString() string {
return this.idString
}
func (this *Regexp) parseKeyword(subExp string) (result []rune) { func (this *Regexp) parseKeyword(subExp string) (result []rune) {
if len(subExp) == 0 { if len(subExp) == 0 {
return nil return nil

View File

@@ -1,7 +1,6 @@
package utils package utils
import ( import (
"fmt"
"github.com/TeaOSLab/EdgeNode/internal/re" "github.com/TeaOSLab/EdgeNode/internal/re"
"github.com/TeaOSLab/EdgeNode/internal/ttlcache" "github.com/TeaOSLab/EdgeNode/internal/ttlcache"
"github.com/cespare/xxhash" "github.com/cespare/xxhash"
@@ -19,13 +18,13 @@ func MatchStringCache(regex *re.Regexp, s string) bool {
return regex.MatchString(s) return regex.MatchString(s)
} }
hash := xxhash.Sum64String(s) var hash = xxhash.Sum64String(s)
key := fmt.Sprintf("%p_", regex) + strconv.FormatUint(hash, 10) var key = regex.IdString() + "@" + strconv.FormatUint(hash, 10)
item := cache.Read(key) var item = cache.Read(key)
if item != nil { if item != nil {
return types.Int8(item.Value) == 1 return types.Int8(item.Value) == 1
} }
b := regex.MatchString(s) var b = regex.MatchString(s)
if b { if b {
cache.Write(key, 1, time.Now().Unix()+1800) cache.Write(key, 1, time.Now().Unix()+1800)
} else { } else {
@@ -41,16 +40,16 @@ func MatchBytesCache(regex *re.Regexp, byteSlice []byte) bool {
return regex.Match(byteSlice) return regex.Match(byteSlice)
} }
hash := xxhash.Sum64(byteSlice) var hash = xxhash.Sum64(byteSlice)
key := fmt.Sprintf("%p_", regex) + strconv.FormatUint(hash, 10) var key = regex.IdString() + "@" + strconv.FormatUint(hash, 10)
item := cache.Read(key) var item = cache.Read(key)
if item != nil { if item != nil {
return types.Int8(item.Value) == 1 return types.Int8(item.Value) == 1
} }
if item != nil { if item != nil {
return types.Int8(item.Value) == 1 return types.Int8(item.Value) == 1
} }
b := regex.Match(byteSlice) var b = regex.Match(byteSlice)
if b { if b {
cache.Write(key, 1, time.Now().Unix()+1800) cache.Write(key, 1, time.Now().Unix()+1800)
} else { } else {