duanmou9228 2018-05-19 12:12
浏览 104
已采纳

为什么io.Copy会引发NetworkIdleTimeout错误?

please consider the runnable example below.

package main

import (
    "bytes"
    "crypto/rand"
    "crypto/rsa"
    "crypto/tls"
    "crypto/x509"
    "encoding/pem"
    "io"
    "log"
    "math/big"
    "time"

    quic "github.com/lucas-clemente/quic-go"
)

// var qc = &quic.Config{Versions: []quic.VersionNumber{101}}
var qc *quic.Config

// GenerateTLSConfig creates bare-bones TLS config for the server
func GenerateTLSConfig() *tls.Config {
    key, err := rsa.GenerateKey(rand.Reader, 1024)
    if err != nil {
        panic(err)
    }

    // generate a random serial number (a real cert authority would have some logic behind this)
    serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
    serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
    if err != nil {
        panic("failed to generate serial number: " + err.Error())
    }

    template := x509.Certificate{
        SerialNumber: serialNumber,
        // DNSNames:              []string{"localhost"}, // keep mint happy
        NotBefore:             time.Now(),
        NotAfter:              time.Now().Add(time.Hour * 87600), // in 10 years
        BasicConstraintsValid: true,
    }
    certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
    if err != nil {
        panic(err)
    }
    keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
    certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})

    tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
    if err != nil {
        panic(err)
    }
    return &tls.Config{
        Certificates:       []tls.Certificate{tlsCert},
        InsecureSkipVerify: true,
    }
}

func server() (quic.Listener, error) {
    return quic.ListenAddr("localhost:9001", GenerateTLSConfig(), qc)
}

func accept(l quic.Listener) (quic.Session, error) {
    return l.Accept()
}

func acceptStream(sess quic.Session) (quic.Stream, error) {
    return sess.AcceptStream()
}

func client() (quic.Session, error) {
    return quic.DialAddr("localhost:9001", GenerateTLSConfig(), qc)
}

func openStream(sess quic.Session) (quic.Stream, error) {
    return sess.OpenStreamSync()
}

func main() {
    ch := make(chan struct{})

    go func() {
        l, err := server()
        if err != nil {
            log.Fatal("failed to listen:", err)
        }

        sess, err := accept(l)
        if err != nil {
            log.Fatal("failed to accept session:", err)
        }

        s, err := acceptStream(sess)
        if err != nil {
            log.Fatal("failed to accept stream:", err)
        }

        log.Println("stream accepted")

        // b := make([]byte, len("hello, world"))
        // if _, err = s.Read(b); err != nil {
        //  log.Fatal("failed to read from stream:", err)
        // }
        // log.Println(string(b))

        buf := bytes.NewBuffer(make([]byte, len("hello, world")))
        if _, err = io.Copy(buf, s); err != nil {
            log.Fatal("failed to read from stream:", err)
        }
        log.Println(buf.String())

        close(ch)
    }()

    sess, err := client()
    if err != nil {
        log.Fatal("failed to dial:", err)
    }

    s, err := openStream(sess)
    if err != nil {
        log.Fatal("failed to open stream:", err)
    }

    log.Print("writing")
    if _, err = io.Copy(s, bytes.NewBuffer([]byte("hello, world"))); err != nil {
        log.Fatal("failed to write:", err)
    }
    log.Print("wrote")

    <-ch
}

Running this example will produce the following output:

$ go run cmd/scratch/main.go                                                                                                    (656ms)
2018/05/19 13:09:17 writing
2018/05/19 13:09:17 wrote
2018/05/19 13:09:17 stream accepted
2018/05/19 13:09:47 failed to read from stream:NetworkIdleTimeout: No recent network activity.
exit status 1

changing

buf := bytes.NewBuffer(make([]byte, len("hello, world")))
if _, err = io.Copy(buf, s); err != nil {
    log.Fatal("failed to read from stream:", err)
}
log.Println(buf.String())

to

b := make([]byte, len("hello, world"))
if _, err = s.Read(b); err != nil {
    log.Fatal("failed to read from stream:", err)
}
log.Println(string(b))

seems to fix the issue.

Question:

Why doesn't using io.Copy with a bytes.Buffer work in this case?

  • 写回答

1条回答 默认 最新

  • dtjkl42086 2018-05-19 12:18
    关注

    This isn't related to your use of bytes.Buffer. Rather, io.Copy waits for EOF. Read just reads what's present at the moment. If your stream is never closed, io.Copy will never finish.

    This means that, all else being equal, the timeout will occur regardless of your use of io.Copy or Read--you just may not notice it when using Read, because Read returns immediately, and the timeout only happens 30 seconds later.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题