diff options
author | nickago <ngonella@calpoly.edu> | 2015-08-25 09:19:07 -0700 |
---|---|---|
committer | nickago <ngonella@calpoly.edu> | 2015-08-25 09:19:07 -0700 |
commit | 4a0eeba2372755de574f69ca97525974addec8a3 (patch) | |
tree | 29775b6b4951355e41c222a96abf1a018b6f7bfe /api/command.go | |
parent | 19a48acfc60ffa3500360474362bb5c52c12f278 (diff) | |
download | chat-4a0eeba2372755de574f69ca97525974addec8a3.tar.gz chat-4a0eeba2372755de574f69ca97525974addec8a3.tar.bz2 chat-4a0eeba2372755de574f69ca97525974addec8a3.zip |
Added semaphore for resource allocation to echo command
Diffstat (limited to 'api/command.go')
-rw-r--r-- | api/command.go | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/api/command.go b/api/command.go index 31f676a11..749cbf790 100644 --- a/api/command.go +++ b/api/command.go @@ -24,6 +24,8 @@ var commands = []commandHandler{ echoCommand, } +var echoSem chan bool + func InitCommand(r *mux.Router) { l4g.Debug("Initializing command api routes") r.Handle("/command", ApiUserRequired(command)).Methods("POST") @@ -95,6 +97,7 @@ func logoutCommand(c *Context, command *model.Command) bool { func echoCommand(c *Context, command *model.Command) bool { cmd := "/echo" + maxThreads := 100 if !command.Suggest && strings.Index(command.Command, cmd) == 0 { parameters := strings.SplitN(command.Command, " ", 2) @@ -118,8 +121,24 @@ func echoCommand(c *Context, command *model.Command) bool { } } - // Fire off asynchronous process + if delay > 10000 { + c.Err = model.NewAppError("echoCommand", "Delays must be under 10000 seconds", "") + return false + } + + if echoSem == nil { + // We want one additional thread allowed so we never reach channel lockup + echoSem = make(chan bool, maxThreads+1) + } + + if len(echoSem) >= maxThreads { + c.Err = model.NewAppError("echoCommand", "High volume of echo request, cannot process request", "") + return false + } + + echoSem <- true go func() { + defer func() { <-echoSem }() post := &model.Post{} post.ChannelId = command.ChannelId post.Message = message |