dongxun7962
2014-09-15 14:55
浏览 1.0k
已采纳

Golang tcp套接字读取最终使EOF

I have problem reading from socket. There is asterisk instance running with plenty of calls (10-60 in a minute) and I'm trying to read and process CDR events related to those calls (connected to AMI).

Here is library which I'm using (not mine, but was pushed to fork because of bugs) https://github.com/warik/gami

Its pretty straightforward, main action goes in gami.go - readDispatcher.

buf := make([]byte, _READ_BUF)    // read buffer
    for {
        rc, err := (*a.conn).Read(buf)

So, there is TCPConn (a.conn) and buffer with size 1024 to which I'm reading messages from socket. So far so good, but eventually, from time to time (this time may vary from 10 minutes to 5 hours independently of data amount which comes through socket) Read operation fails with io.EOF error. I was trying to reconnect and relogin immediately, but its also impossible - connection times out, so i was pushed to wait for about 40-60sec, and this time is very crucial to me, I'm losing a lot of data because of delay. I was googling, reading sources and trying a lot of stuff - nothing. The most strange thing, that simple socket opened in python or php does not fail.

  • Is it possible that problem because of lack of file descriptors to represent socket on mine machine or on asterisk server?
  • Is it possible that problem in asterisk configuration (because i have another asterisk on which this problem doesn't reproduce, but also, i have time less calls on last one)?
  • Is it possible that problem in my way to deal with socket connection or with Go in general?

go version go1.2.1 linux/amd64

asterisk 1.8

图片转代码服务由CSDN问答提供 功能建议

我从套接字读取时遇到问题。 有一个星号实例正在运行大量呼叫(每分钟10-60个),我正在尝试读取和处理与这些呼叫相关的CDR事件(已连接到AMI)。

此处 是我正在使用的库(不是我的库,但是由于错误而被迫分叉了) https://github.com / warik / gami

非常简单,主要动作在gami.go中-readDispatcher。

  buf:=  make([] byte,_READ_BUF)//读取缓冲区
,用于{
 rc,err:=(* a.conn).Read(buf)
   
 
 

因此,有TCPConn(a.conn)和大小为1024的缓冲区,我正在从套接字读取消息。 到目前为止还算不错,但最终还是会不时地发生(此时间可能会从10分钟到5个小时不等,与通过套接字传来的数据量无关)读取操作失败,并出现io.EOF错误。 我试图立即重新连接并重新登录,但是它也是不可能的-连接超时,所以我被迫等待大约40-60秒,这对我来说非常关键,由于延迟,我丢失了很多数据 。 我正在谷歌搜索,阅读资料并尝试了很多东西-什么也没有。 最奇怪的是,在python或php中打开的简单套接字不会失败。

  • 是否可能由于缺少用于在我的机器上表示套接字的文件描述符而导致该问题 还是在星号服务器上?
  • 在星号配置中是否可能出现此问题(因为我在该星号上没有重现此问题的另一个星号,而且我在最后一个星号上的通话时间减少了)?
  • 用我的方式来处理套接字连接还是一般而言,使用Go可以解决这个问题吗?

    go版本go1.2.1 linux / amd64

    星号1.8

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongxi3859 2014-09-16 14:49
    已采纳

    Ok, problem was in OS socket buffer overflow. As appeared there were to much data to handle.

    So, were are three possible ways to fix this:

    • increase socket buffer volume
    • increase somehow speed of process which reeds data from socket
    • lower data volume or frequency

    The thing that gami is by default reading all data from asterisk. And i was reading all of them and filter them after actual read operation. According that AMI listening application were running on pretty poor PC it appeared that it simply cannot read all the data before buffer capacity will be exposed.But its possible to receive only particular events, by sending "Events" action to AMI and specifying desired "EventMask". So, my decision was to do that. And create different connections for different events type.

    打赏 评论
  • dougaopu7938 2014-09-15 19:38

    Update to latest asterisk. There was bug like that when AMI send alot of data.

    For check issue, you have send via ami command like "COMMAND sip show peers"(or any other long output command) and see result.

    打赏 评论

相关推荐 更多相似问题