Skip to content

Commit

Permalink
使用sonic包
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Nov 20, 2024
1 parent 01af41d commit a8b9a8d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ 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
github.com/stretchr/testify v1.9.0
)

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
)
13 changes: 7 additions & 6 deletions rabbitProduct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
11 changes: 6 additions & 5 deletions test/sendJson_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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"]
Expand All @@ -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"]
Expand Down

0 comments on commit a8b9a8d

Please sign in to comment.