(websocket)Golang同步数据锁定失败 – 管道中断

huangapple go评论83阅读模式
英文:

(websocket) Golang failed syncerous data locking - Broken Pipe

问题

我目前正在尝试编写一个简单的 WebSocket,当有人加入或离开 WebSocket 连接池时,它会将所有客户端列表列出给每个连接的客户端。

目前,我使用 (R)Lock 和 unlock 来确保存在并发连接,以避免连接之间的任何干扰。
每当我尝试访问连接池时,我都会对其进行锁定,这包括对池进行读写操作,但出现奇怪的情况是,只有当我通过一次性发送 100 个并发连接来暴力测试 WebSocket 时,当我结束所有连接时,会出现 Broken Pipe 错误。

从外观上看,错误发生在删除客户端并广播新客户端列表之后。

你能找出为什么在有人失去连接时无法将连接池发送给每个客户端的原因吗?请记住,只有当我通过创建 100 个连接的 for 循环并在结束连接列表时才会出现这种情况。

我在出错的地方添加了一个注释。


另外一个注意事项是,如果你们有更好的方法来发送连接池,而不是通过循环遍历并将 UUID 存储到字符串数组中,请随时告诉我,但现在我主要关注调试这个问题,因为我想找出我失败的地方。

编辑:忘记添加源代码:

WebSocket 源代码:https://gist.github.com/anonymous/eaaf2e5430ed694bc424

压力测试源代码:https://gist.github.com/anonymous/92ad79ffee1afdfd3382

英文:

I'm currently trying to write up a simple websocket which lists all the client lists to each connected client when somebody joins or leaves the websocket connection pool.

Currently I'm using the (R)Lock and unlock to make sure that there is a concurrent connection going on to avoid any interference between the connections.
Whenever I try to access the connection pool I lock it, this includes both reading and writing to the pool but for some strange reason ONLY when I bruteforce the websocket by sending 100 concurrent connections at once and I end them all I get a Broken Pipe Error.

From the looks of it the error occurs right after removing the client and broadcasting the new client list.

Could you figure out why it's failing to send the connection pool to each client when somebody looses a connection? Keep in mind that this only happens when I bruteforce the websocket connection by having a for loop which creates 100 connections and when ending the connection list it would fail.

I added a note where it fails.


Another side note is if you guys have a better way to send the connection pool other then by looping through it and storing the UUID into a string array then feel free to tell me about it aswell but right now I'm mainly focusing on debugging this problem as I'd like to figure out where I'm failing.

Edit: Forgot to add source:

Websocket source: https://gist.github.com/anonymous/eaaf2e5430ed694bc424

Stress Test source: https://gist.github.com/anonymous/92ad79ffee1afdfd3382

答案1

得分: 3

所以事实证明,异常似乎只在 broadcastMessage 函数中捕获时才成为问题(请参考 websocket 源代码)。

正如你所看到的,我在 broadcastMessage 函数中捕获了 WriteMessage 的错误。
不太确定为什么在不捕获时它不是问题,但我会提出另一个问题来解决这个问题。

无论如何,感谢那些选择阅读我遇到的问题的人!

对于那些感兴趣的人,这是帖子的链接:https://stackoverflow.com/questions/26235760/golang-websocket-broken-pipe-error-only-when-catching-sending-message-to-clien

英文:

So it turns out that the exception only seems to be a problem somehow when catching it in broadcastMessage (Refer to the websocket source).

As you can see, I catched the error of WriteMessage in the broadcastMessage function.
Not entirely sure why it isn't a problem when not catching it but I'll create another question about it.

Thanks anyway to those who chose to read my the problem I was having!

For those who are interested, here is the post https://stackoverflow.com/questions/26235760/golang-websocket-broken-pipe-error-only-when-catching-sending-message-to-clien

huangapple
  • 本文由 发表于 2014年10月7日 17:50:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/26233237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定