gomemcache icon indicating copy to clipboard operation
gomemcache copied to clipboard

Connections are attempted on idle connections that have already been closed

Open joel0 opened this issue 2 years ago • 2 comments

I'm attempting to query memcache on localhost every 10 minutes. The first Get("key") works fine, but the one 10 minutes later results in an error. The cycle repeats, so every other query succeeds. tcpdump shows that the memcache server attempted to close the TCP connection after about 30 seconds.

The errors I get are either of the following.

panic: write tcp 127.0.0.1:13590->127.0.0.1:11211: write: broken pipe
EOF

I've found I can work around this problem by calling Ping() and ignoring the returned error immediately before any Get() call.

I've boiled down a minimal repro of the problem I'm facing.

package main

import (
        "github.com/bradfitz/gomemcache/memcache"
        "fmt"
        "time"
)

func query(mc *memcache.Client) {
        // This Ping() call mitigates the problem
        // err := mc.Ping()
        // fmt.Printf("Ping(): %v\n", err)

        r, err := mc.Get("key")
        if err != nil {
                panic(err)
        }
        fmt.Printf("%v\n", string(r.Value))
}

func main() {
        mc := memcache.New("127.0.0.1:11211")

        query(mc)

        fmt.Printf("Sleeping...\n")
        time.Sleep(35 * time.Second)

        // This second query call results in an error from mc.Get("key")
        query(mc)
}

joel0 avatar Apr 07 '22 23:04 joel0

I've tried your main and the Get works fine also after 35 seconds. I'm using a MacBook M1 with Go1.18 and Memcached v1.6.15 installed with brew install memcached without touching any config file.

pioz avatar Apr 29 '22 06:04 pioz

I later discovered that the memcache server I am connecting to is built inhouse. I think the difference is that Memcached is leaving idle connections open, but the memcache server I'm using closes the TCP connection after 30 second of idle.

Since I see no Memcached config parameter for idle connection timeout, I'm not sure how to provide a case you can reproduce this issue yourself.

joel0 avatar Apr 29 '22 12:04 joel0