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;博士:
使用 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 对文件不起作用?的详细内容。更多信息请关注PHP中文网其他相关文章!

goisidealforbeginnersandsubableforforcloudnetworkservicesduetoitssimplicity,效率和concurrencyFeatures.1)installgromtheofficialwebsitealwebsiteandverifywith'.2)

Goexcelsinproductionduetoitsperformanceandsimplicity,butrequirescarefulmanagementofscalability,errorhandling,andresources.1)DockerusesGoforefficientcontainermanagementthroughgoroutines.2)UberscalesmicroserviceswithGo,facingchallengesinservicemanageme

我们需要自定义错误类型,因为标准错误接口提供的信息有限,自定义类型能添加更多上下文和结构化信息。1)自定义错误类型能包含错误代码、位置、上下文数据等,2)提高调试效率和用户体验,3)但需注意其复杂性和维护成本。

goisidealforbuildingscalablesystemsduetoitssimplicity,效率和建筑物内currencysupport.1)go'scleansyntaxandaxandaxandaxandMinimalisticDesignenhanceProductivityAndRedCoductivityAndRedCuceErr.2)ItSgoroutinesAndInesAndInesAndInesAndineSandChannelsEnablenableNablenableNableNablenableFifficConcurrentscorncurrentprogragrammentworking torkermenticmminging

Initfunctionsingorunautomationbeforemain()andareusefulforsettingupenvorments和InitializingVariables.usethemforsimpletasks,避免使用辅助效果,andbecautiouswithTestingTestingTestingAndLoggingTomaintAnainCodeCodeCodeClarityAndTestesto。

goinitializespackagesintheordertheordertheyimported,thenexecutesInitFunctionswithinApcageIntheirdeFinityOrder,andfilenamesdetermineTheOrderAcractacractacrosmultiplefiles.thisprocessCanbeCanbeinepessCanbeInfleccessByendercrededBydeccredByDependenciesbetenciesbetencemendencenciesbetnependendpackages,whermayleLeadtocomplexinitialitialializizesizization

CustomInterfacesingoarecrucialforwritingFlexible,可维护,andTestableCode.TheyEnableDevelostOverostOcusonBehaviorBeiroveration,增强ModularityAndRobustness.byDefiningMethodSigntulSignatulSigntulSignTypaterSignTyperesthattypesmustemmustemmustemmustemplement,InterfaceSallowForCodeRepodEreusaperia

使用接口进行模拟和测试的原因是:接口允许定义合同而不指定实现方式,使得测试更加隔离和易于维护。1)接口的隐式实现使创建模拟对象变得简单,这些对象在测试中可以替代真实实现。2)使用接口可以轻松地在单元测试中替换服务的真实实现,降低测试复杂性和时间。3)接口提供的灵活性使得可以为不同测试用例更改模拟行为。4)接口有助于从一开始就设计可测试的代码,提高代码的模块化和可维护性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器