From 08ca3b023a4d0d712bf60ce83c7a0cd2a9bb34e8 Mon Sep 17 00:00:00 2001 From: icarus-ai <82353054+icarus-ai@users.noreply.github.com> Date: Wed, 24 Aug 2022 14:22:36 +0800 Subject: [PATCH] deprecate CAS in favor of CompareAndSwap (#291) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * deprecate CAS in favor of CompareAndSwap * ci: update to go1.19 * 为 Statistics 实现 MarshalJSON 接口 (See 2d79fcc) --- .github/workflows/go.yml | 2 +- .github/workflows/goimports.yml | 2 +- client/c2c_processor.go | 2 +- client/client.go | 2 +- client/statistics.go | 44 +++++++++++++++++++++++++------- go.mod | 18 +++++++------ go.sum | 45 ++++++++++++++++++++++----------- 7 files changed, 79 insertions(+), 36 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 720f0273..c5de0468 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -16,7 +16,7 @@ jobs: - name: Set up Go 1.x uses: actions/setup-go@v2 with: - go-version: 1.18 + go-version: 1.19 - name: Check out code into the Go module directory uses: actions/checkout@v2 diff --git a/.github/workflows/goimports.yml b/.github/workflows/goimports.yml index adc39586..dc70f973 100644 --- a/.github/workflows/goimports.yml +++ b/.github/workflows/goimports.yml @@ -14,7 +14,7 @@ jobs: - name: Set up Go 1.x uses: actions/setup-go@v2 with: - go-version: 1.18 + go-version: 1.19 - name: Check out code into the Go module directory uses: actions/checkout@v2 diff --git a/client/c2c_processor.go b/client/c2c_processor.go index 60c4a601..553aca13 100644 --- a/client/c2c_processor.go +++ b/client/c2c_processor.go @@ -106,7 +106,7 @@ func privateMessageDecoder(c *QQClient, pMsg *msg.Message, _ *network.IncomingPa for { frdSeq := c.friendSeq.Load() if frdSeq < pMsg.Head.MsgSeq.Unwrap() { - if c.friendSeq.CAS(frdSeq, pMsg.Head.MsgSeq.Unwrap()) { + if c.friendSeq.CompareAndSwap(frdSeq, pMsg.Head.MsgSeq.Unwrap()) { break } } else { diff --git a/client/client.go b/client/client.go index 9a458238..d4632302 100644 --- a/client/client.go +++ b/client/client.go @@ -9,10 +9,10 @@ import ( "sort" "strconv" "sync" + "sync/atomic" "time" "github.com/pkg/errors" - "go.uber.org/atomic" "github.com/RomiChan/syncx" diff --git a/client/statistics.go b/client/statistics.go index ceb5f7f2..d0079d61 100644 --- a/client/statistics.go +++ b/client/statistics.go @@ -1,20 +1,46 @@ package client import ( - "go.uber.org/atomic" + "bytes" + "strconv" + "sync/atomic" ) type Statistics struct { - PacketReceived atomic.Uint64 `json:"packet_received"` - PacketSent atomic.Uint64 `json:"packet_sent"` - PacketLost atomic.Uint64 `json:"packet_lost"` - MessageReceived atomic.Uint64 `json:"message_received"` - MessageSent atomic.Uint64 `json:"message_sent"` - LastMessageTime atomic.Int64 `json:"last_message_time"` - DisconnectTimes atomic.Uint32 `json:"disconnect_times"` - LostTimes atomic.Uint32 `json:"lost_times"` + PacketReceived atomic.Uint64 + PacketSent atomic.Uint64 + PacketLost atomic.Uint64 + MessageReceived atomic.Uint64 + MessageSent atomic.Uint64 + LastMessageTime atomic.Int64 + DisconnectTimes atomic.Uint32 + LostTimes atomic.Uint32 } func (c *QQClient) GetStatistics() *Statistics { return &c.stat } + +// MarshalJSON encodes the wrapped statistics into JSON. +func (m *Statistics) MarshalJSON() ([]byte, error) { + var w bytes.Buffer + w.Grow(256) + w.WriteString(`{"packet_received":`) + w.WriteString(strconv.FormatUint(m.PacketReceived.Load(), 10)) + w.WriteString(`,"packet_sent":`) + w.WriteString(strconv.FormatUint(m.PacketSent.Load(), 10)) + w.WriteString(`,"packet_lost":`) + w.WriteString(strconv.FormatUint(m.PacketLost.Load(), 10)) + w.WriteString(`,"message_received":`) + w.WriteString(strconv.FormatUint(m.MessageReceived.Load(), 10)) + w.WriteString(`,"message_sent":`) + w.WriteString(strconv.FormatUint(m.MessageSent.Load(), 10)) + w.WriteString(`,"disconnect_times":`) + w.WriteString(strconv.FormatUint(uint64(m.DisconnectTimes.Load()), 10)) + w.WriteString(`,"lost_times":`) + w.WriteString(strconv.FormatUint(uint64(m.LostTimes.Load()), 10)) + w.WriteString(`,"last_message_time":`) + w.WriteString(strconv.FormatInt(m.LastMessageTime.Load(), 10)) + w.WriteByte('}') + return w.Bytes(), nil +} diff --git a/go.mod b/go.mod index 7bfb0aa8..ba0804ca 100644 --- a/go.mod +++ b/go.mod @@ -1,23 +1,25 @@ module github.com/Mrs4s/MiraiGo -go 1.18 +go 1.19 require ( - github.com/RomiChan/protobuf v0.1.1-0.20220624030127-3310cba9dbc0 + github.com/RomiChan/protobuf v0.0.0-20220624030127-3310cba9dbc0 github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c github.com/fumiama/imgsz v0.0.2 - github.com/pierrec/lz4/v4 v4.1.11 + github.com/pierrec/lz4/v4 v4.1.15 github.com/pkg/errors v0.9.1 - github.com/stretchr/testify v1.7.0 - github.com/tidwall/gjson v1.11.0 - go.uber.org/atomic v1.9.0 - golang.org/x/sync v0.0.0-20210220032951-036812b2e83c + github.com/stretchr/testify v1.8.0 + github.com/tidwall/gjson v1.14.3 + golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde ) require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kr/pretty v0.3.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rogpeppe/go-internal v1.9.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f078110e..70cf5d2c 100644 --- a/go.sum +++ b/go.sum @@ -1,33 +1,48 @@ -github.com/RomiChan/protobuf v0.1.1-0.20220624030127-3310cba9dbc0 h1:+UGPBYVjssFsdahLJIiNPwpmmwgl/OaVdv1oc5NonC0= -github.com/RomiChan/protobuf v0.1.1-0.20220624030127-3310cba9dbc0/go.mod h1:2Ie+hdBFQpQFDHfeklgxoFmQRCE7O+KwFpISeXq7OwA= +github.com/RomiChan/protobuf v0.0.0-20220624030127-3310cba9dbc0 h1:GEwcB4dL9vc4veW1fLNt0Fby3wspVflAn5v9/HbUwDM= +github.com/RomiChan/protobuf v0.0.0-20220624030127-3310cba9dbc0/go.mod h1:2Ie+hdBFQpQFDHfeklgxoFmQRCE7O+KwFpISeXq7OwA= github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c h1:cNPOdTNiVwxLpROLjXCgbIPvdkE+BwvxDvgmdYmWx6Q= github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c/go.mod h1:KqZzu7slNKROh3TSYEH/IUMG6f4M+1qubZ5e52QypsE= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fumiama/imgsz v0.0.2 h1:fAkC0FnIscdKOXwAxlyw3EUba5NzxZdSxGaq3Uyfxak= github.com/fumiama/imgsz v0.0.2/go.mod h1:dR71mI3I2O5u6+PCpd47M9TZptzP+39tRBcbdIkoqM4= -github.com/pierrec/lz4/v4 v4.1.11 h1:LVs17FAZJFOjgmJXl9Tf13WfLUvZq7/RjfEJrnwZ9OE= -github.com/pierrec/lz4/v4 v4.1.11/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0= +github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= 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.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/tidwall/gjson v1.11.0 h1:C16pk7tQNiH6VlCrtIXL1w8GaOsi1X3W8KDkE1BuYd4= -github.com/tidwall/gjson v1.11.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw= +github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= -go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= -go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde h1:ejfdSekXMDxDLbRrJMwUk6KnSLZ2McaUCVcIKM+N6jc= +golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/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=