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 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 doDecr(id string, signal chan int, client redis.AsyncClient, cnt int) { var fr redis.FutureInt64 key := "ctr-" + id for i := 0; i < cnt; i++ { fr, _ = client.Decr(key) } fr.Get() signal <- 1 }
func doIncr(id string, signal chan int, client redis.AsyncClient, cnt int) { key := "ctr-" + id var fr redis.FutureInt64 for i := 0; i < cnt; i++ { fr, _ = client.Incr(key) } v, _ := fr.Get() if v != int64(cnt) { log.Fatalf("BUG: expecting counter %s to be %d but it is %d\n", key, cnt, v) panic(1) } // debug sanity check // log.Printf("worker[%s] - last INCR result %s=%d\n", id, key, v) signal <- 1 }