diff options
author | Unknwon <u@gogs.io> | 2016-08-30 15:50:30 -0700 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-08-30 15:50:30 -0700 |
commit | c1ecb6c60a1acacf530e226b8043ca93d2fe4a07 (patch) | |
tree | 5df15490f2f6c53f1b633dbefaf4d99df48347f1 /models/webhook.go | |
parent | 43297148b22b976d9cec0c56c863a704f3691c0e (diff) | |
download | gitea-c1ecb6c60a1acacf530e226b8043ca93d2fe4a07.tar.gz gitea-c1ecb6c60a1acacf530e226b8043ca93d2fe4a07.zip |
modules/sync: add UniqueQueue
Diffstat (limited to 'models/webhook.go')
-rw-r--r-- | models/webhook.go | 63 |
1 files changed, 3 insertions, 60 deletions
diff --git a/models/webhook.go b/models/webhook.go index 2db0274115..528dd5e474 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 |