mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 16:00:25 +08:00 
			
		
		
		
	优化代码
This commit is contained in:
		
							
								
								
									
										10
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								go.mod
									
									
									
									
									
								
							@@ -2,9 +2,7 @@ module github.com/TeaOSLab/EdgeNode
 | 
			
		||||
 | 
			
		||||
go 1.18
 | 
			
		||||
 | 
			
		||||
replace (
 | 
			
		||||
	github.com/TeaOSLab/EdgeCommon => ../EdgeCommon
 | 
			
		||||
)
 | 
			
		||||
replace github.com/TeaOSLab/EdgeCommon => ../EdgeCommon
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/TeaOSLab/EdgeCommon v0.0.0-00010101000000-000000000000
 | 
			
		||||
@@ -16,7 +14,7 @@ require (
 | 
			
		||||
	github.com/go-redis/redis/v8 v8.11.5
 | 
			
		||||
	github.com/golang/protobuf v1.5.2
 | 
			
		||||
	github.com/google/nftables v0.0.0-20220407195405-950e408d48c6
 | 
			
		||||
	github.com/iwind/TeaGo v0.0.0-20220304043459-0dd944a5b475
 | 
			
		||||
	github.com/iwind/TeaGo v0.0.0-20220807030847-31de8e1cbe55
 | 
			
		||||
	github.com/iwind/gofcgi v0.0.0-20210528023741-a92711d45f11
 | 
			
		||||
	github.com/iwind/gosock v0.0.0-20211103081026-ee4652210ca4
 | 
			
		||||
	github.com/iwind/gowebp v0.0.0-20211029040624-7331ecc78ed8
 | 
			
		||||
@@ -32,7 +30,8 @@ require (
 | 
			
		||||
	golang.org/x/sys v0.0.0-20220412211240-33da011f77ad
 | 
			
		||||
	golang.org/x/text v0.3.7
 | 
			
		||||
	google.golang.org/grpc v1.45.0
 | 
			
		||||
	gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
 | 
			
		||||
	gopkg.in/yaml.v3 v3.0.1
 | 
			
		||||
	rogchap.com/v8go v0.7.0
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
@@ -41,6 +40,7 @@ require (
 | 
			
		||||
	github.com/chai2010/webp v1.1.0 // indirect
 | 
			
		||||
	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
 | 
			
		||||
	github.com/go-ole/go-ole v1.2.6 // indirect
 | 
			
		||||
	github.com/go-sql-driver/mysql v1.5.0 // indirect
 | 
			
		||||
	github.com/google/go-cmp v0.5.7 // indirect
 | 
			
		||||
	github.com/josharian/native v0.0.0-20200817173448-b6b71def0850 // indirect
 | 
			
		||||
	github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										23
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								go.sum
									
									
									
									
									
								
							@@ -47,6 +47,10 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
 | 
			
		||||
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
 | 
			
		||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
 | 
			
		||||
github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k=
 | 
			
		||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
 | 
			
		||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
 | 
			
		||||
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
 | 
			
		||||
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
 | 
			
		||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
 | 
			
		||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
 | 
			
		||||
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
 | 
			
		||||
@@ -89,8 +93,10 @@ github.com/google/nftables v0.0.0-20220407195405-950e408d48c6/go.mod h1:0F8on3JW
 | 
			
		||||
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 | 
			
		||||
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
 | 
			
		||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
 | 
			
		||||
github.com/iwind/TeaGo v0.0.0-20220304043459-0dd944a5b475 h1:EseyfFaQOjWanGiby9KMw7PjDBMg/95tLDgIw/ns0Cw=
 | 
			
		||||
github.com/iwind/TeaGo v0.0.0-20220304043459-0dd944a5b475/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
 | 
			
		||||
github.com/iwind/TeaGo v0.0.0-20220807023459-448081424640 h1:nBVQzDI4mrQS+Egg+Li6BGiTToBsv+XTck+BItgI52k=
 | 
			
		||||
github.com/iwind/TeaGo v0.0.0-20220807023459-448081424640/go.mod h1:+K2l6Num4Evl0jH7TYlZJ1oFJX8sA8YUC31Pb+I1mJk=
 | 
			
		||||
github.com/iwind/TeaGo v0.0.0-20220807030847-31de8e1cbe55 h1:shQNx0flJFBwKsGE7Hs3bI2bDz+YF0zl/4qE8B2KRiY=
 | 
			
		||||
github.com/iwind/TeaGo v0.0.0-20220807030847-31de8e1cbe55/go.mod h1:fi/Pq+/5m2HZoseM+39dMF57ANXRt6w4PkGu3NXPc5s=
 | 
			
		||||
github.com/iwind/gofcgi v0.0.0-20210528023741-a92711d45f11 h1:DaQjoWZhLNxjhIXedVg4/vFEtHkZhK4IjIwsWdyzBLg=
 | 
			
		||||
github.com/iwind/gofcgi v0.0.0-20210528023741-a92711d45f11/go.mod h1:JtbX20untAjUVjZs1ZBtq80f5rJWvwtQNRL6EnuYRnY=
 | 
			
		||||
github.com/iwind/gosock v0.0.0-20211103081026-ee4652210ca4 h1:VWGsCqTzObdlbf7UUE3oceIpcEKi4C/YBUszQXk118A=
 | 
			
		||||
@@ -109,11 +115,8 @@ github.com/jsimonetti/rtnetlink v0.0.0-20210212075122-66c871082f2b/go.mod h1:8w9
 | 
			
		||||
github.com/jsimonetti/rtnetlink v0.0.0-20210525051524-4cc836578190/go.mod h1:NmKSdU4VGSiv1bMsdqNALI4RSvvjtz65tTMCnD05qLo=
 | 
			
		||||
github.com/jsimonetti/rtnetlink v0.0.0-20211022192332-93da33804786 h1:N527AHMa793TP5z5GNAn/VLPzlc0ewzWdeP/25gDfgQ=
 | 
			
		||||
github.com/jsimonetti/rtnetlink v0.0.0-20211022192332-93da33804786/go.mod h1:v4hqbTdfQngbVSZJVWUhGE/lbTFf9jb+ygmNUDQMuOs=
 | 
			
		||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
 | 
			
		||||
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e h1:LvL4XsI70QxOGHed6yhQtAU34Kx3Qq2wwBzGFKY8zKk=
 | 
			
		||||
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e/go.mod h1:kLgvv7o6UM+0QSf0QjAse3wReFDsb9qbZJdfexWlrQw=
 | 
			
		||||
github.com/klauspost/compress v1.15.6 h1:6D9PcO8QWu0JyaQ2zUMmu16T1T+zjjEpP91guRsvDfY=
 | 
			
		||||
github.com/klauspost/compress v1.15.6/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
 | 
			
		||||
github.com/klauspost/compress v1.15.8 h1:JahtItbkWjf2jzm/T+qgMxkP9EMHsqEUA6vCMGmXvhA=
 | 
			
		||||
github.com/klauspost/compress v1.15.8/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
 | 
			
		||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 | 
			
		||||
@@ -150,8 +153,6 @@ github.com/mdlayher/socket v0.0.0-20211102153432-57e3fa563ecb h1:2dC7L10LmTqlyMV
 | 
			
		||||
github.com/mdlayher/socket v0.0.0-20211102153432-57e3fa563ecb/go.mod h1:nFZ1EtZYK8Gi/k6QNu7z7CgO20i/4ExeQswwWuPmG/g=
 | 
			
		||||
github.com/miekg/dns v1.1.43 h1:JKfpVSCB84vrAmHzyrsxB5NAr5kLoMXZArPSw7Qlgyg=
 | 
			
		||||
github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
 | 
			
		||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 | 
			
		||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
 | 
			
		||||
github.com/mssola/user_agent v0.5.3 h1:lBRPML9mdFuIZgI2cmlQ+atbpJdLdeVl2IDodjBR578=
 | 
			
		||||
github.com/mssola/user_agent v0.5.3/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw=
 | 
			
		||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
 | 
			
		||||
@@ -180,7 +181,6 @@ github.com/shirou/gopsutil/v3 v3.22.2/go.mod h1:WapW1AOOPlHyXr+yOyw3uYx36enocrtS
 | 
			
		||||
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
 | 
			
		||||
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
 | 
			
		||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 | 
			
		||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 | 
			
		||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
 | 
			
		||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 | 
			
		||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
 | 
			
		||||
@@ -268,6 +268,7 @@ golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7w
 | 
			
		||||
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
@@ -291,6 +292,7 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
 | 
			
		||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
@@ -373,10 +375,13 @@ gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 | 
			
		||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 | 
			
		||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
			
		||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
 | 
			
		||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
 | 
			
		||||
honnef.co/go/tools v0.2.1/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY=
 | 
			
		||||
honnef.co/go/tools v0.2.2 h1:MNh1AVMyVX23VUHE2O27jm6lNj3vjO5DexS4A1xvnzk=
 | 
			
		||||
honnef.co/go/tools v0.2.2/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY=
 | 
			
		||||
rogchap.com/v8go v0.7.0 h1:kgjbiO4zE5itA962ze6Hqmbs4HgZbGzmueCXsZtremg=
 | 
			
		||||
rogchap.com/v8go v0.7.0/go.mod h1:MxgP3pL2MW4dpme/72QRs8sgNMmM0pRc8DPhcuLWPAs=
 | 
			
		||||
 
 | 
			
		||||
@@ -180,14 +180,14 @@ var httpRequestTimestamp int64
 | 
			
		||||
var httpRequestId int32 = 1_000_000
 | 
			
		||||
 | 
			
		||||
func httpRequestNextId() string {
 | 
			
		||||
	var unixTime = utils.UnixTimeMilli()
 | 
			
		||||
	unixTime, unixTimeString := utils.UnixTimeMilliString()
 | 
			
		||||
	if unixTime > httpRequestTimestamp {
 | 
			
		||||
		atomic.StoreInt32(&httpRequestId, 1_000_000)
 | 
			
		||||
		httpRequestTimestamp = unixTime
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// timestamp + requestId + nodeId
 | 
			
		||||
	return strconv.FormatInt(unixTime, 10) + teaconst.NodeIdString + strconv.Itoa(int(atomic.AddInt32(&httpRequestId, 1)))
 | 
			
		||||
	return unixTimeString + teaconst.NodeIdString + strconv.Itoa(int(atomic.AddInt32(&httpRequestId, 1)))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 检查是否可以接受某个编码
 | 
			
		||||
 
 | 
			
		||||
@@ -106,7 +106,7 @@ func (this *TrafficStatManager) Add(serverId int64, domain string, bytes int64,
 | 
			
		||||
	this.totalRequests++
 | 
			
		||||
 | 
			
		||||
	var timestamp = utils.FloorUnixTime(300)
 | 
			
		||||
	key := strconv.FormatInt(timestamp, 10) + strconv.FormatInt(serverId, 10)
 | 
			
		||||
	var key = strconv.FormatInt(timestamp, 10) + strconv.FormatInt(serverId, 10)
 | 
			
		||||
	this.locker.Lock()
 | 
			
		||||
 | 
			
		||||
	// 总的流量
 | 
			
		||||
 
 | 
			
		||||
@@ -1,108 +1,46 @@
 | 
			
		||||
package utils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeNode/internal/goman"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"time"
 | 
			
		||||
	"sync"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var BytePool1k = NewBytePool(20480, 1024)
 | 
			
		||||
var BytePool4k = NewBytePool(20480, 4*1024)
 | 
			
		||||
var BytePool16k = NewBytePool(40960, 16*1024)
 | 
			
		||||
var BytePool32k = NewBytePool(20480, 32*1024)
 | 
			
		||||
var BytePool1k = NewBytePool(1024)
 | 
			
		||||
var BytePool4k = NewBytePool(4 * 1024)
 | 
			
		||||
var BytePool16k = NewBytePool(16 * 1024)
 | 
			
		||||
var BytePool32k = NewBytePool(32 * 1024)
 | 
			
		||||
 | 
			
		||||
// BytePool pool for get byte slice
 | 
			
		||||
type BytePool struct {
 | 
			
		||||
	c       chan []byte
 | 
			
		||||
	maxSize int
 | 
			
		||||
	length  int
 | 
			
		||||
	hasNew  bool
 | 
			
		||||
	rawPool *sync.Pool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewBytePool 创建新对象
 | 
			
		||||
func NewBytePool(maxSize, length int) *BytePool {
 | 
			
		||||
	if maxSize <= 0 {
 | 
			
		||||
		maxSize = 1024
 | 
			
		||||
func NewBytePool(length int) *BytePool {
 | 
			
		||||
	if length < 0 {
 | 
			
		||||
		length = 1024
 | 
			
		||||
	}
 | 
			
		||||
	if length <= 0 {
 | 
			
		||||
		length = 128
 | 
			
		||||
	}
 | 
			
		||||
	var pool = &BytePool{
 | 
			
		||||
		c:       make(chan []byte, maxSize),
 | 
			
		||||
		maxSize: maxSize,
 | 
			
		||||
	return &BytePool{
 | 
			
		||||
		length: length,
 | 
			
		||||
		rawPool: &sync.Pool{
 | 
			
		||||
			New: func() any {
 | 
			
		||||
				return make([]byte, length)
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pool.init()
 | 
			
		||||
 | 
			
		||||
	return pool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 初始化
 | 
			
		||||
func (this *BytePool) init() {
 | 
			
		||||
	var ticker = time.NewTicker(2 * time.Minute)
 | 
			
		||||
	if Tea.IsTesting() {
 | 
			
		||||
		ticker = time.NewTicker(5 * time.Second)
 | 
			
		||||
	}
 | 
			
		||||
	goman.New(func() {
 | 
			
		||||
		for range ticker.C {
 | 
			
		||||
			if this.hasNew {
 | 
			
		||||
				this.hasNew = false
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			this.Purge()
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Get 获取一个新的byte slice
 | 
			
		||||
func (this *BytePool) Get() (b []byte) {
 | 
			
		||||
	select {
 | 
			
		||||
	case b = <-this.c:
 | 
			
		||||
	default:
 | 
			
		||||
		b = make([]byte, this.length)
 | 
			
		||||
		this.hasNew = true
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
func (this *BytePool) Get() []byte {
 | 
			
		||||
	return this.rawPool.Get().([]byte)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Put 放回一个使用过的byte slice
 | 
			
		||||
func (this *BytePool) Put(b []byte) {
 | 
			
		||||
	if cap(b) != this.length {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	select {
 | 
			
		||||
	case this.c <- b:
 | 
			
		||||
	default:
 | 
			
		||||
		// 已达最大容量,则抛弃
 | 
			
		||||
	}
 | 
			
		||||
	this.rawPool.Put(b)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Length 单个字节slice长度
 | 
			
		||||
func (this *BytePool) Length() int {
 | 
			
		||||
	return this.length
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Size 当前的数量
 | 
			
		||||
func (this *BytePool) Size() int {
 | 
			
		||||
	return len(this.c)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Purge 清理
 | 
			
		||||
func (this *BytePool) Purge() {
 | 
			
		||||
	// 1%
 | 
			
		||||
	var count = len(this.c) / 100
 | 
			
		||||
	if count == 0 {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
Loop:
 | 
			
		||||
	for i := 0; i < count; i++ {
 | 
			
		||||
		select {
 | 
			
		||||
		case <-this.c:
 | 
			
		||||
		default:
 | 
			
		||||
			break Loop
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,37 +1,16 @@
 | 
			
		||||
package utils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/iwind/TeaGo/assert"
 | 
			
		||||
	"runtime"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestNewBytePool(t *testing.T) {
 | 
			
		||||
	a := assert.NewAssertion(t)
 | 
			
		||||
 | 
			
		||||
	pool := NewBytePool(5, 8)
 | 
			
		||||
	buf := pool.Get()
 | 
			
		||||
	a.IsTrue(len(buf) == 8)
 | 
			
		||||
	a.IsTrue(len(pool.c) == 0)
 | 
			
		||||
 | 
			
		||||
	pool.Put(buf)
 | 
			
		||||
	a.IsTrue(len(pool.c) == 1)
 | 
			
		||||
 | 
			
		||||
	pool.Get()
 | 
			
		||||
	a.IsTrue(len(pool.c) == 0)
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < 10; i++ {
 | 
			
		||||
		pool.Put(buf)
 | 
			
		||||
	}
 | 
			
		||||
	t.Log(len(pool.c))
 | 
			
		||||
	a.IsTrue(len(pool.c) == 5)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestBytePool_Memory(t *testing.T) {
 | 
			
		||||
	var stat1 = &runtime.MemStats{}
 | 
			
		||||
	runtime.ReadMemStats(stat1)
 | 
			
		||||
 | 
			
		||||
	var pool = NewBytePool(20480, 32*1024)
 | 
			
		||||
	var pool = NewBytePool(32 * 1024)
 | 
			
		||||
	for i := 0; i < 20480; i++ {
 | 
			
		||||
		pool.Put(make([]byte, 32*1024))
 | 
			
		||||
	}
 | 
			
		||||
@@ -44,18 +23,50 @@ func TestBytePool_Memory(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	var stat2 = &runtime.MemStats{}
 | 
			
		||||
	runtime.ReadMemStats(stat2)
 | 
			
		||||
	t.Log((stat2.HeapInuse-stat1.HeapInuse)/1024/1024, "MB,", pool.Size(), "slices")
 | 
			
		||||
	t.Log((stat2.HeapInuse-stat1.HeapInuse)/1024/1024, "MB,")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func BenchmarkBytePool_Get(b *testing.B) {
 | 
			
		||||
	runtime.GOMAXPROCS(1)
 | 
			
		||||
 | 
			
		||||
	pool := NewBytePool(1024, 1)
 | 
			
		||||
	var pool = NewBytePool(1)
 | 
			
		||||
	b.ResetTimer()
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < b.N; i++ {
 | 
			
		||||
		buf := pool.Get()
 | 
			
		||||
		var buf = pool.Get()
 | 
			
		||||
		_ = buf
 | 
			
		||||
		pool.Put(buf)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	b.Log(pool.Size())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func BenchmarkBytePool_Get_Parallel(b *testing.B) {
 | 
			
		||||
	runtime.GOMAXPROCS(1)
 | 
			
		||||
 | 
			
		||||
	var pool = NewBytePool(1024)
 | 
			
		||||
	b.ResetTimer()
 | 
			
		||||
 | 
			
		||||
	b.RunParallel(func(pb *testing.PB) {
 | 
			
		||||
		for pb.Next() {
 | 
			
		||||
			var buf = pool.Get()
 | 
			
		||||
			pool.Put(buf)
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func BenchmarkBytePool_Get_Sync(b *testing.B) {
 | 
			
		||||
	runtime.GOMAXPROCS(1)
 | 
			
		||||
 | 
			
		||||
	var pool = &sync.Pool{
 | 
			
		||||
		New: func() any {
 | 
			
		||||
			return make([]byte, 1024)
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	b.ResetTimer()
 | 
			
		||||
 | 
			
		||||
	b.RunParallel(func(pb *testing.PB) {
 | 
			
		||||
		for pb.Next() {
 | 
			
		||||
			var buf = pool.Get()
 | 
			
		||||
			pool.Put(buf)
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,11 +2,13 @@ package utils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeNode/internal/goman"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var unixTime = time.Now().Unix()
 | 
			
		||||
var unixTimeMilli = time.Now().UnixMilli()
 | 
			
		||||
var unixTimeMilliString = types.String(unixTimeMilli)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	var ticker = time.NewTicker(200 * time.Millisecond)
 | 
			
		||||
@@ -14,6 +16,7 @@ func init() {
 | 
			
		||||
		for range ticker.C {
 | 
			
		||||
			unixTime = time.Now().Unix()
 | 
			
		||||
			unixTimeMilli = time.Now().UnixMilli()
 | 
			
		||||
			unixTimeMilliString = types.String(unixTimeMilli)
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
@@ -43,6 +46,10 @@ func UnixTimeMilli() int64 {
 | 
			
		||||
	return unixTimeMilli
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func UnixTimeMilliString() (int64, string) {
 | 
			
		||||
	return unixTimeMilli, unixTimeMilliString
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GMTUnixTime 计算GMT时间戳
 | 
			
		||||
func GMTUnixTime(timestamp int64) int64 {
 | 
			
		||||
	_, offset := time.Now().Zone()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user