From a8b9a8d5536757f0f3bc454da03d7d607fd9542d Mon Sep 17 00:00:00 2001 From: steden <1470804@qq.com> Date: Wed, 20 Nov 2024 21:00:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8sonic=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 8 ++++++++ rabbitProduct.go | 13 +++++++------ test/sendJson_test.go | 11 ++++++----- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 0ea106f..d889492 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21 toolchain go1.23.3 require ( + github.com/bytedance/sonic v1.12.4 github.com/farseer-go/collections v0.15.0 github.com/farseer-go/fs v0.15.0 github.com/rabbitmq/amqp091-go v1.10.0 @@ -12,8 +13,15 @@ require ( ) require ( + github.com/bytedance/sonic/loader v0.2.1 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/klauspost/cpuid/v2 v2.2.9 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/timandy/routine v1.1.4 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + golang.org/x/arch v0.12.0 // indirect + golang.org/x/sys v0.27.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/rabbitProduct.go b/rabbitProduct.go index 2807c25..9d1c659 100644 --- a/rabbitProduct.go +++ b/rabbitProduct.go @@ -3,15 +3,16 @@ package rabbit import ( "context" "crypto/md5" - "encoding/json" "fmt" + "sync" + "sync/atomic" + "time" + + "github.com/bytedance/sonic" "github.com/farseer-go/fs/core" "github.com/farseer-go/fs/flog" "github.com/farseer-go/fs/parse" amqp "github.com/rabbitmq/amqp091-go" - "sync" - "sync/atomic" - "time" ) type rabbitProduct struct { @@ -126,7 +127,7 @@ func (receiver *rabbitProduct) SendString(message string) error { // SendJson 发送消息,将data序列化成json(使用配置设置) func (receiver *rabbitProduct) SendJson(data any) error { - message, _ := json.Marshal(data) + message, _ := sonic.Marshal(data) messageId := fmt.Sprintf("%x", md5.Sum(message)) return receiver.SendMessage(message, receiver.manager.config.RoutingKey, messageId, 0) } @@ -139,7 +140,7 @@ func (receiver *rabbitProduct) SendStringKey(message, routingKey string) error { // SendJsonKey 发送消息(使用配置设置) func (receiver *rabbitProduct) SendJsonKey(data any, routingKey string) error { - message, _ := json.Marshal(data) + message, _ := sonic.Marshal(data) messageId := fmt.Sprintf("%x", md5.Sum(message)) return receiver.SendMessage(message, routingKey, messageId, 0) } diff --git a/test/sendJson_test.go b/test/sendJson_test.go index 782cd0a..ad688af 100644 --- a/test/sendJson_test.go +++ b/test/sendJson_test.go @@ -1,12 +1,13 @@ package test import ( - "encoding/json" + "testing" + "time" + + "github.com/bytedance/sonic" "github.com/farseer-go/fs/container" "github.com/farseer-go/rabbit" "github.com/stretchr/testify/assert" - "testing" - "time" ) func TestSendJson(t *testing.T) { @@ -15,7 +16,7 @@ func TestSendJson(t *testing.T) { // 手动ACK consumer.SubscribeAck("TestSingleJsonAck", "Test_Single_Json_Ack", 100, func(message string, ea rabbit.EventArgs) bool { var dic map[string]string - _ = json.Unmarshal([]byte(message), &dic) + _ = sonic.Unmarshal([]byte(message), &dic) assert.Equal(t, 1, len(dic)) r := dic["ack"] @@ -26,7 +27,7 @@ func TestSendJson(t *testing.T) { // 自动ACK consumer.Subscribe("TestSingleJsonAutoAck", "Test_Single_Json_AutoAck", 100, func(message string, ea rabbit.EventArgs) { var dic map[string]string - _ = json.Unmarshal([]byte(message), &dic) + _ = sonic.Unmarshal([]byte(message), &dic) assert.Equal(t, 1, len(dic)) r := dic["auto_ack"]