Esempio n. 1
0
// Sum sends a SUM request to the sumr database and returns the value underlying key, or zero otherwise
func (cli *Client) Sum(key sumr.Key) (result float64) {
	cli.lmtr.Open()
	defer cli.lmtr.Close()

	cli.lk.Lock()
	server := cli.metric.Nearest(xor.Key(key), 1)[0].(*shard).Server
	cli.lk.Unlock()

	// Recover from dead shard panic
	defer func() {
		if err := recover(); err != nil {
			log.Printf("dead shard: %s", err)
			result = math.NaN()
		}
	}()

	retrn := server.Call("Sum", key)
	return retrn[0].(float64)
}
Esempio n. 2
0
// Add sends an ADD request to the database to add value to key; if key does not exist, it is created with the given value.
// updateTime is the application-level timestamp of this request.
// Add returns the value of the key after the update.
func (cli *Client) Add(updateTime time.Time, key sumr.Key, value float64) (result float64) {

	// Per-client rate-limiting
	cli.lmtr.Open()
	defer cli.lmtr.Close()

	cli.lk.Lock()
	server := cli.metric.Nearest(xor.Key(key), 1)[0].(*shard).Server
	cli.lk.Unlock()

	// Recover from dead shard panic
	defer func() {
		if err := recover(); err != nil {
			log.Printf("dead shard: %s", err)
			// XXX: Take a more comprehensive action here
			result = math.NaN()
		}
	}()

	retrn := server.Call("Add", updateTime, key, value)
	return retrn[0].(float64)
}