dongmanni6916 2019-01-26 02:43
浏览 109

UDP服务器的联网最佳实践

I'm writing a DNS server in Go to learn how DNS works and how to write a real, potentially useful program in Go.

One of the reasons I chose Go was for its Go routines instead of threads.

Currently, my DNS server doesn't really do much, it sends the same response for every query it receives.

One thing that confuses me is that my DNS server, even with its Go routines and even though it's small and doesn't do much is 10x slower than BIND.

I ran a program called dnsblast to send lots of DNS queries at once and these are my results:

BIND
Sending 10,000 queries = 39,000 pps

My server
Sending 10,000 queries = 3,000 pps

Also, as I increase the number of packets I send per second, the server responds to less and less of the queries.

For example: When sending 1,000 queries, the server responds to 100%, but when sending 10,000 queries the server responds to just 66%.

Is there anything to do with networking in Go that could be limiting the performance of my DNS server? Are there settings in Go I can configure?

Currently, the main program looks like this:

func main() {

    serv, err := net.ListenPacket("udp", ":53")

    if err != nil {
        panic(err)
    }

    defer serv.Close()

    for {
        tmp := make([]byte, 512)
        num_bytes, addr, _ := serv.ReadFrom(tmp)
        go handleQuery(serv, bytes.NewBuffer(tmp[:num_bytes]), addr)
    }

}

This seems to be a pretty standard way of creating a server in Go from what I've read online.

  1. Listen for packets
  2. Save packet data in a buffer
  3. Process each packet using a separate Go routine.

Are there any best practices to improve my server's throughput or does the server look okay and it's just my partial DNS implementation is slow?

Thanks!

  • 写回答

1条回答 默认 最新

  • dsl36367 2019-01-27 00:16
    关注

    Unfortunately Go's UDP support is suboptimal. The implementation allocates memory. What helps a bit is to run the loop in parallel. Increased buffer sizes on OS level limit package loss.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM