GOLANG:为什么在使用 os.File.Fd() 时 SetDeadline/SetReadDeadline/SetWriteDeadline 对文件不起作用?

golang:为什么在使用 os.file.fd() 时 setdeadline/setreaddeadline/setwritedeadline 对文件不起作用?

php小编子墨在这篇文章中将为您解答关于Golang中使用os.File.Fd()时SetDeadline/SetReadDeadline/SetWriteDeadline对文件不起作用的问题。在Golang中,这些方法是用来设置文件的截止时间,但是有时候可能会出现无效的情况。接下来,我们将探讨可能的原因,并提供解决方案来确保这些方法正常工作。

问题内容

我使用 os.File.SetReadDeadline 和 os.File.ReadFull 的组合。但即使使用 SetReadDeadline,我设置的截止日期也被完全忽略,并且 ReadFull 永远阻塞。这是为什么?

其他信息:我向文件触发了一些 IOCTLS,因此需要 os.File.Fd() 来获取文件描述符。

解决方法

tl;博士:

立即学习“go语言免费学习笔记(深入)”;

使用 os.file.fd() 后,在文件上使用 syscall.setnonblock(fd.fd(), true)

这是由于 read 在 golang unix 中:

func (fd *fd) read(p []byte) (int, error) {    if err := fd.readlock(); err != nil {        return 0, err    }    defer fd.readunlock()    if len(p) == 0 {        // if the caller wanted a zero byte read, return immediately        // without trying (but after acquiring the readlock).        // otherwise syscall.read returns 0, nil which looks like        // io.eof.        // todo(bradfitz): make it wait for readability? (issue 15735)        return 0, nil    }    if err := fd.pd.prepareread(fd.isfile); err != nil {        return 0, err    }    if fd.isstream && len(p) > maxrw {        p = p[:maxrw]    }    for {        n, err := ignoringeintrio(syscall.read, fd.sysfd, p)        if err != nil {            n = 0            if err == syscall.eagain && fd.pd.pollable() {                if err = fd.pd.waitread(fd.isfile); err == nil {                    continue                }            }        }        err = fd.eoferror(n, err)        return n, err    }}

登录后复制

如果文件设置为阻塞模式,第一个 n, err := ignoringeintrio(syscall.read, fd.sysfd, p) 将永远阻塞。 waitread 仅当文件以非阻塞模式打开时才会执行。但我确实以非阻塞模式打开了文件,那么发生了什么?

os.file.fd()的实现 破坏了它:

func (f *File) Fd() uintptr {    if f == nil {        return ^(uintptr(0))    }    // If we put the file descriptor into nonblocking mode,    // then set it to blocking mode before we return it,    // because historically we have always returned a descriptor    // opened in blocking mode. The File will continue to work,    // but any blocking operation will tie up a thread.    if f.nonblock {        f.pfd.SetBlocking()    }    return uintptr(f.pfd.Sysfd)}

登录后复制

fd() 始终将文件设置为阻塞。因此,我们必须在等待轮询读取之前撤消该操作。因此:

使用 os.file.fd() 后,在文件上使用 syscall.setnonblock(fd.fd(), true)

以上就是GOLANG:为什么在使用 os.File.Fd() 时 SetDeadline/SetReadDeadline/SetWriteDeadline 对文件不起作用?的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2358272.html

(0)
上一篇 2025年3月1日 16:55:18
下一篇 2025年3月1日 16:55:35

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论