mirror of https://github.com/bjdgyc/anylink.git
替换 go.uber.org/atomic
This commit is contained in:
parent
4a412fe0ee
commit
6f875001f2
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
"sort"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/pkg/utils"
|
"github.com/bjdgyc/anylink/pkg/utils"
|
||||||
|
@ -57,10 +56,10 @@ func OnlineSess() []Online {
|
||||||
TunName: v.CSess.IfName,
|
TunName: v.CSess.IfName,
|
||||||
Mtu: v.CSess.Mtu,
|
Mtu: v.CSess.Mtu,
|
||||||
Client: v.CSess.Client,
|
Client: v.CSess.Client,
|
||||||
BandwidthUp: utils.HumanByte(atomic.LoadUint32(&v.CSess.BandwidthUpPeriod)) + "/s",
|
BandwidthUp: utils.HumanByte(v.CSess.BandwidthUpPeriod.Load()) + "/s",
|
||||||
BandwidthDown: utils.HumanByte(atomic.LoadUint32(&v.CSess.BandwidthDownPeriod)) + "/s",
|
BandwidthDown: utils.HumanByte(v.CSess.BandwidthDownPeriod.Load()) + "/s",
|
||||||
BandwidthUpAll: utils.HumanByte(atomic.LoadUint64(&v.CSess.BandwidthUpAll)),
|
BandwidthUpAll: utils.HumanByte(v.CSess.BandwidthUpAll.Load()),
|
||||||
BandwidthDownAll: utils.HumanByte(atomic.LoadUint64(&v.CSess.BandwidthDownAll)),
|
BandwidthDownAll: utils.HumanByte(v.CSess.BandwidthDownAll.Load()),
|
||||||
LastLogin: v.LastLogin,
|
LastLogin: v.LastLogin,
|
||||||
}
|
}
|
||||||
datas = append(datas, val)
|
datas = append(datas, val)
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/bjdgyc/anylink/dbdata"
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
"github.com/bjdgyc/anylink/pkg/utils"
|
"github.com/bjdgyc/anylink/pkg/utils"
|
||||||
"github.com/ivpusic/grpool"
|
"github.com/ivpusic/grpool"
|
||||||
|
atomic2 "go.uber.org/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -39,12 +40,12 @@ type ConnSession struct {
|
||||||
CstpDpd int
|
CstpDpd int
|
||||||
Group *dbdata.Group
|
Group *dbdata.Group
|
||||||
Limit *LimitRater
|
Limit *LimitRater
|
||||||
BandwidthUp uint32 // 使用上行带宽 Byte
|
BandwidthUp atomic2.Uint32 // 使用上行带宽 Byte
|
||||||
BandwidthDown uint32 // 使用下行带宽 Byte
|
BandwidthDown atomic2.Uint32 // 使用下行带宽 Byte
|
||||||
BandwidthUpPeriod uint32 // 前一周期的总量
|
BandwidthUpPeriod atomic2.Uint32 // 前一周期的总量
|
||||||
BandwidthDownPeriod uint32
|
BandwidthDownPeriod atomic2.Uint32
|
||||||
BandwidthUpAll uint64 // 使用上行带宽总量
|
BandwidthUpAll atomic2.Uint64 // 使用上行带宽总量
|
||||||
BandwidthDownAll uint64 // 使用下行带宽总量
|
BandwidthDownAll atomic2.Uint64 // 使用下行带宽总量
|
||||||
closeOnce sync.Once
|
closeOnce sync.Once
|
||||||
CloseChan chan struct{}
|
CloseChan chan struct{}
|
||||||
PayloadIn chan *Payload
|
PayloadIn chan *Payload
|
||||||
|
@ -286,14 +287,14 @@ func (cs *ConnSession) ratePeriod() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 实时流量清零
|
// 实时流量清零
|
||||||
rtUp := atomic.SwapUint32(&cs.BandwidthUp, 0)
|
rtUp := cs.BandwidthUp.Swap(0)
|
||||||
rtDown := atomic.SwapUint32(&cs.BandwidthDown, 0)
|
rtDown := cs.BandwidthDown.Swap(0)
|
||||||
// 设置上一周期每秒的流量
|
// 设置上一周期每秒的流量
|
||||||
atomic.SwapUint32(&cs.BandwidthUpPeriod, rtUp/BandwidthPeriodSec)
|
cs.BandwidthUpPeriod.Swap(rtUp / BandwidthPeriodSec)
|
||||||
atomic.SwapUint32(&cs.BandwidthDownPeriod, rtDown/BandwidthPeriodSec)
|
cs.BandwidthDownPeriod.Swap(rtDown / BandwidthPeriodSec)
|
||||||
// 累加所有流量
|
// 累加所有流量
|
||||||
atomic.AddUint64(&cs.BandwidthUpAll, uint64(rtUp))
|
cs.BandwidthUpAll.Add(uint64(rtUp))
|
||||||
atomic.AddUint64(&cs.BandwidthDownAll, uint64(rtDown))
|
cs.BandwidthDownAll.Add(uint64(rtDown))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -323,11 +324,11 @@ func (cs *ConnSession) SetIfName(name string) {
|
||||||
|
|
||||||
func (cs *ConnSession) RateLimit(byt int, isUp bool) error {
|
func (cs *ConnSession) RateLimit(byt int, isUp bool) error {
|
||||||
if isUp {
|
if isUp {
|
||||||
atomic.AddUint32(&cs.BandwidthUp, uint32(byt))
|
cs.BandwidthUp.Add(uint32(byt))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// 只对下行速率限制
|
// 只对下行速率限制
|
||||||
atomic.AddUint32(&cs.BandwidthDown, uint32(byt))
|
cs.BandwidthDown.Add(uint32(byt))
|
||||||
if cs.Limit == nil {
|
if cs.Limit == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,9 +30,9 @@ func TestConnSession(t *testing.T) {
|
||||||
|
|
||||||
err := cSess.RateLimit(100, true)
|
err := cSess.RateLimit(100, true)
|
||||||
ast.Nil(err)
|
ast.Nil(err)
|
||||||
ast.Equal(cSess.BandwidthUp, uint32(100))
|
ast.Equal(cSess.BandwidthUp.Load(), uint32(100))
|
||||||
err = cSess.RateLimit(200, false)
|
err = cSess.RateLimit(200, false)
|
||||||
ast.Nil(err)
|
ast.Nil(err)
|
||||||
ast.Equal(cSess.BandwidthDown, uint32(200))
|
ast.Equal(cSess.BandwidthDown.Load(), uint32(200))
|
||||||
cSess.Close()
|
cSess.Close()
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package sessdata
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/dbdata"
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
|
@ -34,8 +33,8 @@ func saveStatsInfo() {
|
||||||
onlineNum += 1
|
onlineNum += 1
|
||||||
numGroups[v.CSess.Group.Id] += 1
|
numGroups[v.CSess.Group.Id] += 1
|
||||||
// 网络吞吐
|
// 网络吞吐
|
||||||
userUp := atomic.LoadUint32(&v.CSess.BandwidthUpPeriod)
|
userUp := v.CSess.BandwidthUpPeriod.Load()
|
||||||
userDown := atomic.LoadUint32(&v.CSess.BandwidthDownPeriod)
|
userDown := v.CSess.BandwidthDownPeriod.Load()
|
||||||
if userUp > 0 {
|
if userUp > 0 {
|
||||||
upGroups[v.CSess.Group.Id] += userUp
|
upGroups[v.CSess.Group.Id] += userUp
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue