douzui6173 2015-08-05 11:09
浏览 49
已采纳

Goroutine I / O调度

Golangs goroutines present an interface of blocking I/O to the goroutine (-programmer). Behind the scenes the runtime naturally uses some kind of non-blocking I/O to prevent the OS from suspending the OS-thread, so that the runtime can run another goroutine on top of the OS thread while the I/O is performed.

When does the runtime consider the I/O performed so that it can reschedule the goroutine?

To make it clear, assuming I have a net.TCPConn that I call Write on, when can I expect the goroutine to be rescheduled?

conn, err := net.Dial("tcp", serverAddr)
conn.Write(buffer)
timestamp = time.Now()

That is when can I expect the timestamp to be taken?

  • When the buffer has been copied to the golang runtime?
  • When the buffer has been copied to the runtime and to the OS's kernel space?
  • When the buffer has been copied to the runtime, kernel space and additionally to the NIC's send buffer?
  • When the buffer has been sent over the network/from the NIC?
  • When the buffer has been acknowledged by the recieve ends TCP stack?
  • 写回答

2条回答 默认 最新

  • douhuangjian9627 2015-08-05 12:51
    关注

    You can have a look in file https://github.com/golang/go/blob/master/src/net/fd_unix.go (Write function).

    Basically, it depends whether the socket buffer has enough space or not.

    If there is enough space in the socket buffer to accommodate the size of your write operation, the data will be immediately written to the socket buffer. I guess this corresponds to your second answer. Additionally, the kernel may actually send the packet (or add it to the NIC queues), but it is independent from the Go runtime.

    If there is not enough space in the socket buffer to accommodate the whole write operation, only part of the data will be immediately written to the socket buffer. Then, the call will block (via the runtime polling engine) until the kernel has made some space in the socket buffer (by sending some packets). As soon as some space is available, and all the data have been copied, the call will unblock.

    You should consider the timestamp is taken when the net package has written the whole buffer in the socket buffer via a system call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA