Go: http.Server 连接池

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

Go: http.Server connection pool

问题

我知道Go语言中的http.Client在Transport类型中有连接池,但是http.Server中没有Transport类型。我希望能够访问服务器的连接池。

  1. 列出所有连接
  2. 关闭连接/从连接池中移除连接

更新

我正在学习Go的源代码
https://code.google.com/p/go/source/browse/src/pkg/net/http/server.go#1087

看起来这个函数表明没有连接池。

也许有人知道如何覆盖那个函数吗?
我需要创建一个完全复制该包的副本吗?

英文:

I know http.Client in Go language has connection pool in type Transport

But there is no Transport in http.Server

I wish to access server's pool.

  1. List all connections
  2. Close connections/remove them from pool

Update

I am learning Go source code
https://code.google.com/p/go/source/browse/src/pkg/net/http/server.go#1087

It seems that this function has the answer that there is no Pool.

May be someone knows how to override that function?
Do I have to create a hole copy of package?

答案1

得分: 3

我在go-nuts邮件列表上得到了答案。

https://groups.google.com/forum/#!topic/golang-nuts/eoBsx0Sl3Co

  1. Transport在服务器端不可用(仅在客户端可用)。
  2. 我可以传递自己的net.Listener。
  3. LimitListener是其中一种实现。
    https://code.google.com/p/go/source/browse/netutil/listen.go?repo=net
英文:

I got answer on go-nuts mailing list.

https://groups.google.com/forum/#!topic/golang-nuts/eoBsx0Sl3Co

  1. Transport is not available in server (only in client)
  2. I can pass my own net.Listener
  3. LimitListener is one of such implementations.
    https://code.google.com/p/go/source/browse/netutil/listen.go?repo=net

答案2

得分: 1

编辑:请注意,Transport结构对应的是一个HTTP客户端,而不是HTTP服务器

Transport结构位于此处:https://code.google.com/p/go/source/browse/src/pkg/net/http/transport.go

在该结构中,有几个感兴趣的成员:

idleConn
idleConnCh
reqConn

我认为严格来说这就是你所询问的内容。

然而,这些成员并没有被导出,很明显作者并不打算让其他包对其进行修改。

我建议你看一下CloseIdleConnectionsCancelRequest这些导出的函数,它们似乎是处理连接池的一部分,也许可以解决你的部分问题。

否则,你基本上面临的是“它并不设计用于那样的操作”。显然(也许有人会纠正我,但我还没有看到如何做到),没有办法以一种方式扩展内置类型,让你访问仅在其包内可见的成员(即以小写字母开头的成员)。这意味着你无法在不修改http包源代码(或复制并制作自己的包)的情况下访问Transport结构的这些成员。

英文:

EDIT: Note that the Transport struct corresponds to an http client, not an http server

The Transport struct lives here: https://code.google.com/p/go/source/browse/src/pkg/net/http/transport.go

In that struct there are a few members of interest:

idleConn
idleConnCh
reqConn

I think strictly speaking this is what you are asking about.

However those are not exported and it's pretty clear the author did not intend those to be modified by from other packages.

I would suggest having a look at CloseIdleConnections, CancelRequest - those seem to be the exported functions that deal with the connection pool, maybe that solves part of your problem.

Otherwise, you're basically up against "it wasn't designed to do that". There is apparently (someone might come along and correct me on this, but I haven't see how) no way to extend built-in types in a fashion that lets you access members which are only visible within it's package (i.e. members that start with a lower case letter). Meaning you can't access those members of the Transport struct without modifying the source of the http package (or copying it and making your own).

huangapple
  • 本文由 发表于 2013年10月4日 21:24:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/19182367.html
匿名

发表评论

匿名网友

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

确定