fix: sync/atomic bug, fix #1804 (#1805)

Co-authored-by: tanghuafa <tanghuafa@bytedance.com>
This commit is contained in:
Tank 2020-05-12 22:09:16 +08:00 committed by GitHub
parent bd6435c982
commit 228e225f84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 17 deletions

View File

@ -177,12 +177,12 @@ func (m *serverMetrics) GetServer() *ServerStats {
s := &ServerStats{ s := &ServerStats{
TotalTrafficIn: m.info.TotalTrafficIn.TodayCount(), TotalTrafficIn: m.info.TotalTrafficIn.TodayCount(),
TotalTrafficOut: m.info.TotalTrafficOut.TodayCount(), TotalTrafficOut: m.info.TotalTrafficOut.TodayCount(),
CurConns: m.info.CurConns.Count(), CurConns: int64(m.info.CurConns.Count()),
ClientCounts: m.info.ClientCounts.Count(), ClientCounts: int64(m.info.ClientCounts.Count()),
ProxyTypeCounts: make(map[string]int64), ProxyTypeCounts: make(map[string]int64),
} }
for k, v := range m.info.ProxyTypeCounts { for k, v := range m.info.ProxyTypeCounts {
s.ProxyTypeCounts[k] = v.Count() s.ProxyTypeCounts[k] = int64(v.Count())
} }
return s return s
} }
@ -202,7 +202,7 @@ func (m *serverMetrics) GetProxiesByType(proxyType string) []*ProxyStats {
Type: proxyStats.ProxyType, Type: proxyStats.ProxyType,
TodayTrafficIn: proxyStats.TrafficIn.TodayCount(), TodayTrafficIn: proxyStats.TrafficIn.TodayCount(),
TodayTrafficOut: proxyStats.TrafficOut.TodayCount(), TodayTrafficOut: proxyStats.TrafficOut.TodayCount(),
CurConns: proxyStats.CurConns.Count(), CurConns: int64(proxyStats.CurConns.Count()),
} }
if !proxyStats.LastStartTime.IsZero() { if !proxyStats.LastStartTime.IsZero() {
ps.LastStartTime = proxyStats.LastStartTime.Format("01-02 15:04:05") ps.LastStartTime = proxyStats.LastStartTime.Format("01-02 15:04:05")
@ -233,7 +233,7 @@ func (m *serverMetrics) GetProxiesByTypeAndName(proxyType string, proxyName stri
Type: proxyStats.ProxyType, Type: proxyStats.ProxyType,
TodayTrafficIn: proxyStats.TrafficIn.TodayCount(), TodayTrafficIn: proxyStats.TrafficIn.TodayCount(),
TodayTrafficOut: proxyStats.TrafficOut.TodayCount(), TodayTrafficOut: proxyStats.TrafficOut.TodayCount(),
CurConns: proxyStats.CurConns.Count(), CurConns: int64(proxyStats.CurConns.Count()),
} }
if !proxyStats.LastStartTime.IsZero() { if !proxyStats.LastStartTime.IsZero() {
res.LastStartTime = proxyStats.LastStartTime.Format("01-02 15:04:05") res.LastStartTime = proxyStats.LastStartTime.Format("01-02 15:04:05")

View File

@ -143,6 +143,7 @@ func SendUdpMsg(addr string, msg string) (res string, err error) {
return return
} }
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
buf := make([]byte, 2048) buf := make([]byte, 2048)
n, errRet := conn.Read(buf) n, errRet := conn.Read(buf)
if errRet != nil { if errRet != nil {

View File

@ -19,9 +19,9 @@ import (
) )
type Counter interface { type Counter interface {
Count() int64 Count() int32
Inc(int64) Inc(int32)
Dec(int64) Dec(int32)
Snapshot() Counter Snapshot() Counter
Clear() Clear()
} }
@ -33,28 +33,28 @@ func NewCounter() Counter {
} }
type StandardCounter struct { type StandardCounter struct {
count int64 count int32
} }
func (c *StandardCounter) Count() int64 { func (c *StandardCounter) Count() int32 {
return atomic.LoadInt64(&c.count) return atomic.LoadInt32(&c.count)
} }
func (c *StandardCounter) Inc(count int64) { func (c *StandardCounter) Inc(count int32) {
atomic.AddInt64(&c.count, count) atomic.AddInt32(&c.count, count)
} }
func (c *StandardCounter) Dec(count int64) { func (c *StandardCounter) Dec(count int32) {
atomic.AddInt64(&c.count, -count) atomic.AddInt32(&c.count, -count)
} }
func (c *StandardCounter) Snapshot() Counter { func (c *StandardCounter) Snapshot() Counter {
tmp := &StandardCounter{ tmp := &StandardCounter{
count: atomic.LoadInt64(&c.count), count: atomic.LoadInt32(&c.count),
} }
return tmp return tmp
} }
func (c *StandardCounter) Clear() { func (c *StandardCounter) Clear() {
atomic.StoreInt64(&c.count, 0) atomic.StoreInt32(&c.count, 0)
} }