mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-03 23:20:25 +08:00
优化代码:使用fasttime取代以往的utils.UnixTime
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package caches
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -36,7 +36,7 @@ type Item struct {
|
||||
}
|
||||
|
||||
func (this *Item) IsExpired() bool {
|
||||
return this.ExpiredAt < utils.UnixTime()
|
||||
return this.ExpiredAt < fasttime.Now().Unix()
|
||||
}
|
||||
|
||||
func (this *Item) TotalSize() int64 {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/dbs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
@@ -246,7 +247,7 @@ func (this *FileListDB) AddAsync(hash string, item *Item) error {
|
||||
item.StaleAt = item.ExpiredAt
|
||||
}
|
||||
|
||||
this.writeBatch.Add(this.insertSQL, hash, item.Key, item.HeaderSize, item.BodySize, item.MetaSize, item.ExpiredAt, item.StaleAt, item.Host, item.ServerId, utils.UnixTime(), timeutil.Format("YW"))
|
||||
this.writeBatch.Add(this.insertSQL, hash, item.Key, item.HeaderSize, item.BodySize, item.MetaSize, item.ExpiredAt, item.StaleAt, item.Host, item.ServerId, fasttime.Now().Unix(), timeutil.Format("YW"))
|
||||
return nil
|
||||
|
||||
}
|
||||
@@ -258,7 +259,7 @@ func (this *FileListDB) AddSync(hash string, item *Item) error {
|
||||
item.StaleAt = item.ExpiredAt
|
||||
}
|
||||
|
||||
_, err := this.insertStmt.Exec(hash, item.Key, item.HeaderSize, item.BodySize, item.MetaSize, item.ExpiredAt, item.StaleAt, item.Host, item.ServerId, utils.UnixTime(), timeutil.Format("YW"))
|
||||
_, err := this.insertStmt.Exec(hash, item.Key, item.HeaderSize, item.BodySize, item.MetaSize, item.ExpiredAt, item.StaleAt, item.Host, item.ServerId, fasttime.Now().Unix(), timeutil.Format("YW"))
|
||||
if err != nil {
|
||||
return this.WrapError(err)
|
||||
}
|
||||
@@ -377,8 +378,8 @@ func (this *FileListDB) CleanPrefix(prefix string) error {
|
||||
return nil
|
||||
}
|
||||
var count = int64(10000)
|
||||
var staleLife = 600 // TODO 需要可以设置
|
||||
var unixTime = utils.UnixTime() // 只删除当前的,不删除新的
|
||||
var staleLife = 600 // TODO 需要可以设置
|
||||
var unixTime = fasttime.Now().Unix() // 只删除当前的,不删除新的
|
||||
for {
|
||||
result, err := this.writeDB.Exec(`UPDATE "`+this.itemsTableName+`" SET expiredAt=0,staleAt=? WHERE id IN (SELECT id FROM "`+this.itemsTableName+`" WHERE expiredAt>0 AND createdAt<=? AND INSTR("key", ?)=1 LIMIT `+types.String(count)+`)`, unixTime+int64(staleLife), unixTime, prefix)
|
||||
if err != nil {
|
||||
@@ -424,8 +425,8 @@ func (this *FileListDB) CleanMatchKey(key string) error {
|
||||
queryKey = strings.Replace(queryKey, "*", "%", 1)
|
||||
|
||||
// TODO 检查大批量数据下的操作性能
|
||||
var staleLife = 600 // TODO 需要可以设置
|
||||
var unixTime = utils.UnixTime() // 只删除当前的,不删除新的
|
||||
var staleLife = 600 // TODO 需要可以设置
|
||||
var unixTime = fasttime.Now().Unix() // 只删除当前的,不删除新的
|
||||
|
||||
_, err = this.writeDB.Exec(`UPDATE "`+this.itemsTableName+`" SET "expiredAt"=0, "staleAt"=? WHERE "host" GLOB ? AND "host" NOT GLOB ? AND "key" LIKE ? ESCAPE '\'`, unixTime+int64(staleLife), host, "*."+host, queryKey)
|
||||
if err != nil {
|
||||
@@ -466,8 +467,8 @@ func (this *FileListDB) CleanMatchPrefix(prefix string) error {
|
||||
queryPrefix += "%"
|
||||
|
||||
// TODO 检查大批量数据下的操作性能
|
||||
var staleLife = 600 // TODO 需要可以设置
|
||||
var unixTime = utils.UnixTime() // 只删除当前的,不删除新的
|
||||
var staleLife = 600 // TODO 需要可以设置
|
||||
var unixTime = fasttime.Now().Unix() // 只删除当前的,不删除新的
|
||||
|
||||
_, err = this.writeDB.Exec(`UPDATE "`+this.itemsTableName+`" SET "expiredAt"=0, "staleAt"=? WHERE "host" GLOB ? AND "host" NOT GLOB ? AND "key" LIKE ? ESCAPE '\'`, unixTime+int64(staleLife), host, "*."+host, queryPrefix)
|
||||
return err
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
package caches
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/linkedlist"
|
||||
)
|
||||
|
||||
@@ -19,7 +19,7 @@ func NewOpenFilePool(filename string) *OpenFilePool {
|
||||
var pool = &OpenFilePool{
|
||||
filename: filename,
|
||||
c: make(chan *OpenFile, 1024),
|
||||
version: utils.UnixTimeMilli(),
|
||||
version: fasttime.Now().UnixMilli(),
|
||||
}
|
||||
pool.linkItem = linkedlist.NewItem(pool)
|
||||
return pool
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/trackers"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
setutils "github.com/TeaOSLab/EdgeNode/internal/utils/sets"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/sizes"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/zero"
|
||||
@@ -32,7 +33,7 @@ type MemoryItem struct {
|
||||
}
|
||||
|
||||
func (this *MemoryItem) IsExpired() bool {
|
||||
return this.ExpiresAt < utils.UnixTime()
|
||||
return this.ExpiresAt < fasttime.Now().Unix()
|
||||
}
|
||||
|
||||
type MemoryStorage struct {
|
||||
@@ -119,7 +120,7 @@ func (this *MemoryStorage) OpenReader(key string, useStale bool, isPartial bool)
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
if useStale || (item.ExpiresAt > utils.UnixTime()) {
|
||||
if useStale || (item.ExpiresAt > fasttime.Now().Unix()) {
|
||||
reader := NewMemoryReader(item)
|
||||
err := reader.Init()
|
||||
if err != nil {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package iplibrary
|
||||
|
||||
import "github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
)
|
||||
|
||||
type IPItemType = string
|
||||
|
||||
@@ -45,7 +47,7 @@ func (this *IPItem) containsIPv4(ip uint64) bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if this.ExpiredAt > 0 && this.ExpiredAt < utils.UnixTime() {
|
||||
if this.ExpiredAt > 0 && this.ExpiredAt < fasttime.Now().Unix() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -56,7 +58,7 @@ func (this *IPItem) containsIPv6(ip uint64) bool {
|
||||
if this.IPFrom != ip {
|
||||
return false
|
||||
}
|
||||
if this.ExpiredAt > 0 && this.ExpiredAt < utils.UnixTime() {
|
||||
if this.ExpiredAt > 0 && this.ExpiredAt < fasttime.Now().Unix() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -64,7 +66,7 @@ func (this *IPItem) containsIPv6(ip uint64) bool {
|
||||
|
||||
// 检查是否包所有IP
|
||||
func (this *IPItem) containsAll() bool {
|
||||
if this.ExpiredAt > 0 && this.ExpiredAt < utils.UnixTime() {
|
||||
if this.ExpiredAt > 0 && this.ExpiredAt < fasttime.Now().Unix() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
@@ -3,6 +3,7 @@ package iplibrary
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/expires"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
@@ -129,7 +130,7 @@ func (this *IPList) addItem(item *IPItem, sortable bool) {
|
||||
return
|
||||
}
|
||||
|
||||
if item.ExpiredAt > 0 && item.ExpiredAt < utils.UnixTime() {
|
||||
if item.ExpiredAt > 0 && item.ExpiredAt < fasttime.Now().Unix() {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/stats"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/waf"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
@@ -288,7 +289,7 @@ func (this *ClientConn) resetSYNFlood() {
|
||||
func (this *ClientConn) increaseSYNFlood(synFloodConfig *firewallconfigs.SYNFloodConfig) {
|
||||
var ip = this.RawIP()
|
||||
if len(ip) > 0 && !iplibrary.IsInWhiteList(ip) && (!synFloodConfig.IgnoreLocal || !utils.IsLocalIP(ip)) {
|
||||
var timestamp = utils.NextMinuteUnixTime()
|
||||
var timestamp = fasttime.Now().UnixNextMinute()
|
||||
var result = ttlcache.SharedCache.IncreaseInt64("SYN_FLOOD:"+ip, 1, timestamp, true)
|
||||
var minAttempts = synFloodConfig.MinAttempts
|
||||
if minAttempts < 5 {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package nodes
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@@ -15,7 +15,7 @@ type HTTPClient struct {
|
||||
func NewHTTPClient(rawClient *http.Client) *HTTPClient {
|
||||
return &HTTPClient{
|
||||
rawClient: rawClient,
|
||||
accessAt: utils.UnixTime(),
|
||||
accessAt: fasttime.Now().Unix(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func (this *HTTPClient) RawClient() *http.Client {
|
||||
|
||||
// UpdateAccessTime 更新访问时间
|
||||
func (this *HTTPClient) UpdateAccessTime() {
|
||||
this.accessAt = utils.UnixTime()
|
||||
this.accessAt = fasttime.Now().Unix()
|
||||
}
|
||||
|
||||
// AccessTime 获取访问时间
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/compressions"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
rangeutils "github.com/TeaOSLab/EdgeNode/internal/utils/ranges"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"io"
|
||||
@@ -328,7 +329,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) {
|
||||
}
|
||||
|
||||
// 设置cache.age变量
|
||||
var age = strconv.FormatInt(utils.UnixTime()-reader.LastModified(), 10)
|
||||
var age = strconv.FormatInt(fasttime.Now().Unix()-reader.LastModified(), 10)
|
||||
this.varMapping["cache.age"] = age
|
||||
|
||||
if addStatusHeader {
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/ranges"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"io"
|
||||
@@ -184,7 +184,7 @@ var httpRequestTimestamp int64
|
||||
var httpRequestId int32 = 1_000_000
|
||||
|
||||
func httpRequestNextId() string {
|
||||
unixTime, unixTimeString := utils.UnixTimeMilliString()
|
||||
unixTime, unixTimeString := fasttime.Now().UnixMilliString()
|
||||
if unixTime > httpRequestTimestamp {
|
||||
atomic.StoreInt32(&httpRequestId, 1_000_000)
|
||||
httpRequestTimestamp = unixTime
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/readers"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/writers"
|
||||
_ "github.com/biessek/golang-ico"
|
||||
@@ -299,7 +300,7 @@ func (this *HTTPWriter) PrepareCache(resp *http.Response, size int64) {
|
||||
}
|
||||
}
|
||||
|
||||
var expiresAt = utils.UnixTime() + life
|
||||
var expiresAt = fasttime.Now().Unix() + life
|
||||
|
||||
if this.req.isLnRequest {
|
||||
// 返回上级节点过期时间
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
@@ -185,9 +185,10 @@ func (this *BandwidthStatManager) AddBandwidth(userId int64, serverId int64, pee
|
||||
return
|
||||
}
|
||||
|
||||
var timestamp = utils.UnixTime() / bandwidthTimestampDelim * bandwidthTimestampDelim // 将时间戳均分成N等份
|
||||
var day = utils.Ymd()
|
||||
var timeAt = utils.Round5Hi()
|
||||
var now = fasttime.Now()
|
||||
var timestamp = now.Unix() / bandwidthTimestampDelim * bandwidthTimestampDelim // 将时间戳均分成N等份
|
||||
var day = now.Ymd()
|
||||
var timeAt = now.Round5Hi()
|
||||
var key = types.String(serverId) + "@" + day + "@" + timeAt
|
||||
|
||||
// 增加TCP Header尺寸,这里默认MTU为1500,且默认为IPv4
|
||||
@@ -230,8 +231,9 @@ func (this *BandwidthStatManager) AddBandwidth(userId int64, serverId int64, pee
|
||||
|
||||
// AddTraffic 添加请求数据
|
||||
func (this *BandwidthStatManager) AddTraffic(serverId int64, cachedBytes int64, countRequests int64, countCachedRequests int64, countAttacks int64, attackBytes int64) {
|
||||
var day = utils.Ymd()
|
||||
var timeAt = utils.Round5Hi()
|
||||
var now = fasttime.Now()
|
||||
var day = now.Ymd()
|
||||
var timeAt = now.Round5Hi()
|
||||
var key = types.String(serverId) + "@" + day + "@" + timeAt
|
||||
this.locker.Lock()
|
||||
// 只有有记录了才会添加
|
||||
@@ -292,7 +294,7 @@ func (this *BandwidthStatManager) recover() {
|
||||
if err == nil && len(m) > 0 {
|
||||
var lastTime = ""
|
||||
for _, stat := range m {
|
||||
if stat.Day != utils.Ymd() {
|
||||
if stat.Day != fasttime.Now().Ymd() {
|
||||
continue
|
||||
}
|
||||
if len(lastTime) == 0 || stat.TimeAt > lastTime {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/monitor"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
@@ -122,7 +122,7 @@ func (this *TrafficStatManager) Add(userId int64, serverId int64, domain string,
|
||||
|
||||
this.totalRequests++
|
||||
|
||||
var timestamp = utils.FloorUnixTime(300)
|
||||
var timestamp = fasttime.Now().UnixFloor(300)
|
||||
var key = strconv.FormatInt(timestamp, 10) + strconv.FormatInt(serverId, 10)
|
||||
this.locker.Lock()
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package ttlcache
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -74,7 +75,7 @@ func (this *Cache) Write(key string, value interface{}, expiredAt int64) (ok boo
|
||||
return
|
||||
}
|
||||
|
||||
var currentTimestamp = utils.UnixTime()
|
||||
var currentTimestamp = fasttime.Now().Unix()
|
||||
if expiredAt <= currentTimestamp {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package ttlcache
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
@@ -195,7 +195,7 @@ func BenchmarkCache_Add(b *testing.B) {
|
||||
|
||||
var cache = NewCache()
|
||||
for i := 0; i < b.N; i++ {
|
||||
cache.Write(strconv.Itoa(i), i, utils.UnixTime()+int64(i%1024))
|
||||
cache.Write(strconv.Itoa(i), i, fasttime.Now().Unix()+int64(i%1024))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,7 +207,7 @@ func BenchmarkCache_Add_Parallel(b *testing.B) {
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
var j = atomic.AddInt64(&i, 1)
|
||||
cache.Write(types.String(j), j, utils.UnixTime()+i%1024)
|
||||
cache.Write(types.String(j), j, fasttime.Now().Unix()+i%1024)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package ttlcache
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/expires"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -75,7 +75,7 @@ func (this *Piece) Delete(key uint64) {
|
||||
func (this *Piece) Read(key uint64) (item *Item) {
|
||||
this.locker.RLock()
|
||||
item = this.m[key]
|
||||
if item != nil && item.expiredAt < utils.UnixTime() {
|
||||
if item != nil && item.expiredAt < fasttime.Now().Unix() {
|
||||
item = nil
|
||||
}
|
||||
this.locker.RUnlock()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package expires
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
@@ -197,7 +197,7 @@ func BenchmarkList_GC(b *testing.B) {
|
||||
for m := 0; m < 1_000; m++ {
|
||||
var list = NewList()
|
||||
for j := 0; j < 10_000; j++ {
|
||||
list.Add(uint64(j), utils.UnixTime()+100)
|
||||
list.Add(uint64(j), fasttime.Now().Unix()+100)
|
||||
}
|
||||
lists = append(lists, list)
|
||||
}
|
||||
|
||||
93
internal/utils/fasttime/time_fast.go
Normal file
93
internal/utils/fasttime/time_fast.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package fasttime
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"time"
|
||||
)
|
||||
|
||||
var sharedFastTime = NewFastTime()
|
||||
|
||||
func init() {
|
||||
if !teaconst.IsMain {
|
||||
return
|
||||
}
|
||||
|
||||
var ticker = time.NewTicker(200 * time.Millisecond)
|
||||
goman.New(func() {
|
||||
for range ticker.C {
|
||||
sharedFastTime = NewFastTime()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func Now() *FastTime {
|
||||
return sharedFastTime
|
||||
}
|
||||
|
||||
type FastTime struct {
|
||||
rawTime time.Time
|
||||
unixTime int64
|
||||
unixTimeMilli int64
|
||||
unixTimeMilliString string
|
||||
ymd string
|
||||
round5Hi string
|
||||
}
|
||||
|
||||
func NewFastTime() *FastTime {
|
||||
var rawTime = time.Now()
|
||||
|
||||
return &FastTime{
|
||||
rawTime: rawTime,
|
||||
unixTime: rawTime.Unix(),
|
||||
unixTimeMilli: rawTime.UnixMilli(),
|
||||
unixTimeMilliString: types.String(rawTime.UnixMilli()),
|
||||
ymd: timeutil.Format("Ymd", rawTime),
|
||||
round5Hi: timeutil.FormatTime("Hi", rawTime.Unix()/300*300),
|
||||
}
|
||||
}
|
||||
|
||||
// Unix 最快获取时间戳的方式,通常用在不需要特别精确时间戳的场景
|
||||
func (this *FastTime) Unix() int64 {
|
||||
return this.unixTime
|
||||
}
|
||||
|
||||
// UnixFloor 取整
|
||||
func (this *FastTime) UnixFloor(seconds int) int64 {
|
||||
return this.unixTime / int64(seconds) * int64(seconds)
|
||||
}
|
||||
|
||||
// UnixCell 取整并加1
|
||||
func (this *FastTime) UnixCell(seconds int) int64 {
|
||||
return this.unixTime/int64(seconds)*int64(seconds) + int64(seconds)
|
||||
}
|
||||
|
||||
// UnixNextMinute 获取下一分钟开始的时间戳
|
||||
func (this *FastTime) UnixNextMinute() int64 {
|
||||
return this.UnixCell(60)
|
||||
}
|
||||
|
||||
// UnixMilli 获取时间戳,精确到毫秒
|
||||
func (this *FastTime) UnixMilli() int64 {
|
||||
return this.unixTimeMilli
|
||||
}
|
||||
|
||||
func (this *FastTime) UnixMilliString() (int64, string) {
|
||||
return this.unixTimeMilli, this.unixTimeMilliString
|
||||
}
|
||||
|
||||
func (this *FastTime) Ymd() string {
|
||||
return this.ymd
|
||||
}
|
||||
|
||||
func (this *FastTime) Round5Hi() string {
|
||||
return this.round5Hi
|
||||
}
|
||||
|
||||
func (this *FastTime) Format(layout string) string {
|
||||
return timeutil.Format(layout, this.rawTime)
|
||||
}
|
||||
57
internal/utils/fasttime/time_fast_test.go
Normal file
57
internal/utils/fasttime/time_fast_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package fasttime_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFastTime_Unix(t *testing.T) {
|
||||
for i := 0; i < 5; i++ {
|
||||
var now = fasttime.Now()
|
||||
t.Log(now.Unix(), now.UnixMilli(), "real:", time.Now().Unix())
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFastTime_UnixMilli(t *testing.T) {
|
||||
t.Log(fasttime.Now().UnixMilliString())
|
||||
}
|
||||
|
||||
func TestFastTime_UnixFloor(t *testing.T) {
|
||||
var now = fasttime.Now()
|
||||
|
||||
var timestamp = time.Now().Unix()
|
||||
t.Log("floor 60:", timestamp, now.UnixFloor(60), timeutil.FormatTime("Y-m-d H:i:s", now.UnixFloor(60)))
|
||||
t.Log("ceil 60:", timestamp, now.UnixCell(60), timeutil.FormatTime("Y-m-d H:i:s", now.UnixCell(60)))
|
||||
t.Log("floor 300:", timestamp, now.UnixFloor(300), timeutil.FormatTime("Y-m-d H:i:s", now.UnixFloor(300)))
|
||||
t.Log("next minute:", now.UnixNextMinute(), timeutil.FormatTime("Y-m-d H:i:s", now.UnixNextMinute()))
|
||||
t.Log("day:", now.Ymd())
|
||||
t.Log("round 5 minute:", now.Round5Hi())
|
||||
}
|
||||
|
||||
func TestFastTime_Format(t *testing.T) {
|
||||
var now = fasttime.Now()
|
||||
t.Log(now.Format("Y-m-d H:i:s"))
|
||||
}
|
||||
|
||||
func BenchmarkNewFastTime(b *testing.B) {
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
var now = fasttime.Now()
|
||||
_ = now.Ymd()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkNewFastTime_Raw(b *testing.B) {
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
var now = time.Now()
|
||||
_ = timeutil.Format("Ymd", now)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,65 +1,9 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"time"
|
||||
)
|
||||
|
||||
var unixTime = time.Now().Unix()
|
||||
var unixTimeMilli = time.Now().UnixMilli()
|
||||
var unixTimeMilliString = types.String(unixTimeMilli)
|
||||
var ymd = timeutil.Format("Ymd")
|
||||
var round5Hi = timeutil.FormatTime("Hi", time.Now().Unix()/300*300)
|
||||
|
||||
func init() {
|
||||
if !teaconst.IsMain {
|
||||
return
|
||||
}
|
||||
|
||||
var ticker = time.NewTicker(200 * time.Millisecond)
|
||||
goman.New(func() {
|
||||
for range ticker.C {
|
||||
unixTime = time.Now().Unix()
|
||||
unixTimeMilli = time.Now().UnixMilli()
|
||||
unixTimeMilliString = types.String(unixTimeMilli)
|
||||
ymd = timeutil.Format("Ymd")
|
||||
round5Hi = timeutil.FormatTime("Hi", time.Now().Unix()/300*300)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// UnixTime 最快获取时间戳的方式,通常用在不需要特别精确时间戳的场景
|
||||
func UnixTime() int64 {
|
||||
return unixTime
|
||||
}
|
||||
|
||||
// FloorUnixTime 取整
|
||||
func FloorUnixTime(seconds int) int64 {
|
||||
return UnixTime() / int64(seconds) * int64(seconds)
|
||||
}
|
||||
|
||||
// CeilUnixTime 取整并加1
|
||||
func CeilUnixTime(seconds int) int64 {
|
||||
return UnixTime()/int64(seconds)*int64(seconds) + int64(seconds)
|
||||
}
|
||||
|
||||
// NextMinuteUnixTime 获取下一分钟开始的时间戳
|
||||
func NextMinuteUnixTime() int64 {
|
||||
return CeilUnixTime(60)
|
||||
}
|
||||
|
||||
// UnixTimeMilli 获取时间戳,精确到毫秒
|
||||
func UnixTimeMilli() int64 {
|
||||
return unixTimeMilli
|
||||
}
|
||||
|
||||
func UnixTimeMilliString() (int64, string) {
|
||||
return unixTimeMilli, unixTimeMilliString
|
||||
}
|
||||
|
||||
// GMTUnixTime 计算GMT时间戳
|
||||
func GMTUnixTime(timestamp int64) int64 {
|
||||
_, offset := time.Now().Zone()
|
||||
@@ -71,13 +15,3 @@ func GMTTime(t time.Time) time.Time {
|
||||
_, offset := time.Now().Zone()
|
||||
return t.Add(-time.Duration(offset) * time.Second)
|
||||
}
|
||||
|
||||
// Ymd 读取YYYYMMDD
|
||||
func Ymd() string {
|
||||
return ymd
|
||||
}
|
||||
|
||||
// Round5Hi 读取5分钟间隔时间
|
||||
func Round5Hi() string {
|
||||
return round5Hi
|
||||
}
|
||||
|
||||
@@ -2,18 +2,10 @@ package utils_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestUnixTime(t *testing.T) {
|
||||
for i := 0; i < 5; i++ {
|
||||
t.Log(utils.UnixTime(), "real:", time.Now().Unix())
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGMTUnixTime(t *testing.T) {
|
||||
t.Log(utils.GMTUnixTime(time.Now().Unix()))
|
||||
}
|
||||
@@ -21,19 +13,3 @@ func TestGMTUnixTime(t *testing.T) {
|
||||
func TestGMTTime(t *testing.T) {
|
||||
t.Log(utils.GMTTime(time.Now()))
|
||||
}
|
||||
|
||||
func TestFloorUnixTime(t *testing.T) {
|
||||
var timestamp = time.Now().Unix()
|
||||
t.Log("floor 60:", timestamp, utils.FloorUnixTime(60), timeutil.FormatTime("Y-m-d H:i:s", utils.FloorUnixTime(60)))
|
||||
t.Log("ceil 60:", timestamp, utils.CeilUnixTime(60), timeutil.FormatTime("Y-m-d H:i:s", utils.CeilUnixTime(60)))
|
||||
t.Log("floor 300:", timestamp, utils.FloorUnixTime(300), timeutil.FormatTime("Y-m-d H:i:s", utils.FloorUnixTime(300)))
|
||||
t.Log("next minute:", utils.NextMinuteUnixTime())
|
||||
}
|
||||
|
||||
func TestYmd(t *testing.T) {
|
||||
t.Log(utils.Ymd())
|
||||
}
|
||||
|
||||
func TestRound5Hi(t *testing.T) {
|
||||
t.Log(utils.Round5Hi())
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/conns"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/firewalls"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/expires"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -105,7 +105,7 @@ func (this *IPList) RecordIP(ipType string,
|
||||
}
|
||||
|
||||
// 加入队列等待上传
|
||||
if this.lastIP != ip || utils.UnixTime()-this.lastTime > 3 /** 3秒外才允许重复添加 **/ {
|
||||
if this.lastIP != ip || fasttime.Now().Unix()-this.lastTime > 3 /** 3秒外才允许重复添加 **/ {
|
||||
select {
|
||||
case recordIPTaskChan <- &recordIPTask{
|
||||
ip: ip,
|
||||
@@ -120,7 +120,7 @@ func (this *IPList) RecordIP(ipType string,
|
||||
reason: reason,
|
||||
}:
|
||||
this.lastIP = ip
|
||||
this.lastTime = utils.UnixTime()
|
||||
this.lastTime = fasttime.Now().Unix()
|
||||
default:
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user