duangan4070
duangan4070
2018-12-21 20:09

在golang中优先使用goroutine

  • load
  • goroutine
  • server
已采纳

I'm writing a low latency network, high throughput server in golang. It has acceptor in the main goroutine (which accepts incoming connections). And it passes the incoming connections to 'n' number of worker goroutines.

In this case, to not drop connections, I want the acceptor goroutine to be prioritized over other goroutines so that connections won't be dropped. Or atleast this acceptor goroutine should not get evicted by other worker goroutines.

How can I ensure this?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douwa6220 douwa6220 3年前

    I have created threadpools on golang. This should allow easily one to prioritize certain goroutines over others.

    https://github.com/vijayviji/executor

    点赞 评论 复制链接分享
  • doucan4873 doucan4873 3年前

    If you prioritize accepting connections over "doing the work", consider what happens over time. You will create a backlog of work, while accepting more and more connections, bringing even more work to be done. This just makes the load on your server higher.

    Instead, focus on writing your worker code to be very efficient, so that the work will be completed quickly, freeing up resources to handle new connections.

    If you are concerned about your server being unable to handle the workload, you should think about rate-limiting your clients.

    点赞 评论 复制链接分享

为你推荐