workerpool.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package webhooks
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "runtime"
  7. "sync"
  8. log "github.com/sirupsen/logrus"
  9. "github.com/owncast/owncast/core/data"
  10. "github.com/owncast/owncast/models"
  11. )
  12. // webhookWorkerPoolSize defines the number of concurrent HTTP webhook requests.
  13. var webhookWorkerPoolSize = runtime.GOMAXPROCS(0)
  14. // Job struct bundling the webhook and the payload in one struct.
  15. type Job struct {
  16. wg *sync.WaitGroup
  17. payload WebhookEvent
  18. webhook models.Webhook
  19. }
  20. var (
  21. queue chan Job
  22. getStatus func() models.Status
  23. )
  24. // SetupWebhooks initializes the webhook worker pool and sets the function to get the current status.
  25. func SetupWebhooks(getStatusFunc func() models.Status) {
  26. getStatus = getStatusFunc
  27. initWorkerPool()
  28. }
  29. // initWorkerPool starts n go routines that await webhook jobs.
  30. func initWorkerPool() {
  31. queue = make(chan Job)
  32. // start workers
  33. for i := 1; i <= webhookWorkerPoolSize; i++ {
  34. go worker(i, queue)
  35. }
  36. }
  37. func addToQueue(webhook models.Webhook, payload WebhookEvent, wg *sync.WaitGroup) {
  38. log.Tracef("Queued Event %s for Webhook %s", payload.Type, webhook.URL)
  39. queue <- Job{wg, payload, webhook}
  40. }
  41. func worker(workerID int, queue <-chan Job) {
  42. log.Debugf("Started Webhook worker %d", workerID)
  43. for job := range queue {
  44. log.Debugf("Event %s sent to Webhook %s using worker %d", job.payload.Type, job.webhook.URL, workerID)
  45. if err := sendWebhook(job); err != nil {
  46. log.Errorf("Event: %s failed to send to webhook: %s Error: %s", job.payload.Type, job.webhook.URL, err)
  47. }
  48. log.Tracef("Done with Event %s to Webhook %s using worker %d", job.payload.Type, job.webhook.URL, workerID)
  49. if job.wg != nil {
  50. job.wg.Done()
  51. }
  52. }
  53. }
  54. func sendWebhook(job Job) error {
  55. jsonText, err := json.Marshal(job.payload)
  56. if err != nil {
  57. return err
  58. }
  59. req, err := http.NewRequest("POST", job.webhook.URL, bytes.NewReader(jsonText))
  60. if err != nil {
  61. return err
  62. }
  63. req.Header.Set("Content-Type", "application/json")
  64. client := &http.Client{}
  65. resp, err := client.Do(req)
  66. if err != nil {
  67. return err
  68. }
  69. defer resp.Body.Close()
  70. if err := data.SetWebhookAsUsed(job.webhook); err != nil {
  71. log.Warnln(err)
  72. }
  73. return nil
  74. }