@@ -3,6 +3,7 @@ package message
33import (
44 "encoding/json"
55 "fmt"
6+ "sync"
67
78 "github.com/jin06/binlogo/pkg/store/model/pipeline"
89)
@@ -23,16 +24,16 @@ type STATUS int16
2324type Message struct {
2425 Status int16
2526 Filter bool
26- Content * Content
27+ Content Content
2728}
2829
2930// New return a new message
3031func New () * Message {
3132 msg := & Message {
3233 Status : STATUS_NEW ,
3334 Filter : false ,
34- Content : & Content {
35- Head : & Head {},
35+ Content : Content {
36+ Head : Head {},
3637 Data : nil ,
3738 },
3839 }
@@ -41,17 +42,30 @@ func New() *Message {
4142
4243// Content of Message
4344type Content struct {
44- Head * Head `json:"head"`
45+ Head Head `json:"head"`
4546 Data interface {} `json:"data"`
4647}
4748
49+ func (c * Content ) reset () {
50+ c .Data = nil
51+ c .Head .reset ()
52+ }
53+
4854// Head of Message
4955type Head struct {
50- Type string `json:"type"`
51- Time uint32 `json:"time"`
52- Database string `json:"database"`
53- Table string `json:"table"`
54- Position * pipeline.Position `json:"position"`
56+ Type string `json:"type"`
57+ Time uint32 `json:"time"`
58+ Database string `json:"database"`
59+ Table string `json:"table"`
60+ Position pipeline.Position `json:"position"`
61+ }
62+
63+ func (h * Head ) reset () {
64+ h .Type = ""
65+ h .Time = 0
66+ h .Database = ""
67+ h .Table = ""
68+ h .Position .Reset ()
5569}
5670
5771// Json marshal message to json data
@@ -65,9 +79,9 @@ func (msg *Message) Json() (string, error) {
6579
6680// JsonContent only marshal message's content to josn data
6781func (msg * Message ) JsonContent () (string , error ) {
68- if msg .Content == nil {
69- return "" , nil
70- }
82+ // if msg.Content == nil {
83+ // return "", nil
84+ // }
7185 b , err := json .Marshal (msg .Content )
7286 if err != nil {
7387 return "" , err
@@ -79,16 +93,44 @@ func (msg *Message) JsonContent() (string, error) {
7993func (msg * Message ) ToString () string {
8094 res := "Status: " + fmt .Sprintf ("%v\n " , msg .Status )
8195 res += "Filter: " + fmt .Sprintf ("%v\n " , msg .Filter )
82- if msg .Content != nil {
83- res += "BinlogPosition.File: " + fmt .Sprintf ("%v\n " , msg .Content .Head .Position .BinlogFile )
84- res += "BinlogPosition.Pos: " + fmt .Sprintf ("%v\n " , msg .Content .Head .Position .BinlogPosition )
85- res += "BinlogPosition.GTID: " + fmt .Sprintf ("%v\n " , msg .Content .Head .Position .GTIDSet )
86- res += "Content.Head: " + fmt .Sprintf ("%v\n " , msg .Content .Head )
87- res += "Content.Data: " + fmt .Sprintf ("%v\n " , msg .Content .Data )
88- }
96+ // if msg.Content != nil {
97+ res += "BinlogPosition.File: " + fmt .Sprintf ("%v\n " , msg .Content .Head .Position .BinlogFile )
98+ res += "BinlogPosition.Pos: " + fmt .Sprintf ("%v\n " , msg .Content .Head .Position .BinlogPosition )
99+ res += "BinlogPosition.GTID: " + fmt .Sprintf ("%v\n " , msg .Content .Head .Position .GTIDSet )
100+ res += "Content.Head: " + fmt .Sprintf ("%v\n " , msg .Content .Head )
101+ res += "Content.Data: " + fmt .Sprintf ("%v\n " , msg .Content .Data )
102+ // }
89103 return res
90104}
91105
106+ // Table returns table with database
92107func (msg * Message ) Table () string {
93108 return fmt .Sprintf ("%s.%s" , msg .Content .Head .Database , msg .Content .Head .Table )
94109}
110+
111+ // reset
112+ func (msg * Message ) reset () {
113+ msg .Status = STATUS_NEW
114+ msg .Filter = false
115+ msg .Content .reset ()
116+ }
117+
118+ // Pool reuse message object
119+ var Pool = sync.Pool {
120+ New : func () interface {} {
121+ return & Message {
122+ }
123+ },
124+ }
125+
126+ // Get get a message object from Pool
127+ func Get () * Message {
128+ msg := Pool .Get ().(* Message )
129+ msg .reset ()
130+ return msg
131+ }
132+
133+ // Put put a message to Pool
134+ func Put (msg * Message ) {
135+ Pool .Put (msg )
136+ }
0 commit comments