Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix eventserver response time #63

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 53 additions & 47 deletions eventserver/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,34 @@ func (h *EventServerHandler) PostClick(c *gin.Context) {
return
}

present := h.cacheService.IsPresent(token)
if !present {
h.cacheService.Add(token)
eventData := &dto.ClickEvent{
PublisherId: uint32(data.PublisherID),
EventTime: time.Now().Format(time.RFC3339),
AdId: uint32(data.AdID),
Bid: data.Bid,
}

// Marshal to Protobuf
protoData, err := proto.Marshal(eventData)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to produce click event"})
}

err = h.producer.Produce(protoData, h.clickTopic)
c.Redirect(http.StatusMovedPermanently, data.RedirectPath)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to produce click event"})
return
// Running in goroutine so the server would respond without needing to execute all of this code
go func() {
present := h.cacheService.IsPresent(token)
if !present {
h.cacheService.Add(token)
eventData := &dto.ClickEvent{
PublisherId: uint32(data.PublisherID),
EventTime: time.Now().Format(time.RFC3339),
AdId: uint32(data.AdID),
Bid: data.Bid,
}

// Marshal to Protobuf
protoData, err := proto.Marshal(eventData)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to produce click event"})
}

err = h.producer.Produce(protoData, h.clickTopic)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to produce click event"})
return
}
}
}

c.Redirect(http.StatusMovedPermanently, data.RedirectPath)
}()
}

// PostImpression handles impression events and produces them to a Kafka topic.
Expand All @@ -108,29 +111,32 @@ func (h *EventServerHandler) PostImpression(c *gin.Context) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token"})
return
}

present := h.cacheService.IsPresent(token)
if !present {
h.cacheService.Add(token)

eventData := &dto.ImpressionEvent{
PublisherId: uint32(data.PublisherID),
EventTime: time.Now().Format(time.RFC3339),
AdId: uint32(data.AdID),
Bid: data.Bid,
}

// Marshal to Protobuf
protoData, err := proto.Marshal(eventData)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to produce click event"})

// Running in goroutine so the server would respond without needing to execute all of this code
go func() {
present := h.cacheService.IsPresent(token)
if !present {
h.cacheService.Add(token)

eventData := &dto.ImpressionEvent{
PublisherId: uint32(data.PublisherID),
EventTime: time.Now().Format(time.RFC3339),
AdId: uint32(data.AdID),
Bid: data.Bid,
}

// Marshal to Protobuf
protoData, err := proto.Marshal(eventData)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to produce click event"})
}

err = h.producer.Produce(protoData, h.impressionTopic)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to produce click event"})
return
}
}

err = h.producer.Produce(protoData, h.impressionTopic)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to produce click event"})
return
}
}
}()
}
Loading