Skip to content

Commit

Permalink
refs #26 Record and check duplicated event
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Mar 13, 2020
1 parent c40f3b0 commit 9fedf96
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion event/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

var notifyHistory = map[string]time.Time{}
var processingEvents = map[string]bool{}

type Server struct {
channel string
Expand Down Expand Up @@ -58,6 +59,19 @@ func (s *Server) HandleEvent(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Check the event whether it has already received.
eventID := event.String("event_id")
if _, ok := processingEvents[eventID]; ok {
s.logger.Infof("Event has already received: %s", eventID)
w.WriteHeader(http.StatusOK)
return
}

// Record event id.
processingEvents[eventID] = true
defer func(eventID string) {
delete(processingEvents, eventID)
}(eventID)

switch event.Type() {
case "url_verification":
Expand All @@ -73,7 +87,7 @@ func (s *Server) HandleEvent(w http.ResponseWriter, r *http.Request) {
http.Error(w, "failed to cast", http.StatusBadRequest)
return
}
s.logger.Infof("Received event: %+v", mes)
s.logger.Infof("Received event: %+v", event)

message := Event(mes)
if message.Type() != "message" {
Expand Down

0 comments on commit 9fedf96

Please sign in to comment.