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() }
// 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.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 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 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 }