aboutsummaryrefslogtreecommitdiff
path: root/models/webhook.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/webhook.go')
-rw-r--r--models/webhook.go65
1 files changed, 4 insertions, 61 deletions
diff --git a/models/webhook.go b/models/webhook.go
index 2db02741..084a7ee7 100644
--- a/models/webhook.go
+++ b/models/webhook.go
@@ -10,10 +10,8 @@ import (
"fmt"
"io/ioutil"
"strings"
- "sync"
"time"
- "github.com/Unknwon/com"
"github.com/go-xorm/xorm"
gouuid "github.com/satori/go.uuid"
@@ -22,8 +20,11 @@ import (
"github.com/gogits/gogs/modules/httplib"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/setting"
+ "github.com/gogits/gogs/modules/sync"
)
+var HookQueue = sync.NewUniqueQueue(setting.Webhook.QueueLength)
+
type HookContentType int
const (
@@ -500,64 +501,6 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err
return nil
}
-// UniqueQueue represents a queue that guarantees only one instance of same ID is in the line.
-type UniqueQueue struct {
- lock sync.Mutex
- ids map[string]bool
-
- queue chan string
-}
-
-func (q *UniqueQueue) Queue() <-chan string {
- return q.queue
-}
-
-func NewUniqueQueue(queueLength int) *UniqueQueue {
- if queueLength <= 0 {
- queueLength = 100
- }
-
- return &UniqueQueue{
- ids: make(map[string]bool),
- queue: make(chan string, queueLength),
- }
-}
-
-func (q *UniqueQueue) Remove(id interface{}) {
- q.lock.Lock()
- defer q.lock.Unlock()
- delete(q.ids, com.ToStr(id))
-}
-
-func (q *UniqueQueue) AddFunc(id interface{}, fn func()) {
- newid := com.ToStr(id)
-
- if q.Exist(id) {
- return
- }
-
- q.lock.Lock()
- q.ids[newid] = true
- if fn != nil {
- fn()
- }
- q.lock.Unlock()
- q.queue <- newid
-}
-
-func (q *UniqueQueue) Add(id interface{}) {
- q.AddFunc(id, nil)
-}
-
-func (q *UniqueQueue) Exist(id interface{}) bool {
- q.lock.Lock()
- defer q.lock.Unlock()
-
- return q.ids[com.ToStr(id)]
-}
-
-var HookQueue = NewUniqueQueue(setting.Webhook.QueueLength)
-
func (t *HookTask) deliver() {
t.IsDelivered = true
@@ -654,7 +597,7 @@ func DeliverHooks() {
// Start listening on new hook requests.
for repoID := range HookQueue.Queue() {
- log.Trace("DeliverHooks [%v]: processing delivery hooks", repoID)
+ log.Trace("DeliverHooks [repo_id: %v]", repoID)
HookQueue.Remove(repoID)
tasks = make([]*HookTask, 0, 5)