webhook will be lock by repo to prevent re-entrance.
This commit is contained in:
parent
2e567fae8b
commit
fd28954e48
1 changed files with 23 additions and 6 deletions
29
lib/hook.go
29
lib/hook.go
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WebhookCFG struct {
|
type WebhookCFG struct {
|
||||||
|
|
@ -112,7 +113,21 @@ func (c *WebhookCFG) clone(user, repo string) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var repoLock = &sync.Map{}
|
||||||
|
|
||||||
|
func (c *WebhookCFG) lock(user, repo string) func() {
|
||||||
|
key := user + "/" + repo
|
||||||
|
|
||||||
|
lock, _ := repoLock.LoadOrStore(key, &sync.Mutex{})
|
||||||
|
m := lock.(*sync.Mutex)
|
||||||
|
m.Lock()
|
||||||
|
return m.Unlock
|
||||||
|
}
|
||||||
|
|
||||||
func (c *WebhookCFG) download(user, repo string) (err error) {
|
func (c *WebhookCFG) download(user, repo string) (err error) {
|
||||||
|
unlock := c.lock(user, repo)
|
||||||
|
defer unlock()
|
||||||
|
|
||||||
gitDir := c.gitDir(user, repo)
|
gitDir := c.gitDir(user, repo)
|
||||||
if _, err = os.Stat(gitDir); os.IsNotExist(err) {
|
if _, err = os.Stat(gitDir); os.IsNotExist(err) {
|
||||||
err = c.clone(user, repo)
|
err = c.clone(user, repo)
|
||||||
|
|
@ -169,12 +184,14 @@ func (c *WebhookCFG) handle(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
user, repo := path.Split(payload.Repository.FullName)
|
user, repo := path.Split(payload.Repository.FullName)
|
||||||
user = user[:len(user)-1]
|
user = user[:len(user)-1]
|
||||||
fmt.Println("Pulling ", user, repo)
|
go func() {
|
||||||
err = c.download(user, repo)
|
fmt.Println("Pulling ", user, repo)
|
||||||
if err != nil {
|
err = c.download(user, repo)
|
||||||
fmt.Println("Failed to download repository: ", err)
|
if err != nil {
|
||||||
return
|
fmt.Println("Failed to download repository: ", err)
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func UseWebhook(bind string, cfg *WebhookCFG) (*http.Server, error) {
|
func UseWebhook(bind string, cfg *WebhookCFG) (*http.Server, error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue