【问题标题】:Go crypto/ssh package, what is the buffer limit for stdoutpipe() io.ReaderGo crypto/ssh 包,stdoutpipe() io.Reader 的缓冲区限制是多少
【发布时间】:2019-02-10 03:27:06
【问题描述】:

我正在编写一个实用程序来使用 crypto/ssh 包在远程服务器上执行命令。我目前正在从 session.stdoutpipe() io.Reader 读取到 bytes.Buffer ,我可以在会话完成后对其进行格式化和打印。

文档说明:

StdoutPipe func()(io.Reader,错误) StdoutPipe 返回一个管道,该管道将在命令启动时连接到远程命令的标准输出。 在 stdout 和 stderr 流之间共享固定数量的缓冲。如果 StdoutPipe 读取器的服务速度不够快,最终可能会导致远程命令阻塞。

到目前为止,我的测试没有任何问题,但我很想知道固定金额是多少。在命令完成之前,我已经成功地流式传输高达 6.5mb 的文本,而无需读取管道阅读器。

有谁知道固定数量是多少,或者命令什么时候开始阻塞?我在源码中找不到。

【问题讨论】:

    标签: go ssh pipe buffer


    【解决方案1】:

    它不在 Go 源代码中,因为它依赖于操作系统。

    应用程序不应依赖于特定的容量:应用程序的设计应使读取进程在数据可用时立即使用数据,以便写入进程不会保持阻塞状态。

    例如,在 Linux 上:

    $ man pipe
    
    PIPE(2)                    Linux Programmer's Manual                   PIPE(2)
    
    NAME
           pipe, pipe2 - create pipe
    
    Pipe capacity
    
           A pipe has a limited capacity.  If the pipe is full, then a write(2)
           will block or fail, depending on whether the O_NONBLOCK flag is set
           (see below).  Different implementations have different limits for the
           pipe capacity.  Applications should not rely on a particular
           capacity: an application should be designed so that a reading process
           consumes data as soon as it is available, so that a writing process
           does not remain blocked.
    
           In Linux versions before 2.6.11, the capacity of a pipe was the same
           as the system page size (e.g., 4096 bytes on i386).  Since Linux
           2.6.11, the pipe capacity is 16 pages (i.e., 65,536 bytes in a system
           with a page size of 4096 bytes).  Since Linux 2.6.35, the default
           pipe capacity is 16 pages, but the capacity can be queried and set
           using the fcntl(2) F_GETPIPE_SZ and F_SETPIPE_SZ operations.  See
           fcntl(2) for more information.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 2011-06-06
      • 2022-11-11
      • 1970-01-01
      • 2014-09-11
      • 2011-06-15
      • 2015-06-07
      相关资源
      最近更新 更多