升级Go版本为v1.18

This commit is contained in:
刘祥超
2022-07-07 09:21:18 +08:00
parent ce7dda8cf5
commit 987350f0b4
9 changed files with 67 additions and 54 deletions
+1 -1
View File
@@ -112,7 +112,7 @@ func (this *ClientConn) Write(b []byte) (n int, err error) {
// 统计当前服务带宽
if this.serverId > 0 {
if !this.isLO { // 环路不统计带宽,避免缓存预热等行为产生带宽
stats.SharedBandwidthStatManager.Add(this.serverId, int64(n))
stats.SharedBandwidthStatManager.Add(this.userId, this.serverId, int64(n))
}
}
}
+11
View File
@@ -8,6 +8,7 @@ type BaseClientConn struct {
rawConn net.Conn
isBound bool
userId int64
serverId int64
remoteAddr string
@@ -46,6 +47,16 @@ func (this *BaseClientConn) ServerId() int64 {
return this.serverId
}
// SetUserId 设置所属服务的用户ID
func (this *BaseClientConn) SetUserId(userId int64) {
this.userId = userId
}
// UserId 获取当前连接所属服务的用户ID
func (this *BaseClientConn) UserId() int64 {
return this.userId
}
// RawIP 原本IP
func (this *BaseClientConn) RawIP() string {
ip, _, _ := net.SplitHostPort(this.rawConn.RemoteAddr().String())
+6
View File
@@ -17,4 +17,10 @@ type ClientConnInterface interface {
// SetServerId 设置服务ID
SetServerId(serverId int64)
// SetUserId 设置所属服务的用户ID
SetUserId(userId int64)
// UserId 获取当前连接所属服务的用户ID
UserId() int64
}
+1
View File
@@ -185,6 +185,7 @@ func (this *HTTPListener) ServeHTTP(rawWriter http.ResponseWriter, rawReq *http.
clientConn, ok := requestConn.(ClientConnInterface)
if ok {
clientConn.SetServerId(server.Id)
clientConn.SetUserId(server.UserId)
}
}
}
+2
View File
@@ -75,6 +75,7 @@ func (this *TCPListener) handleConn(conn net.Conn) error {
clientConn, ok := conn.(ClientConnInterface)
if ok {
clientConn.SetServerId(server.Id)
clientConn.SetUserId(server.UserId)
} else {
tlsConn, ok := conn.(*tls.Conn)
if ok {
@@ -83,6 +84,7 @@ func (this *TCPListener) handleConn(conn net.Conn) error {
clientConn, ok = internalConn.(ClientConnInterface)
if ok {
clientConn.SetServerId(server.Id)
clientConn.SetUserId(server.UserId)
}
}
}
+6 -1
View File
@@ -28,6 +28,7 @@ func init() {
type BandwidthStat struct {
Day string
TimeAt string
UserId int64
ServerId int64
CurrentBytes int64
@@ -78,6 +79,7 @@ func (this *BandwidthStatManager) Loop() error {
if stat.Day < day || stat.TimeAt < currentTime {
pbStats = append(pbStats, &pb.ServerBandwidthStat{
Id: 0,
UserId: stat.UserId,
ServerId: stat.ServerId,
Day: stat.Day,
TimeAt: stat.TimeAt,
@@ -104,7 +106,7 @@ func (this *BandwidthStatManager) Loop() error {
}
// Add 添加带宽数据
func (this *BandwidthStatManager) Add(serverId int64, bytes int64) {
func (this *BandwidthStatManager) Add(userId int64, serverId int64, bytes int64) {
if serverId <= 0 || bytes == 0 {
return
}
@@ -118,6 +120,8 @@ func (this *BandwidthStatManager) Add(serverId int64, bytes int64) {
this.locker.Lock()
stat, ok := this.m[key]
if ok {
// 此刻如果发生用户ID(userId)的变化也忽略,等N分钟后有新记录后再换
if stat.CurrentTimestamp == timestamp {
stat.CurrentBytes += bytes
} else {
@@ -131,6 +135,7 @@ func (this *BandwidthStatManager) Add(serverId int64, bytes int64) {
this.m[key] = &BandwidthStat{
Day: day,
TimeAt: timeAt,
UserId: userId,
ServerId: serverId,
CurrentBytes: bytes,
MaxBytes: bytes,
+10 -10
View File
@@ -10,24 +10,24 @@ import (
func TestBandwidthStatManager_Add(t *testing.T) {
var manager = stats.NewBandwidthStatManager()
manager.Add(1, 10)
manager.Add(1, 10)
manager.Add(1, 10)
manager.Add(1, 1, 10)
manager.Add(1, 1, 10)
manager.Add(1, 1, 10)
time.Sleep(1 * time.Second)
manager.Add(1, 15)
manager.Add(1, 1, 15)
time.Sleep(1 * time.Second)
manager.Add(1, 25)
manager.Add(1, 75)
manager.Add(1, 1, 25)
manager.Add(1, 1, 75)
manager.Inspect()
}
func TestBandwidthStatManager_Loop(t *testing.T) {
var manager = stats.NewBandwidthStatManager()
manager.Add(1, 10)
manager.Add(1, 10)
manager.Add(1, 10)
manager.Add(1, 1, 10)
manager.Add(1, 1, 10)
manager.Add(1, 1, 10)
err := manager.Loop()
if err != nil {
t.Fatal(err)
}
}
}