duanmi8349 2016-03-21 10:09
浏览 132

UDP代理,如何维护/重用/清除连接的客户端池?

I am creating a UDP-proxy in go, but while doing some load test using iperf, I start to get this error:

socket: too many open files

After searching and testing, I found that if I create a pool using a map of opening connections being the key *net.UDPAddr.String() and the value an instance of UDP-proxy containing an *net.UDPConn, I am available to reuse existing connection in case the client address is the same:

var clients   map[string]*UDPProxy.UDPProxy = make(map[string]*UDPProxy.UDPProxy) 

This block of code looks something like:

// wait for connections
for {
    n, clientAddr, err := conn.ReadFromUDP(buffer)
    if err != nil {
        log.Println(err)
    }
    counter++
    if *d {
        log.Printf("new connection from %s", clientAddr.String())
    }
    fmt.Printf("Connections: %d, clients: %d
", counter, len(clients))
    proxy, found = clients[clientAddr.String()]
    if !found {
        // make new connection to remote server
        proxy = UDPProxy.New(conn, clientAddr, raddr_udp, *d)
        clients[clientAddr.String()] = proxy
    }
    go proxy.Start(buffer[0:n])
}

This seems to be working, but the problem I have now, is that I need find a way of expiring,cleaning the map when the client exists or is not using any more the proxy so that I could avoid having multiple unused connections.

Any idea how of could I improve this or even better, how could I replace totally the map, I don't know if channels could be help full?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongyao8698 2016-03-21 14:25
    关注

    Since you are creating UDP proxies, you probably know that you have to come up with your own solution for deciding when to "terminate" the proxy session. The session is just an abstraction when it comes to UDP - unless the UDPProxy package you're using has an established mechanism already.

    Depending on why you are creating UDP proxies, it might be easy to arbitrarily cleanup connections ...

    So if you know that a client is exiting, call the Close() method on the proxy (assuming there is one) and use delete on the map entry.

    How to decide that a client is exiting is up to you. Could use a slice as a FIFO, or pick one randomly, or try setting timers for each.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组