// Reflect over the methods defined in redis.Client // and send back as []string (tolowercase) // TOOD get rid of redundant code in switch // (REVU: needs minor update to core code) func getDefinedMethods(ctype clientType) (map[string]string, *error_) { var mmap = map[string]string{} spec := redis.DefaultSpec().Db(13).Password("go-redis") var e redis.Error var client interface{} switch ctype { case sync: client, e = redis.NewSynchClientWithSpec(spec) case async: client, e = redis.NewAsynchClientWithSpec(spec) } if e != nil { log.Println("ignoring - ", e) } if client == nil { return mmap, &error_{"client is nil", nil} } else { defer client.(redis.RedisClient).Quit() } tc := reflect.TypeOf(client) nm := tc.NumMethod() for i := 0; i < nm; i++ { m := tc.Method(i) mname := strings.ToLower(m.Name) mmap[mname] = mname } return mmap, nil }
func spamChat1(chanKey string) { fmt.Println("spamchat being called") spec := redis.DefaultSpec().Password("go-redis") client, e := redis.NewAsynchClientWithSpec(spec) if e != nil { fmt.Print("Error creating spam client for: ", e) } defer client.Quit() for i := 0; i < 1000000; i++ { var fr redis.FutureInt64 //var fr2 redis.FutureBool var buf bytes.Buffer buf.Write([]byte("hello" + string(i))) bt := buf.Bytes() fr, e = client.Publish("chat", bt) //fmt.Println("we got past 2 publishes") if e != nil { fmt.Println("error in publishing: ", e) } _, e = client.Rpush("chatlog", bt) if e != nil { fmt.Println("error in storing list: ", e) } numRecieved, _ := fr.Get() //fmt.Println(numRecieved) //fr2.Get() } }
func NewAsyncClientWithSpec(t *testing.T, spec *redis.ConnectionSpec) redis.AsyncClient { client, err := redis.NewAsynchClientWithSpec(getTestConnSpec()) if err != nil { t.Fatalf("NewAsynchClientWithSpec - ", err) } return client }
// Use a single redis.AsyncClient with specified number // of workers to bench concurrent load on the async client func benchTask(taskspec taskSpec, iterations int, workers int, printReport bool) (delta int64, err os.Error) { signal := make(chan int, workers) // Buffering optional but sensible. spec := redis.DefaultSpec().Db(13).Password("go-redis") client, e := redis.NewAsynchClientWithSpec(spec) if e != nil { log.Println("Error creating client for worker: ", e) return -1, e } // defer client.Quit() // will be deprecated soon defer client.RedisClient().Quit() t0 := time.Nanoseconds() for i := 0; i < workers; i++ { id := fmt.Sprintf("%d", i) go taskspec.task(id, signal, client, iterations) } for i := 0; i < workers; i++ { <-signal } delta = time.Nanoseconds() - t0 // for i := 0; i < workers; i++ { // clients[i].Quit() // } // if printReport { report("concurrent "+taskspec.name, delta, iterations*workers) } return }
func asyncPublish(spec *redis.ConnectionSpec, channel string) { client, e := redis.NewAsynchClientWithSpec(spec) if e != nil { log.Println("failed to create the async client", e) return } // ref will ultimately point to the last future returned from Publish() var rcvCntFuture redis.FutureInt64 for i := 0; i < 100; i++ { msg := []byte(fmt.Sprintf("this is message # %d (using async client)!", i)) var err redis.Error // publish the message and don't wait for the future // we only care if an error was raised on publish rcvCntFuture, err = client.Publish(channel, msg) if err != nil { fmt.Printf("Error on Publish - %s", err) } } // ok, now let's wait until the last publish's future is done // before quiting. rcvCnt, fe := rcvCntFuture.Get() if fe != nil { fmt.Printf("Error on future get - %s\n", fe) return } else { fmt.Printf("(LAST) Message sent to %d subscribers\n", rcvCnt) } client.Quit() }
func NewRedisAsyncClient() redis.AsyncClient { spec := redis.DefaultSpec().Db(0).Host("10.174.178.235") client, err := redis.NewAsynchClientWithSpec(spec) if err != nil { panic(err) } return client }
// Check that connection is actually passing passwords from spec // and catching AUTH ERRs. func TestAsyncClientConnectWithBadSpec(t *testing.T) { spec := getTestConnSpec() spec.Password("bad-password") client, expected := redis.NewAsynchClientWithSpec(spec) if expected == nil { t.Error("BUG: Expected a RedisError") } if client != nil { t.Error("BUG: async client reference on error MUST be nil") } }
func main() { runtime.GOMAXPROCS(16) key = "user_data_2" spec := redis.DefaultSpec().Db(0).Host("10.174.178.235") client, err := redis.NewAsynchClientWithSpec(spec) if err != nil { panic(err) } primeKey(key, client) defer client.Quit() handler := func(w http.ResponseWriter, r *http.Request) { responseHandler(w, r, client) } http.HandleFunc("/", handler) http.ListenAndServe(":80", nil) }
func main() { runtime.GOMAXPROCS(16) key = "user_data_2" spec := redis.DefaultSpec().Db(0).Host("10.174.178.235") primeClient, err := redis.NewAsynchClientWithSpec(spec) if err != nil { panic(err) } primeKey(key, primeClient) log.Println("Key primed and ready") defer primeClient.Quit() pool = NewClientPool(100) handler := func(w http.ResponseWriter, r *http.Request) { responseHandler(w, r) } http.HandleFunc("/", handler) http.ListenAndServe(":80", nil) }
// Check that connection is actually passing passwords from spec func TestAsyncClientConnectWithSpec(t *testing.T) { spec := getTestConnSpec() client, err := redis.NewAsynchClientWithSpec(spec) if err != nil { t.Fatalf("failed to create client with spec. Error: %s ", err) } else if client == nil { t.Fatal("BUG: client is nil") } // quit once -- OK futureBool, err := client.Quit() if err != nil { t.Errorf("BUG - initial Quit on asyncClient should not return error - %s ", err) } if futureBool == nil { t.Errorf("BUG - non-error asyncClient response should not return nil future") } // block until we get results ok, fe := futureBool.Get() if fe != nil { t.Errorf("BUG - non-Error Quit future result get must never return error - got: %s", fe) } if !ok { t.Errorf("BUG - non-Error Quit future result must always be true ") } // subsequent quit should raise error for i := 0; i < 10; i++ { futureBool, err = client.Quit() if err == nil { t.Errorf("BUG - Quit on shutdown asyncClient should return error") } if futureBool != nil { t.Errorf("BUG - Quit on shutdown asyncClient should not return future. got: %s", futureBool) } } }
// Use a single redis.AsyncClient with specified number // of workers to bench concurrent load on the async client func benchTask(taskspec taskSpec, iterations int, workers int, printReport bool) (delta time.Duration, err error) { // channel to signal completion signal := make(chan int, workers) // spec and initialize an AsyncClient // will flush your db13 as noted in README .. spec := redis.DefaultSpec().Db(13).Password("go-redis") client, e := redis.NewAsynchClientWithSpec(spec) if e != nil { log.Println("Error creating client for worker: ", e) return -1, e } defer client.Quit() // panics setup(client) t0 := time.Now() for i := 0; i < workers; i++ { id := fmt.Sprintf("%d", i) go taskspec.task(id, signal, client, iterations) } // wait for completion for i := 0; i < workers; i++ { <-signal } delta = time.Now().Sub(t0) if printReport { report(taskspec.name, workers, delta, iterations*workers) } return }