performance optimization

This commit is contained in:
yuzuki999 2022-09-07 17:26:12 +08:00
parent 45fd9ad15b
commit fe6a21a96c
3 changed files with 21 additions and 13 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/xtls/xray-core/features/inbound" "github.com/xtls/xray-core/features/inbound"
"github.com/xtls/xray-core/features/outbound" "github.com/xtls/xray-core/features/outbound"
"github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/features/routing"
statsFeature "github.com/xtls/xray-core/features/stats"
coreConf "github.com/xtls/xray-core/infra/conf" coreConf "github.com/xtls/xray-core/infra/conf"
io "io/ioutil" io "io/ioutil"
"log" "log"
@ -24,6 +25,7 @@ type Core struct {
Server *core.Instance Server *core.Instance
ihm inbound.Manager ihm inbound.Manager
ohm outbound.Manager ohm outbound.Manager
shm statsFeature.Manager
dispatcher *dispatcher.DefaultDispatcher dispatcher *dispatcher.DefaultDispatcher
} }
@ -155,6 +157,7 @@ func (p *Core) Start() {
if err := p.Server.Start(); err != nil { if err := p.Server.Start(); err != nil {
log.Panicf("Failed to start instance: %s", err) log.Panicf("Failed to start instance: %s", err)
} }
p.shm = p.Server.GetFeature(statsFeature.ManagerType()).(statsFeature.Manager)
p.ihm = p.Server.GetFeature(inbound.ManagerType()).(inbound.Manager) p.ihm = p.Server.GetFeature(inbound.ManagerType()).(inbound.Manager)
p.ohm = p.Server.GetFeature(outbound.ManagerType()).(outbound.Manager) p.ohm = p.Server.GetFeature(outbound.ManagerType()).(outbound.Manager)
p.dispatcher = p.Server.GetFeature(routing.DispatcherType()).(*dispatcher.DefaultDispatcher) p.dispatcher = p.Server.GetFeature(routing.DispatcherType()).(*dispatcher.DefaultDispatcher)

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/core/app/dispatcher" "github.com/Yuzuki616/V2bX/core/app/dispatcher"
"github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/features/stats"
"github.com/xtls/xray-core/proxy" "github.com/xtls/xray-core/proxy"
) )
@ -57,19 +56,25 @@ func (p *Core) RemoveUsers(users []string, tag string) error {
return nil return nil
} }
func (p *Core) GetUserTraffic(email string) (up int64, down int64) { func (p *Core) GetUserTraffic(email string, reset bool) (up int64, down int64) {
upName := "user>>>" + email + ">>>traffic>>>uplink" upName := "user>>>" + email + ">>>traffic>>>uplink"
downName := "user>>>" + email + ">>>traffic>>>downlink" downName := "user>>>" + email + ">>>traffic>>>downlink"
statsManager := p.Server.GetFeature(stats.ManagerType()).(stats.Manager) upCounter := p.shm.GetCounter(upName)
upCounter := statsManager.GetCounter(upName) downCounter := p.shm.GetCounter(downName)
downCounter := statsManager.GetCounter(downName) if reset {
if upCounter != nil {
up = upCounter.Set(0)
}
if downCounter != nil {
down = downCounter.Set(0)
}
} else {
if upCounter != nil { if upCounter != nil {
up = upCounter.Value() up = upCounter.Value()
upCounter.Set(0)
} }
if downCounter != nil { if downCounter != nil {
down = downCounter.Value() down = downCounter.Value()
downCounter.Set(0) }
} }
return up, down return up, down
} }

View File

@ -363,7 +363,7 @@ func (c *Node) userInfoMonitor() (err error) {
// Get User traffic // Get User traffic
userTraffic := make([]panel.UserTraffic, 0) userTraffic := make([]panel.UserTraffic, 0)
for i := range c.userList { for i := range c.userList {
up, down := c.server.GetUserTraffic(c.buildUserTag(&(c.userList)[i])) up, down := c.server.GetUserTraffic(c.buildUserTag(&(c.userList)[i]), true)
if up > 0 || down > 0 { if up > 0 || down > 0 {
userTraffic = append(userTraffic, panel.UserTraffic{ userTraffic = append(userTraffic, panel.UserTraffic{
UID: (c.userList)[i].UID, UID: (c.userList)[i].UID,