Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

max-concurrency hook property support #167

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ type Hook struct {
PassEnvironmentToCommand []Argument `json:"pass-environment-to-command,omitempty"`
PassArgumentsToCommand []Argument `json:"pass-arguments-to-command,omitempty"`
JSONStringParameters []Argument `json:"parse-parameters-as-json,omitempty"`
MaxConcurrency int `json:"max-concurrency,omiempty"`
moorereason marked this conversation as resolved.
Show resolved Hide resolved
TriggerRule *Rules `json:"trigger-rule,omitempty"`
TriggerRuleMismatchHttpResponseCode int `json:"trigger-rule-mismatch-http-response-code,omitempty"`
}
Expand Down
24 changes: 23 additions & 1 deletion webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ var (

watcher *fsnotify.Watcher
signals chan os.Signal

limits = make(map[string]chan struct{})
moorereason marked this conversation as resolved.
Show resolved Hide resolved
)

func matchLoadedHook(id string) *hook.Hook {
Expand Down Expand Up @@ -108,7 +110,15 @@ func main() {
if matchLoadedHook(hook.ID) != nil {
log.Fatalf("error: hook with the id %s has already been loaded!\nplease check your hooks file for duplicate hooks ids!\n", hook.ID)
}
log.Printf("\tloaded: %s\n", hook.ID)

msg := fmt.Sprintf("\tloaded: %s", hook.ID)

// initialize concurrency map
if hook.MaxConcurrency > 0 {
moorereason marked this conversation as resolved.
Show resolved Hide resolved
limits[hook.ID] = make(chan struct{}, hook.MaxConcurrency)
msg = fmt.Sprintf("%s (max: %d)", msg, hook.MaxConcurrency)
}
log.Println(msg)
}

loadedHooksFromFiles[hooksFilePath] = newHooks
Expand Down Expand Up @@ -208,6 +218,18 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
if matchedHook := matchLoadedHook(id); matchedHook != nil {
log.Printf("%s got matched\n", id)

// check if we have concurrency limits
if _, ok := limits[id]; ok {
if len(limits[id]) == cap(limits[id]) {
log.Printf("reached concurrency limit for: %s (max=%d)", id, len(limits[id]))
w.WriteHeader(http.StatusTooManyRequests)
fmt.Fprintf(w, "Error occurred while evaluating hook rules.")
moorereason marked this conversation as resolved.
Show resolved Hide resolved
return
}
defer func() { <-limits[id] }()
limits[id] <- struct{}{}
}

body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("error reading the request body. %+v\n", err)
Expand Down