@@ -15,14 +15,15 @@ package eventclient
1515import (
1616 "encoding/json"
1717 "net/http"
18+ "reflect"
1819 "time"
1920
20- "gopkg.in/redis.v5"
21-
2221 "configcenter/src/common"
2322 "configcenter/src/common/metadata"
2423 "configcenter/src/common/util"
2524 "configcenter/src/framework/core/errors"
25+
26+ "gopkg.in/redis.v5"
2627)
2728
2829type EventContext struct {
@@ -47,6 +48,14 @@ func (c *EventContext) InsertEvent(eventType, objType, action string, curData in
4748 if c .CacheCli == nil {
4849 return errors .New ("invalid event context with nil cache client" )
4950 }
51+ equal , err := instEqual (curData , preData )
52+ if err != nil {
53+ return err
54+ }
55+ if equal {
56+ return nil
57+ }
58+
5059 eventID , err := c .CacheCli .Incr (common .EventCacheEventIDKey ).Result ()
5160 if err != nil {
5261 return err
@@ -86,3 +95,43 @@ func (c *EventContext) InsertEvent(eventType, objType, action string, curData in
8695 }
8796 return c .CacheCli .LPush (common .EventCacheEventQueueKey , value ).Err ()
8897}
98+
99+ // instEqual Determine whether the data is consistent before and after the change
100+ func instEqual (predata , curdata interface {}) (bool , error ) {
101+ switch {
102+ case predata == curdata :
103+ return true , nil
104+ case predata == nil && curdata != nil :
105+ return false , nil
106+ case curdata == nil && predata != nil :
107+ return false , nil
108+ }
109+
110+ preData , err := toMap (predata )
111+ if err != nil {
112+ return false , err
113+ }
114+ curData , err := toMap (curdata )
115+ if err != nil {
116+ return false , err
117+ }
118+ delete (preData , common .LastTimeField )
119+ delete (curData , common .LastTimeField )
120+
121+ return reflect .DeepEqual (preData , curData ), nil
122+ }
123+
124+ func toMap (data interface {}) (map [string ]interface {}, error ) {
125+ out , err := json .Marshal (data )
126+ if err != nil {
127+ return nil , err
128+ }
129+
130+ m := map [string ]interface {}{}
131+ err = json .Unmarshal (out , & m )
132+ if err != nil {
133+ return nil , err
134+ }
135+
136+ return m , nil
137+ }
0 commit comments