This commit is contained in:
Liang Ding 2016-06-12 18:17:36 +08:00
parent 487d97d844
commit 0062c28edb
2 changed files with 30 additions and 4 deletions

View File

@ -32,7 +32,7 @@ import (
const (
outputBufMax = 1024 // 1024 string(rune)
outputTimeout = 100 // 100ms
outputCountMax = 10 // 10 reads
outputCountMax = 30 // 30 reads
)
type outputBuf struct {

View File

@ -29,8 +29,9 @@ import (
)
const (
outputBufMax = 128 // 128 string(rune)
outputTimeout = 100 // 100ms
outputBufMax = 1024 // 1024 string(rune)
outputTimeout = 100 // 100ms
outputCountMax = 30 // 30 reads
)
type outputBuf struct {
@ -133,6 +134,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
defer util.Recover()
buf := outputBuf{}
count := 0
for {
wsChannel := session.PlaygroundWS[sid]
@ -141,6 +143,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
}
r, _, err := outReader.ReadRune()
count++
if nil != err {
// remove the exited process from user process set
@ -171,11 +174,14 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
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["output"] = buf.content
buf = outputBuf{} // a new buffer
count = 0 // clear count
err = wsChannel.WriteJSON(&channelRet)
if nil != err {
@ -184,6 +190,26 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
}
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
}
}
}()