diff --git a/storage/buffer.go b/storage/buffer.go index b447979a8b87209a866252bda23ccae5371c8f8d..7edcfa092af59344e0cf8c185aa74da7a7a34b3e 100644 --- a/storage/buffer.go +++ b/storage/buffer.go @@ -7,35 +7,37 @@ import ( "sync/atomic" ) - - type NotificationBuffer struct { - bufMap atomic.Value - counter *int64 + bufMap atomic.Value + counter *int64 } -func NewNotificationBuffer()*NotificationBuffer { +func NewNotificationBuffer() *NotificationBuffer { u := int64(0) - return &NotificationBuffer{ + sm := sync.Map{} + nb := &NotificationBuffer{ counter: &u, + bufMap: atomic.Value{}, } + nb.bufMap.Store(&sm) + return nb } -func (bnm *NotificationBuffer)Swap(maxNotifications uint)map[int64][]*pb.NotificationData{ +func (bnm *NotificationBuffer) Swap(maxNotifications uint) map[int64][]*pb.NotificationData { newSM := &sync.Map{} m := bnm.bufMap.Swap(newSM).(*sync.Map) outMap := make(map[int64][]*pb.NotificationData) - f := func(_, value interface{}) bool{ + f := func(_, value interface{}) bool { n := value.(*pb.NotificationData) nSlice, exists := outMap[n.EphemeralID] - if exists{ - if uint(len(nSlice))>=maxNotifications{ + if exists { + if uint(len(nSlice)) >= maxNotifications { bnm.Add(n) - }else{ + } else { nSlice = append(nSlice, n) } - }else{ + } else { nSlice = []*pb.NotificationData{n} } outMap[n.EphemeralID] = nSlice @@ -47,8 +49,7 @@ func (bnm *NotificationBuffer)Swap(maxNotifications uint)map[int64][]*pb.Notific return outMap } -func (bnm *NotificationBuffer)Add(n *pb.NotificationData){ - c := atomic.AddInt64(bnm.counter,1) - bnm.bufMap.Load().(*sync.Map).Store(strconv.FormatInt(c,16),n) +func (bnm *NotificationBuffer) Add(n *pb.NotificationData) { + c := atomic.AddInt64(bnm.counter, 1) + bnm.bufMap.Load().(*sync.Map).Store(strconv.FormatInt(c, 16), n) } -