Skip to content

Commit

Permalink
run every request in separate goroutine + wait 1 second before proces…
Browse files Browse the repository at this point in the history
…sing.
  • Loading branch information
jibon57 committed Sep 3, 2024
1 parent 84c837c commit 364033b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
17 changes: 6 additions & 11 deletions pkg/controllers/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@ import (

func HandleWebhook(c *fiber.Ctx) error {
data := c.Body()
body := make([]byte, len(data))
copy(body, data)

token := c.Request().Header.Peek("Authorization")
authToken := make([]byte, len(token))
copy(authToken, token)

if len(authToken) == 0 {
if len(token) == 0 {
return c.SendStatus(fiber.StatusForbidden)
}

m := models.NewAuthModel(nil, nil)
// here request is coming from livekit
// so, we'll use livekit secret to validate
_, err := m.ValidateLivekitWebhookToken(body, string(authToken))
// here request is coming from livekit, so
// we'll use livekit secret to validate
_, err := m.ValidateLivekitWebhookToken(data, string(token))
if err != nil {
return c.SendStatus(fiber.StatusForbidden)
}
Expand All @@ -32,12 +27,12 @@ func HandleWebhook(c *fiber.Ctx) error {
DiscardUnknown: true,
}
event := new(livekit.WebhookEvent)
if err = op.Unmarshal(body, event); err != nil {
if err = op.Unmarshal(data, event); err != nil {
return c.SendStatus(fiber.StatusForbidden)
}

mm := models.NewWebhookModel(nil, nil, nil, nil)
mm.HandleWebhookEvents(event)
go mm.HandleWebhookEvents(event)

return c.SendStatus(fiber.StatusOK)
}
6 changes: 6 additions & 0 deletions pkg/models/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
natsservice "github.com/mynaparrot/plugnmeet-server/pkg/services/nats"
"github.com/mynaparrot/plugnmeet-server/pkg/services/redis"
log "github.com/sirupsen/logrus"
"time"
)

type WebhookModel struct {
Expand Down Expand Up @@ -50,6 +51,11 @@ func NewWebhookModel(app *config.AppConfig, ds *dbservice.DatabaseService, rs *r
}

func (m *WebhookModel) HandleWebhookEvents(e *livekit.WebhookEvent) {
// wait 1 second before start processing
// otherwise services may not be ready &
// give unexpected results
time.Sleep(time.Second * 1)

switch e.GetEvent() {
case "room_started":
m.roomStarted(e)
Expand Down
1 change: 0 additions & 1 deletion pkg/models/webhook_room_end.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func (m *WebhookModel) roomFinished(event *livekit.WebhookEvent) {
log.Warnln(fmt.Sprintf("invalid webhook info received: %+v", event))
return
}
time.Sleep(time.Second * 1)

rInfo, err := m.natsService.GetRoomInfo(event.Room.Name)
if err != nil || rInfo == nil {
Expand Down

0 comments on commit 364033b

Please sign in to comment.