Don't send command output too fast. 5.0.1 eel/5.0.1 eel_5.0.1
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Fri, 26 Jan 2018 16:19:13 +0000 (17:19 +0100)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Fri, 26 Jan 2018 16:19:13 +0000 (17:19 +0100)
Changes done in eows package of xds-common now allow to accumulate command output during a period
of time (eg. 500ms) or up to the end of a line.
Usage of this feature is mandatory to avoid to post to
much buffers in websocket and consequently flood client (eg. web-browser)

Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
lib/xdsserver/sdk.go

index 633fbee..2736246 100644 (file)
@@ -20,7 +20,6 @@ package xdsserver
 import (
        "encoding/json"
        "fmt"
-       "os"
        "os/exec"
        "path"
        "strconv"
@@ -62,9 +61,6 @@ type CrossSDK struct {
        scripts    map[string]string
        installCmd *eows.ExecOverWS
        removeCmd  *eows.ExecOverWS
-
-       bufStdout string
-       bufStderr string
 }
 
 // ListCrossSDK List all available and installed SDK  (call "db-dump" script)
@@ -233,28 +229,13 @@ func (s *CrossSDK) Install(file string, force bool, timeout int, args []string,
        // Create new instance to execute command and sent output over WS
        s.installCmd = eows.New(s.scripts[scriptAdd], cmdArgs, sess.IOSocket, sess.ID, cmdID)
        s.installCmd.Log = s.Log
+       s.installCmd.LineTimeSpan = 500 * time.Millisecond.Nanoseconds()
        if timeout > 0 {
                s.installCmd.CmdExecTimeout = timeout
        } else {
                s.installCmd.CmdExecTimeout = 30 * 60 // default 30min
        }
 
-       // FIXME: temporary hack
-       s.bufStdout = ""
-       s.bufStderr = ""
-       SizeBufStdout := 10
-       SizeBufStderr := 2000
-       if valS, ok := os.LookupEnv("XDS_SDK_BUF_STDOUT"); ok {
-               if valI, err := strconv.Atoi(valS); err == nil {
-                       SizeBufStdout = valI
-               }
-       }
-       if valS, ok := os.LookupEnv("XDS_SDK_BUF_STDERR"); ok {
-               if valI, err := strconv.Atoi(valS); err == nil {
-                       SizeBufStderr = valI
-               }
-       }
-
        // Define callback for output (stdout+stderr)
        s.installCmd.OutputCB = func(e *eows.ExecOverWS, stdout, stderr string) {
                // paranoia
@@ -281,27 +262,18 @@ func (s *CrossSDK) Install(file string, force bool, timeout int, args []string,
                        }
                }
 
-               // Temporary "Hack": Buffered sent data to avoid freeze in web Browser
-               // FIXME: remove bufStdout & bufStderr and implement better algorithm
-               s.bufStdout += stdout
-               s.bufStderr += stderr
-               if len(s.bufStdout) > SizeBufStdout || len(s.bufStderr) > SizeBufStderr {
-                       // Emit event
-                       err := (*so).Emit(xsapiv1.EVTSDKManagement, xsapiv1.SDKManagementMsg{
-                               CmdID:     e.CmdID,
-                               Timestamp: time.Now().String(),
-                               Action:    xsapiv1.SdkMgtActionInstall,
-                               Sdk:       s.sdk,
-                               Progress:  0, // TODO add progress
-                               Exited:    false,
-                               Stdout:    s.bufStdout,
-                               Stderr:    s.bufStderr,
-                       })
-                       if err != nil {
-                               s.Log.Errorf("WS Emit : %v", err)
-                       }
-                       s.bufStdout = ""
-                       s.bufStderr = ""
+               err := (*so).Emit(xsapiv1.EVTSDKManagement, xsapiv1.SDKManagementMsg{
+                       CmdID:     e.CmdID,
+                       Timestamp: time.Now().String(),
+                       Action:    xsapiv1.SdkMgtActionInstall,
+                       Sdk:       s.sdk,
+                       Progress:  0, // TODO add progress
+                       Exited:    false,
+                       Stdout:    stdout,
+                       Stderr:    stderr,
+               })
+               if err != nil {
+                       s.Log.Errorf("WS Emit : %v", err)
                }
        }
 
@@ -323,25 +295,6 @@ func (s *CrossSDK) Install(file string, force bool, timeout int, args []string,
                        return
                }
 
-               // Emit event remaining data in bufStdout/err
-               if len(s.bufStderr) > 0 || len(s.bufStdout) > 0 {
-                       err := (*so).Emit(xsapiv1.EVTSDKManagement, xsapiv1.SDKManagementMsg{
-                               CmdID:     e.CmdID,
-                               Timestamp: time.Now().String(),
-                               Action:    xsapiv1.SdkMgtActionInstall,
-                               Sdk:       s.sdk,
-                               Progress:  50, // TODO add progress
-                               Exited:    false,
-                               Stdout:    s.bufStdout,
-                               Stderr:    s.bufStderr,
-                       })
-                       if err != nil {
-                               s.Log.Errorf("WS Emit : %v", err)
-                       }
-                       s.bufStdout = ""
-                       s.bufStderr = ""
-               }
-
                // Update SDK status
                if code == 0 && exitError == nil {
                        s.sdk.LastError = ""