memcached 显然正在重置连接。

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

memcached apparently resetting connections

问题

更新:

不是memcached的问题,而是有很多处于TIME_WAIT状态的套接字:

% ss -s
总计:2494(内核 2784)
TCP:43323(已建立 2314,已关闭 40983,孤立 0,synrecv 0,timewait 40982/0),端口 16756

顺便说一下,我已经修改了以前的版本(如下所示),使用了Brad Fitz的memcache客户端,并重用了相同的memcache连接:

http://dpaste.com/1387307/

旧版本:

我用Go语言编写了一个最基本的Web服务器,其中处理程序函数只做一件事:

  • 从memcached中检索一个键
  • 将其作为HTTP响应发送给客户端

以下是代码:http://dpaste.com/1386559/

问题是我在memcached上遇到了很多连接重置:

2013/09/18 20:20:11 http: panic serving [::1]:19990: dial tcp 127.0.0.1:11211: connection reset by peer
goroutine 20995 [running]:
net/http.func·007()
/usr/local/go/src/pkg/net/http/server.go:1022 +0xac
main.maybe_panic(0xc200d2e570, 0xc2014ebd80)
/root/go/src/http_server.go:19 +0x4d
main.get_memc_val(0x615200, 0x7, 0x60b5c0, 0x6, 0x42ee58, ...)
/root/go/src/http_server.go:25 +0x64
main.func·001(0xc200149b40, 0xc2017b3380, 0xc201888b60)
/root/go/src/http_server.go:41 +0x35
net/http.HandlerFunc.ServeHTTP(0x65e950, 0xc200149b40, 0xc2017b3380, 0xc201888b60)
/usr/local/go/src/pkg/net/http/server.go:1149 +0x3e
net/http.serverHandler.ServeHTTP(0xc200095410, 0xc200149b40, 0xc2017b3380, 0xc201888b60)
/usr/local/go/src/pkg/net/http/server.go:1517 +0x16c
net/http.(*conn).serve(0xc201b9b2d0)
/usr/local/go/src/pkg/net/http/server.go:1096 +0x765
created by net/http.(*Server).Serve
/usr/local/go/src/pkg/net/http/server.go:1564 +0x266

我已经注意设置了Linux内核网络,以便不会妨碍(关闭SYN洪泛保护等)。

...
...
然而,在使用"ab"进行测试时,我仍然遇到了这些错误。

ab -c 1000 -n 50000 "http://localhost:8000/"

无论我在哪里查看,都没有任何迹象表明是内核的问题(dmesg,/var/log)。

英文:

UPDATE:

It's not memcached, it's a lot of sockets in TIME_WAIT state:

% ss -s
Total: 2494 (kernel 2784)
TCP:   43323 (estab 2314, closed 40983, orphaned 0, synrecv 0, timewait 40982/0), ports 16756

BTW, I have modified previous version (below) to use Brad Fitz's memcache client and to reuse the same memcache connection:

http://dpaste.com/1387307/

OLD VERSION:

I have thrown together the most basic webserver in Go that has handler function doing only one thing:

  • retrieving a key from memcached
  • sending it as http response to client

Here's the code: http://dpaste.com/1386559/

The problem is I'm getting a lot of connection resets on memcached:

2013/09/18 20:20:11 http: panic serving [::1]:19990: dial tcp 127.0.0.1:11211: connection reset by peer
goroutine 20995 [running]:
net/http.func·007()
		/usr/local/go/src/pkg/net/http/server.go:1022 +0xac
main.maybe_panic(0xc200d2e570, 0xc2014ebd80)
		/root/go/src/http_server.go:19 +0x4d
main.get_memc_val(0x615200, 0x7, 0x60b5c0, 0x6, 0x42ee58, ...)
		/root/go/src/http_server.go:25 +0x64
main.func·001(0xc200149b40, 0xc2017b3380, 0xc201888b60)
		/root/go/src/http_server.go:41 +0x35
net/http.HandlerFunc.ServeHTTP(0x65e950, 0xc200149b40, 0xc2017b3380, 0xc201888b60)
		/usr/local/go/src/pkg/net/http/server.go:1149 +0x3e
net/http.serverHandler.ServeHTTP(0xc200095410, 0xc200149b40, 0xc2017b3380, 0xc201888b60)
		/usr/local/go/src/pkg/net/http/server.go:1517 +0x16c
net/http.(*conn).serve(0xc201b9b2d0)
		/usr/local/go/src/pkg/net/http/server.go:1096 +0x765
created by net/http.(*Server).Serve
		/usr/local/go/src/pkg/net/http/server.go:1564 +0x266

I have taken care to set Linux kernel networking in such way as not to get in the way (turning off SYN flooding protection etc).

...
...
And yet on testing with "ab" (below) I'm getting those errors.

ab -c 1000 -n 50000 "http://localhost:8000/"

There is no sign whatsoever anywhere I looked that it's the kernel (dmesg, /var/log).

答案1

得分: 1

我猜这是因为你的套接字用完了——你在这里没有关闭memc。在程序运行时,使用netstat进行检查。

func get_memc_val(k string) []byte {
    memc, err := gomemcache.Connect(mc_ip, mc_port)
    maybe_panic(err)
    val, _, _ := memc.Get(k)
    return val
}

如果我是你,我会使用这个go memcache接口,它是由memcached的作者编写的,他现在在Google负责Go相关的工作。

英文:

I would guess that is because you are running out of sockets - you never close the memc here. Check with netstat while your program is running.

func get_memc_val(k string) []byte {
	memc, err := gomemcache.Connect(mc_ip, mc_port)
	maybe_panic(err)
	val, _, _ := memc.Get(k)
	return val
}

I'd use this go memcache interface if I were you - it was written by the author of memcached who now works for Google on Go related things.

答案2

得分: 1

尝试使用YBC库中的memcache客户端。与gomemcache不同,它只打开并重复使用少量与memcache服务器的连接,而不管通过客户端发出的并发请求的数量。通过在少量打开的连接上进行并发请求的流水线处理,它实现了高性能。

可以通过ClientConfig.ConnectionsCount来配置与memcache服务器的连接数。

英文:

Try <a href="https://github.com/valyala/ybc/tree/master/libs/go/memcache">memcache client from YBC library</a>. Unlike <a href="https://github.com/bradfitz/gomemcache">gomemcache</a> it opens and re-uses only a few connections to memcache server irregardless of the number of concurrent requests issued via the client. It achieves high performance by pipelining concurrent requests over a small number of open connections to the memcache server.

The number of connections to the memcache server can configured via <a href="http://godoc.org/github.com/valyala/ybc/libs/go/memcache#ClientConfig">ClientConfig.ConnectionsCount</a>.

huangapple
  • 本文由 发表于 2013年9月19日 02:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/18879747.html
匿名

发表评论

匿名网友

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

确定