何时在netty中使用Unpooled(when to use Unpooled in netty)
我想知道何时在Netty中使用Unpooled 。 一些示例显示在
Unpooled.wrappedBuffer
包装了byte [],并且一些示例仅显示ctx.writeAndFlush(bytes); // where bytes is just byte[]
是否有关于何时使用Unpooled的指南?
I'm trying to figure out when to use Unpooled in Netty. Some examples show wrapped the byte[] in
Unpooled.wrappedBuffer
, and some examples show justctx.writeAndFlush(bytes); // where bytes is just byte[]
Is there a guideline on when to use Unpooled?
原文:https://stackoverflow.com/questions/45291211
满意答案
这样写的代码会产生一个死锁。 但是,渠道不一定要关闭。 有多种方法可以解决此问题。
例如,你可以通过以下方法替换for / select循环:
n := len(requests) for r := range ch { processResponse(r) n-- if n == 0 { break } }
这里我们假设潜在的超时在每个goroutine中进行管理。
另一个真正依赖关闭频道的解决方案可以写成如下:
func asyncHttpGets(requests []HttpRequest) { ch := make(chan *HttpResponse) var wg sync.WaitGroup for _, request := range requests { wg.Add(1) go func(r HttpRequest) { defer wg.Done() resp, err := http.Get(r.url) ch <- &HttpResponse{r, resp, err} }(request) } go func() { wg.Wait() close(ch) }() for r := range ch { processResponse(r) } }
请注意,比较初始代码,请求变量不是从goroutine访问的,而是作为参数传递的。 因此,通过通道发布的输出数据结构是一致的。 这是初始代码中的一个问题。 有关此特定主题的更多信息,请访问: https : //github.com/golang/go/wiki/CommonMistakes
另一种解决方案是使用原子计数器计数goroutine中的响应,并在计数器达到极限时明确关闭通道。 但是处理同步/原子常常容易出错,所以在这里可能不是个好主意。
最后,有时您需要获得更多控制权才能正确管理超时,错误等... Tomb包可以帮助您以安全的方式管理goroutines的生命周期。
The code, written like this, will produce a deadlock. But, the channel does not have necessarily to be closed. There are multiple ways to solve this issue.
For instance, you could replace the for/select loop by:
n := len(requests) for r := range ch { processResponse(r) n-- if n == 0 { break } }
Here we assume that the potential timeouts are managed in each goroutine.
Another solution, which really relies on closing the channel could be written as follows:
func asyncHttpGets(requests []HttpRequest) { ch := make(chan *HttpResponse) var wg sync.WaitGroup for _, request := range requests { wg.Add(1) go func(r HttpRequest) { defer wg.Done() resp, err := http.Get(r.url) ch <- &HttpResponse{r, resp, err} }(request) } go func() { wg.Wait() close(ch) }() for r := range ch { processResponse(r) } }
Note that compared the initial code, the request variable is not accessed from the goroutine, but passed as a parameter. The output data structure posted via the channel is therefore consistent. This was an issue in the initial code. See more information about this specific topic at: https://github.com/golang/go/wiki/CommonMistakes
Yet another solution would be to count the responses in the goroutines using an atomic counter, and explicitly close the channel when the counter reaches the limit. But dealing with sync/atomic is often error-prone, so it is probably not a good idea here.
Finally, sometimes you need to get more control in order to properly manage timeouts, errors, etc ... The tomb package can help you to manage the lifecycle of the goroutines in a safe way.
相关问答
更多为什么golang的频道需要一个去程序?(Why does a channel in golang require a go-routine?)
使用Google Channel API关闭频道(Closing Channel with Google Channel API)
golang频道无法使用或发布(golang channel can't consume or publish)
Golang - 为什么要使用完成的频道(Golang - Why use done channel)
Golang频道睡着了(Golang channel is asleep)
GoLang:关闭频道(Golang: Closing a channel)
Golang频道维护(Golang Channel Maintenance)
使用golang频道。(using golang channels. GETTING “all goroutines are asleep - deadlock!”)
Golang goroutine没有使用在频道上使用范围的功能延迟(Golang goroutine not running defer with function that uses range on channel)
golang:如何在所有goroutine完成后关闭频道?(golang: how to close the channel after all goroutines are finished?)
相关文章
更多[Netty 1] 初识Netty
Netty环境配置
Netty开发环境配置
Netty源码分析
storm学习之Netty代替ZMQ
Netty基于流的传输处理
Netty入门实例-时间服务器
Netty入门实例-使用POJO代替ByteBuf
在Twitter,Netty 4 GC开销降为五分之一
Netty入门实例-编写服务器端程序
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!