func (n *node) getResponseTime() float64 { f, err := redis.Float64(do("GET", fmt.Sprintf("nodes.%s.avg", n.name))) if err != nil { logrus.Error(err) } return f }
func updateServerLoad(pool *redis.Pool, serverID string) { tock := time.Duration((common.SERVER_UPDATE_LOAD_SECONDS * 0.95) * float64(time.Second)) c := time.Tick(tock) //want it to run SLIGHTLY before the key expires conn := pool.Get() for _ = range c { //inf for loop fmt.Println("Setting server load...") if _, err := conn.Do("PEXPIRE", "ASMS::server:"+serverID+"::load:", common.SERVER_UPDATE_LOAD_SECONDS*1000); err != nil { panic(err) } //end if numberOfDevices, err := redis.Float64(conn.Do("LLEN", "ASMS::server:"+serverID+"::deviceIDs:")) if err != nil { panic(err) //could run into an issue here as this might not be set. Might need to set it to zero. } // no issue it seems, redis returns a 0 if the list key doesnt exist. currentLoad := numberOfDevices * common.MAX_DEVICES_LOAD_DENOMINATOR // % out of 100 //current load if _, err := conn.Do("SET", "ASMS::server:"+serverID+"::load:", currentLoad); err != nil { panic(err) } //end if fmt.Println("Done setting server load...") } //end for loop } //end UpdateServerLoad
func (n *node) getFill() float64 { f, err := redis.Float64(do("GET", fmt.Sprintf("nodes.%s.fill", n.name))) if err != nil { logrus.Error(err) } return f }
func pipelineScore(conn redis.Conn, keyMembers []common.KeyMember) (map[common.KeyMember]Presence, error) { for _, keyMember := range keyMembers { if err := conn.Send("ZSCORE", keyMember.Key+insertSuffix, keyMember.Member); err != nil { return map[common.KeyMember]Presence{}, err } if err := conn.Send("ZSCORE", keyMember.Key+deleteSuffix, keyMember.Member); err != nil { return map[common.KeyMember]Presence{}, err } } if err := conn.Flush(); err != nil { return map[common.KeyMember]Presence{}, err } m := map[common.KeyMember]Presence{} for i := 0; i < len(keyMembers); i++ { insertReply, insertErr := conn.Receive() insertValue, insertErr := redis.Float64(insertReply, insertErr) deleteReply, deleteErr := conn.Receive() deleteValue, deleteErr := redis.Float64(deleteReply, deleteErr) switch { case insertErr == nil && deleteErr == redis.ErrNil: m[keyMembers[i]] = Presence{ Present: true, Inserted: true, Score: insertValue, } case insertErr == redis.ErrNil && deleteErr == nil: m[keyMembers[i]] = Presence{ Present: true, Inserted: false, Score: deleteValue, } case insertErr == redis.ErrNil && deleteErr == redis.ErrNil: m[keyMembers[i]] = Presence{ Present: false, } default: return map[common.KeyMember]Presence{}, fmt.Errorf( "pipelineScore bad state for %v (%v/%v)", keyMembers[i], insertErr, deleteErr, ) } } return m, nil }
//when a device requests something from the tracker this is what responds func handleReply(pool *redis.Pool, zmpREP *zmq.Socket) { for { //endless loop req, err := zmpREP.Recv() if err != nil { fmt.Println(err) } //end if var rMsg string //the return message if len(req) == 1 { //the message is only 1 part long. msg := string(req[0]) if msg == "serverLoads" { //if they sent the right message to the tracker conn := pool.Get() //get a connection from the pool defer conn.Close() //get the list of server loads //// Start getting the server loads from redis listLength, err := redis.Int(conn.Do("LLEN", "ASMS::connected::serverID:")) if err != nil { fmt.Println(err) } sIDs, err := redis.Strings(conn.Do("LRANGE", "ASMS::connected::serverID:", 0, listLength)) if err != nil { fmt.Println(err) } serverLoads := []*common.Server{} for i := range sIDs { load, err := redis.Float64(conn.Do("GET", "ASMS::server:"+sIDs[i]+"::load:")) if err != nil { fmt.Println(err) } serverLoads = append(serverLoads, &common.Server{ServerID: sIDs[i], ServerLoad: common.Load(load)}) //add the server to the list, assert the load as common.Load } sort.Sort(common.ByLoad{serverLoads}) //sort the loads of the servers //translate the serverID to the servers IP serverIP, err := redis.String(conn.Do("GET", "ASMS::serverID:"+serverLoads[0].ServerID+"::IP:")) if err != nil { fmt.Println(err) } rMsg = serverIP //set rMsg equal to the serverID ip with the smallest load } else { rMsg = "0" // we will program this in as being unknown message recieved } //end if err = zmpREP.Send([][]byte{ //send the response back []byte(rMsg), }) if err != nil { //check for an error panic(err) } //end if } //end if } //end for } //end handleReply
func (db *Database) incrementCounterInternal(key string, delta float64, t time.Time) (float64, error) { // nt = n0 * math.Exp(-lambda * t) // lambda = math.Ln2 / thalf c := db.Pool.Get() defer c.Close() const lambda = math.Ln2 / float64(counterHalflife) scaledTime := lambda * float64(t.Sub(time.Unix(1257894000, 0))) return redis.Float64(incrementCounterScript.Do(c, key, delta, scaledTime, (4*counterHalflife)/time.Second)) }
// Test ZINCRBY func TestSortedSetIncrby(t *testing.T) { s, err := Run() ok(t, err) defer s.Close() c, err := redis.Dial("tcp", s.Addr()) ok(t, err) // Normal cases { // New key b, err := redis.Float64(c.Do("ZINCRBY", "z", 1, "member")) ok(t, err) equals(t, 1.0, b) // Existing key b, err = redis.Float64(c.Do("ZINCRBY", "z", 2.5, "member")) ok(t, err) equals(t, 3.5, b) // New member b, err = redis.Float64(c.Do("ZINCRBY", "z", 1, "othermember")) ok(t, err) equals(t, 1.0, b) } // Error cases { _, err = redis.String(c.Do("ZINCRBY")) assert(t, err != nil, "ZINCRBY error") _, err = redis.String(c.Do("ZINCRBY", "set")) assert(t, err != nil, "ZINCRBY error") _, err = redis.String(c.Do("ZINCRBY", "set", "nofloat", "a")) assert(t, err != nil, "ZINCRBY error") _, err = redis.String(c.Do("ZINCRBY", "set", 1.0, "too", "many")) assert(t, err != nil, "ZINCRBY error") // Wrong type of key s.Set("str", "value") _, err = c.Do("ZINCRBY", "str", 1.0, "member") assert(t, err != nil, "ZINCRBY error") } }
// Test ZSCORE func TestSortedSetScore(t *testing.T) { s, err := Run() ok(t, err) defer s.Close() c, err := redis.Dial("tcp", s.Addr()) ok(t, err) s.ZAdd("z", 1, "one") s.ZAdd("z", 2, "two") s.ZAdd("z", 2, "zwei") // Simple case { b, err := redis.Float64(c.Do("ZSCORE", "z", "two")) ok(t, err) equals(t, 2.0, b) } // no such member { b, err := c.Do("ZSCORE", "z", "nosuch") ok(t, err) equals(t, nil, b) } // no such key { b, err := c.Do("ZSCORE", "nosuch", "nosuch") ok(t, err) equals(t, nil, b) } // Direct { s.ZAdd("z2", 1, "one") s.ZAdd("z2", 2, "two") score, err := s.ZScore("z2", "two") ok(t, err) equals(t, 2.0, score) } // Error cases { _, err = redis.String(c.Do("ZSCORE")) assert(t, err != nil, "ZSCORE error") _, err = redis.String(c.Do("ZSCORE", "key")) assert(t, err != nil, "ZSCORE error") _, err = redis.String(c.Do("ZSCORE", "too", "many", "arguments")) assert(t, err != nil, "ZSCORE error") // Wrong type of key s.Set("str", "value") _, err = redis.Int(c.Do("ZSCORE", "str", "aap")) assert(t, err != nil, "ZSCORE error") } }
func TestPopular(t *testing.T) { db := newDB(t) defer closeDB(db) c := db.Pool.Get() defer c.Close() // Add scores for packages. On each iteration, add half-life to time and // divide the score by two. All packages should have the same score. now := time.Now() score := float64(4048) for id := 12; id >= 0; id-- { path := "github.com/user/repo/p" + strconv.Itoa(id) c.Do("SET", "id:"+path, id) _, err := incrementPopularScore.Do(c, path, score, scaledTime(now)) if err != nil { t.Fatal(err) } now = now.Add(popularHalfLife) score /= 2 } values, _ := redis.Values(c.Do("ZRANGE", "popular", "0", "100000", "WITHSCORES")) if len(values) != 26 { t.Errorf("Expected 26 values, got %d", len(values)) } // Check for equal scores. score, err := redis.Float64(values[1], nil) if err != nil { t.Fatal(err) } for i := 3; i < len(values); i += 2 { s, _ := redis.Float64(values[i], nil) if math.Abs(score-s)/score > 0.0001 { t.Errorf("Bad score, score[1]=%g, score[%d]=%g", score, i, s) } } }
// SortedSetIncrBy increments the value of a member // in a sorted set // // This function tries to return last floating value of the item, // if it fails to parse reply to float64, returns parsing error along with // Reply it self func (r *RedisSession) SortedSetIncrBy(key string, incrBy, item interface{}) (float64, error) { prefixed := make([]interface{}, 0) // add key prefixed = append(prefixed, r.AddPrefix(key)) // add incrBy prefixed = append(prefixed, incrBy) // add item prefixed = append(prefixed, item) return redis.Float64(r.Do("ZINCRBY", prefixed...)) }
func TestAddJobToSetScript(t *testing.T) { testingSetUp() defer testingTeardown() // Create a test job jobs, err := createAndSaveTestJobs(1) if err != nil { t.Fatalf("Unexpected error in createAndSaveTestJobs: %s", err.Error()) } job := jobs[0] // Add the job to the time index with a score of 7 days ago tx := newTransaction() expectedScore := float64(time.Now().Add(-7 * 24 * time.Hour).UTC().UnixNano()) tx.addJobToSet(job, Keys.JobsTimeIndex, expectedScore) if err := tx.exec(); err != nil { t.Errorf("Unexpected err in tx.exec(): %s", err.Error()) } // Make sure the job was added to the set properly conn := redisPool.Get() defer conn.Close() score, err := redis.Float64(conn.Do("ZSCORE", Keys.JobsTimeIndex, job.id)) if err != nil { t.Errorf("Unexpected error in ZSCORE: %s", err.Error()) } if score != expectedScore { t.Errorf("Score in time index set was incorrect. Expected %f but got %f", expectedScore, score) } // Destroy the job and make sure the script does not add it to a new set if err := job.Destroy(); err != nil { t.Errorf("Unexpected err in job.Destroy: %s", err.Error()) } tx = newTransaction() tx.addJobToSet(job, "fooSet", 42.0) if err := tx.exec(); err != nil { t.Errorf("Unexpected err in tx.exec(): %s", err.Error()) } exists, err := redis.Bool(conn.Do("EXISTS", "fooSet")) if err != nil { t.Errorf("Unexpected err in EXISTS: %s", err.Error()) } if exists { t.Error("Expected fooSet to not exist after the job was destroyed but it did.") } }
func (s *SortedSetType) Score(member string) float64 { reply, err := s.client.do("ZSCORE", s.Key, member) score, _ := redis.Float64(reply, err) return score }
ve(redis.Strings(nil, nil)), ve([]string(nil), redis.ErrNil), }, { "values([v1, v2])", ve(redis.Values([]interface{}{[]byte("v1"), []byte("v2")}, nil)), ve([]interface{}{[]byte("v1"), []byte("v2")}, nil), }, { "values(nil)", ve(redis.Values(nil, nil)), ve([]interface{}(nil), redis.ErrNil), }, { "float64(1.0)", ve(redis.Float64([]byte("1.0"), nil)), ve(float64(1.0), nil), }, { "float64(nil)", ve(redis.Float64(nil, nil)), ve(float64(0.0), redis.ErrNil), }, } func TestReply(t *testing.T) { for _, rt := range replyTests { if rt.actual.err != rt.expected.err { t.Errorf("%s returned err %v, want %v", rt.name, rt.actual.err, rt.expected.err) continue }
// SortedSetScore returns score of a member in a sorted set. If no member, // an error is returned. // // See: http://redis.io/commands/zscore func (r *RedisSession) SortedSetScore(key string, member interface{}) (float64, error) { return redis.Float64(r.Do("ZSCORE", r.AddPrefix(key), member)) }
func GetCoverage(c redis.Conn, repo string) (float64, error) { return redis.Float64(c.Do("GET", repo+".coverage")) }
func (this *redisStorage) GetFloat64(key string) (float64, error) { conn := this._pool.Get() src, err := redis.Float64(conn.Do("GET", key)) conn.Close() return src, err }
func (v *RedisValue) Float64() (float64, error) { return redis.Float64(v.Raw, v.Err) }
func TestHashIncrbyfloat(t *testing.T) { s, err := Run() ok(t, err) defer s.Close() c, err := redis.Dial("tcp", s.Addr()) ok(t, err) // Existing key { s.HSet("hash", "field", "12") v, err := redis.Float64(c.Do("HINCRBYFLOAT", "hash", "field", "400.12")) ok(t, err) equals(t, 412.12, v) equals(t, "412.12", s.HGet("hash", "field")) } // Existing key, not a number { s.HSet("hash", "field", "noint") _, err := redis.Float64(c.Do("HINCRBYFLOAT", "hash", "field", "400")) assert(t, err != nil, "do HINCRBYFLOAT error") } // New key { v, err := redis.Float64(c.Do("HINCRBYFLOAT", "hash", "newfield", "40.33")) ok(t, err) equals(t, 40.33, v) equals(t, "40.33", s.HGet("hash", "newfield")) } // Direct usage { s.HSet("hash", "field", "500.1") f, err := s.HIncrfloat("hash", "field", 12) ok(t, err) equals(t, 512.1, f) equals(t, "512.1", s.HGet("hash", "field")) } // Wrong type of existing key { s.Set("wrong", "type") _, err := redis.Int(c.Do("HINCRBYFLOAT", "wrong", "type", "400")) assert(t, err != nil, "do HINCRBYFLOAT error") } // Wrong usage { _, err := redis.Int(c.Do("HINCRBYFLOAT")) assert(t, err != nil, "do HINCRBYFLOAT error") _, err = redis.Int(c.Do("HINCRBYFLOAT", "wrong")) assert(t, err != nil, "do HINCRBYFLOAT error") _, err = redis.Int(c.Do("HINCRBYFLOAT", "wrong", "value")) assert(t, err != nil, "do HINCRBYFLOAT error") _, err = redis.Int(c.Do("HINCRBYFLOAT", "wrong", "value", "noint")) assert(t, err != nil, "do HINCRBYFLOAT error") _, err = redis.Int(c.Do("HINCRBYFLOAT", "foo", "bar", 12, "tomanye")) assert(t, err != nil, "do HINCRBYFLOAT error") } }
return } type RedisTaskConfigQuery struct { Command string `json:"command"` Args []string `json:"args"` Type string `json:"type"` } var ( redisunderstands = map[string]func(interface{}, error) (interface{}, error){ "bool": func(r interface{}, e error) (interface{}, error) { return redis.Bool(r, e) }, "float64": func(r interface{}, e error) (interface{}, error) { return redis.Float64(r, e) }, "int": func(r interface{}, e error) (interface{}, error) { return redis.Int(r, e) }, "int64": func(r interface{}, e error) (interface{}, error) { return redis.Int64(r, e) }, "ints": func(r interface{}, e error) (interface{}, error) { return redis.Ints(r, e) }, "string": func(r interface{}, e error) (interface{}, error) { return redis.String(r, e) }, "strings": func(r interface{}, e error) (interface{}, error) { return redis.Strings(r, e)
func TestIncrbyfloat(t *testing.T) { s, err := Run() ok(t, err) defer s.Close() c, err := redis.Dial("tcp", s.Addr()) ok(t, err) // Existing key { s.Set("foo", "12") v, err := redis.Float64(c.Do("INCRBYFLOAT", "foo", "400.12")) ok(t, err) equals(t, 412.12, v) s.CheckGet(t, "foo", "412.12") } // Existing key, not a number { s.Set("foo", "noint") _, err := redis.Float64(c.Do("INCRBYFLOAT", "foo", "400")) assert(t, err != nil, "do INCRBYFLOAT error") } // New key { v, err := redis.Float64(c.Do("INCRBYFLOAT", "bar", "40.33")) ok(t, err) equals(t, 40.33, v) s.CheckGet(t, "bar", "40.33") } // Direct usage { s.Set("foo", "500.1") f, err := s.Incrfloat("foo", 12) ok(t, err) equals(t, 512.1, f) s.CheckGet(t, "foo", "512.1") s.HSet("wrong", "aap", "noot") _, err = s.Incrfloat("wrong", 12) assert(t, err != nil, "do s.Incrfloat() error") } // Wrong type of existing key { s.HSet("wrong", "aap", "noot") _, err := redis.Int(c.Do("INCRBYFLOAT", "wrong", "400")) assert(t, err != nil, "do INCRBYFLOAT error") } // Amount not a number { _, err := redis.Int(c.Do("INCRBYFLOAT", "key", "noint")) assert(t, err != nil, "do INCRBYFLOAT error") } // Wrong usage { _, err := redis.Int(c.Do("INCRBYFLOAT")) assert(t, err != nil, "do INCRBYFLOAT error") _, err = redis.Int(c.Do("INCRBYFLOAT", "another", "new", "key")) assert(t, err != nil, "do INCRBYFLOAT error") } }
func (*RedisStore) Float64(reply interface{}, err error) (float64, error) { return redis.Float64(reply, err) }