This commit is contained in:
Liang Ding 2016-06-12 17:58:53 +08:00
parent 6b4ec1b924
commit 487d97d844
1 changed files with 29 additions and 3 deletions

View File

@ -30,8 +30,9 @@ import (
) )
const ( const (
outputBufMax = 128 // 128 string(rune) outputBufMax = 1024 // 1024 string(rune)
outputTimeout = 100 // 100ms outputTimeout = 100 // 100ms
outputCountMax = 10 // 10 reads
) )
type outputBuf struct { type outputBuf struct {
@ -136,6 +137,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
defer util.Recover() defer util.Recover()
buf := outputBuf{} buf := outputBuf{}
count := 0
for { for {
wsChannel := session.OutputWS[sid] wsChannel := session.OutputWS[sid]
@ -144,6 +146,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
} }
r, _, err := outReader.ReadRune() r, _, err := outReader.ReadRune()
count++
if nil != err { if nil != err {
// remove the exited process from user's process set // remove the exited process from user's process set
@ -177,11 +180,14 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
buf.millisecond = now buf.millisecond = now
} }
if now-outputTimeout >= buf.millisecond || len(buf.content) > outputBufMax || oneRuneStr == "\n" { flood := count > outputCountMax
if "\n" == oneRuneStr && !flood {
channelRet["cmd"] = "run" channelRet["cmd"] = "run"
channelRet["output"] = buf.content channelRet["output"] = buf.content
buf = outputBuf{} // a new buffer buf = outputBuf{} // a new buffer
count = 0 // clear count
err = wsChannel.WriteJSON(&channelRet) err = wsChannel.WriteJSON(&channelRet)
if nil != err { if nil != err {
@ -190,6 +196,26 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
} }
wsChannel.Refresh() wsChannel.Refresh()
continue
}
if now-outputTimeout >= buf.millisecond || len(buf.content) > outputBufMax {
channelRet["cmd"] = "run"
channelRet["output"] = buf.content
buf = outputBuf{} // a new buffer
count = 0 // clear count
err = wsChannel.WriteJSON(&channelRet)
if nil != err {
logger.Warn(err)
break
}
wsChannel.Refresh()
continue
} }
} }
}() }()