* refactor: use worker pool to limit webhooks to 10 concurrent http executions (#1510) * chore: try to please go linter
This commit is contained in:
@@ -1,16 +1,10 @@
|
||||
package webhooks
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/owncast/owncast/core/user"
|
||||
|
||||
"github.com/owncast/owncast/core/data"
|
||||
"github.com/owncast/owncast/core/user"
|
||||
"github.com/owncast/owncast/models"
|
||||
)
|
||||
|
||||
@@ -36,40 +30,6 @@ func SendEventToWebhooks(payload WebhookEvent) {
|
||||
webhooks := data.GetWebhooksForEvent(payload.Type)
|
||||
|
||||
for _, webhook := range webhooks {
|
||||
go func(webhook models.Webhook, payload WebhookEvent) {
|
||||
log.Debugf("Event %s sent to Webhook %s", payload.Type, webhook.URL)
|
||||
if err := sendWebhook(webhook, payload); err != nil {
|
||||
log.Errorf("Event: %s failed to send to webhook: %s Error: %s", payload.Type, webhook.URL, err)
|
||||
}
|
||||
}(webhook, payload)
|
||||
go addToQueue(webhook, payload)
|
||||
}
|
||||
}
|
||||
|
||||
func sendWebhook(webhook models.Webhook, payload WebhookEvent) error {
|
||||
jsonText, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", webhook.URL, bytes.NewReader(jsonText))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if err := data.SetWebhookAsUsed(webhook); err != nil {
|
||||
log.Warnln(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user