I'm pretty new to Go and very new to Memcached, which is probably a bad combination for using gomemcache, but I couldn't find any info regarding my problem in the docs.
I start Memcached with Docker like this: docker run -it --rm -p 11211:11211 memcached
Then I run this code:
package main
import (
"fmt"
"strconv"
"sync"
"github.com/bradfitz/gomemcache/memcache"
)
func main() {
mc := memcache.New("localhost:11211")
// This one works
mySet(mc, -1)
// See:
myGet(mc, -1)
goroutineCount := 100
waitGroup := sync.WaitGroup{}
waitGroup.Add(goroutineCount)
for i := 0; i < goroutineCount; i++ {
go func(i int) {
// These lead to errors
mySet(mc, i)
//waitGroup.Done()
}(i)
}
waitGroup.Wait()
}
func mySet(mc *memcache.Client, i int) {
item := memcache.Item{
Key: strconv.Itoa(i),
Value: []byte("foo"),
}
err := mc.Set(&item)
if err != nil {
fmt.Println(err)
}
}
func myGet(mc *memcache.Client, i int) {
item, err := mc.Get(strconv.Itoa(i))
if err != nil {
fmt.Println(err)
return
}
fmt.Println("value: " + string(item.Value))
}
waitGroup.Done() is commented out because that leads to more output (?). It's just for reproducing the behavior, so it doesn't matter that the program doesn't finish this way.
So the first Set() and Get() call work, which indicate that I properly created the client and that the server is running fine. But as soon as the calls take place concurrently in multiple goroutines, I get errors.
The server is still working fine. When I restart the program, the first Set() and Get() still work.
I'm pretty new to Go and very new to Memcached, which is probably a bad combination for using
gomemcache
, but I couldn't find any info regarding my problem in the docs.I start Memcached with Docker like this:
docker run -it --rm -p 11211:11211 memcached
Then I run this code:
waitGroup.Done()
is commented out because that leads to more output (?). It's just for reproducing the behavior, so it doesn't matter that the program doesn't finish this way.This is what I get:
So the first
Set()
andGet()
call work, which indicate that I properly created the client and that the server is running fine. But as soon as the calls take place concurrently in multiple goroutines, I get errors.The server is still working fine. When I restart the program, the first
Set()
andGet()
still work.