本文整理匯總了Golang中github.com/garyburd/redigo/redis.Pool類的典型用法代碼示例。如果您正苦於以下問題:Golang Pool類的具體用法?Golang Pool怎麽用?Golang Pool使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Pool類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ReadMACPayloadTXQueue
// ReadMACPayloadTXQueue reads the full MACPayload tx queue for the given
// device address.
func ReadMACPayloadTXQueue(p *redis.Pool, devAddr lorawan.DevAddr) ([]models.MACPayload, error) {
var out []models.MACPayload
c := p.Get()
defer c.Close()
key := fmt.Sprintf(nodeSessionMACTXQueueTempl, devAddr)
values, err := redis.Values(c.Do("LRANGE", key, 0, -1))
if err != nil {
return nil, fmt.Errorf("get mac-payload from tx queue for devaddr %s error: %s", devAddr, err)
}
for _, value := range values {
b, ok := value.([]byte)
if !ok {
return nil, fmt.Errorf("expected []byte type, got %T", value)
}
var pl models.MACPayload
err = gob.NewDecoder(bytes.NewReader(b)).Decode(&pl)
if err != nil {
return nil, fmt.Errorf("decode mac-payload for devaddr %s error: %s", devAddr, err)
}
out = append(out, pl)
}
return out, nil
}
示例2: getHash
func getHash(pool *redis.Pool, id string) map[string]int {
c := pool.Get()
defer c.Close()
m, err := redis.IntMap(c.Do("HGETALL", id))
utee.Chk(err)
return m
}
示例3: Expired
// Expired implements the `func Expired` defined on the Strategy interface. It
// scans iteratively over the Heart's `location` field to look for items that
// have expired. An item is marked as expired iff the last update time happened
// before the instant of the maxAge subtracted from the current time.
func (s HashExpireyStrategy) Expired(location string,
pool *redis.Pool) (expired []string, err error) {
now := time.Now()
cnx := pool.Get()
defer cnx.Close()
reply, err := redis.StringMap(cnx.Do("HGETALL", location))
if err != nil {
return
}
for id, tick := range reply {
lastUpdate, err := time.Parse(DefaultTimeFormat, tick)
if err != nil {
continue
} else if lastUpdate.Add(s.MaxAge).Before(now) {
expired = append(expired, id)
}
}
return
}
示例4: ReclaimSessionID
func ReclaimSessionID(pool *redis.Pool, sessionID ID, authid string, domain string) error {
conn := pool.Get()
defer conn.Close()
sessionKey := fmt.Sprintf("session:%x", sessionID)
// First, try to claim the session ID. This tells us if it exists or
// safely reserves it if it does not.
reply, err := redis.Int(conn.Do("HSETNX", sessionKey, "domain", domain))
if err != nil {
out.Debug("Redis error on key %s: %v", sessionKey, err)
return err
} else if reply == 1 {
// It did not exist before, but now he owns it.
return nil
}
prevDomain, err := redis.String(conn.Do("HGET", sessionKey, "domain"))
if err != nil {
out.Debug("Redis error on key %s: %v", sessionKey, err)
return err
}
// Ensure that the new agent owns the claimed session ID.
if subdomain(authid, prevDomain) {
return nil
} else {
return fmt.Errorf("Permission denied: %s cannot claim %s", authid, sessionKey)
}
}
示例5: MustFlushRedis
// MustFlushRedis flushes the Redis storage.
func MustFlushRedis(p *redis.Pool) {
c := p.Get()
defer c.Close()
if _, err := c.Do("FLUSHALL"); err != nil {
log.Fatal(err)
}
}
示例6: AddMACPayloadToTXQueue
// AddMACPayloadToTXQueue adds the given payload to the queue of MAC commands
// to send to the node. Note that the queue is bound to the node-session, since
// all mac operations are reset after a re-join of the node.
func AddMACPayloadToTXQueue(p *redis.Pool, pl models.MACPayload) error {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(pl); err != nil {
return fmt.Errorf("gob encode tx mac-payload for node %s error: %s", pl.DevEUI, err)
}
c := p.Get()
defer c.Close()
ns, err := GetNodeSessionByDevEUI(p, pl.DevEUI)
if err != nil {
return fmt.Errorf("get node-session for node %s error: %s", pl.DevEUI, err)
}
exp := int64(common.NodeSessionTTL) / int64(time.Millisecond)
key := fmt.Sprintf(nodeSessionMACTXQueueTempl, ns.DevAddr)
c.Send("MULTI")
c.Send("RPUSH", key, buf.Bytes())
c.Send("PEXPIRE", key, exp)
_, err = c.Do("EXEC")
if err != nil {
return fmt.Errorf("add mac-payload to tx queue for node %s error: %s", pl.DevEUI, err)
}
log.WithFields(log.Fields{
"dev_eui": pl.DevEUI,
"dev_addr": ns.DevAddr,
"reference": pl.Reference,
}).Info("mac-payload added to tx queue")
return nil
}
示例7: deliver
func deliver(pool *redis.Pool, key string, data map[string]string) (err error) {
hkey, err := RandomKey(GOOSE_REDIS_REQ_PREFIX, 16)
if err != nil {
return
}
conn := pool.Get()
defer conn.Close()
// Record the request/match.
for field, val := range data {
if err = conn.Send("HSET", hkey, field, val); err != nil {
return
}
}
// Notify any processes blocking on the associated list.
if err = conn.Send("LPUSH", key, hkey); err != nil {
return
}
// Flush the pipeline.
if err = conn.Flush(); err != nil {
return
}
// Read all of the replies, but just drop them on the ground.
for i := 0; i < len(data)+1; i += 1 {
if _, err = conn.Receive(); err != nil {
return
}
}
return
}
示例8: ProcessNewBlock
func ProcessNewBlock(conf *Config, rpool *redis.Pool, spool *redis.Pool) {
log.Println("ProcessNewBlock startup")
conn := rpool.Get()
defer conn.Close()
psc := redis.PubSubConn{Conn: conn}
psc.Subscribe("btcplex:blocknotify")
for {
switch v := psc.Receive().(type) {
case redis.Message:
hash := string(v.Data)
log.Printf("Processing new block: %v\n", hash)
c := rpool.Get()
newblock, err := SaveBlockFromRPC(conf, spool, hash)
if err != nil {
log.Printf("Error processing new block: %v\n", err)
} else {
// Once the block is processed, we can publish it as btcplex own blocknotify
c.Do("PUBLISH", "btcplex:blocknotify2", hash)
newblockjson, _ := json.Marshal(newblock)
c.Do("PUBLISH", "btcplex:newblock", string(newblockjson))
}
c.Close()
}
}
}
示例9: NewRDBpool
// Redis DB Pool
func NewRDBpool(address string) *RDBpool {
pool := redis.Pool{
MaxActive: 0,
MaxIdle: 3,
Dial: func() (redis.Conn, error) {
c, err := redis.DialTimeout(
"tcp",
address,
time.Duration(1)*time.Second,
time.Duration(1)*time.Second,
time.Duration(1)*time.Second,
)
if err != nil {
return nil, err
}
return c, err
},
}
conn := pool.Get()
defer conn.Close()
if conn.Err() != nil {
panic(fmt.Sprintf("Can not connect to redis %s", address))
}
return &RDBpool{pool: pool}
}
示例10: Deq
func Deq(pool *redis.Pool, latch *utee.Throttle, uid interface{}) ([]byte, error) {
c := pool.Get()
defer c.Close()
defer latch.Release()
for {
name := qname(uid)
k, err := redis.String(c.Do("LPOP", name))
if err != nil && err != redis.ErrNil {
continue
}
if len(k) == 0 {
break
}
b, err := redis.Bytes(c.Do("GET", k))
if err != nil && err != redis.ErrNil {
continue
}
if b != nil {
c.Send("DEL", k)
continue
}
}
i++
if i%10000 == 0 {
log.Println("@success:", i)
}
return nil, nil
}
示例11: instanceIsMaster
func instanceIsMaster(pool *redis.Pool, port string) {
c := pool.Get()
defer c.Close()
for {
master, err := redis.StringMap(c.Do("CONFIG", "GET", "slaveof"))
if err != nil {
// Retry connection to Redis until it is back.
//log.Println(err)
defer c.Close()
time.Sleep(time.Second * time.Duration(connectionLostInterval))
c = pool.Get()
continue
}
for _, value := range master {
if value != "" {
// Instance is now a slave, notify.
if fetchPossible[port] {
c.Do("PUBLISH", "redis-scouter", "stop")
fetchPossible[port] = false
log.Printf("[instance-check-%s] became a slave", port)
}
} else {
// Re-enable metrics.
if !fetchPossible[port] {
fetchPossible[port] = true
log.Printf("[instance-check-%s] became a master", port)
}
}
}
time.Sleep(time.Second * time.Duration(masterCheckInterval))
}
}
示例12: Build
// Fetch Txos and Txins
func (tx *Tx) Build(rpool *redis.Pool) (err error) {
c := rpool.Get()
defer c.Close()
tx.TxIns = []*TxIn{}
tx.TxOuts = []*TxOut{}
txinskeys := []interface{}{}
for i := range iter.N(int(tx.TxInCnt)) {
txinskeys = append(txinskeys, fmt.Sprintf("txi:%v:%v", tx.Hash, i))
}
txinsjson, _ := redis.Strings(c.Do("MGET", txinskeys...))
for _, txinjson := range txinsjson {
ctxi := new(TxIn)
err = json.Unmarshal([]byte(txinjson), ctxi)
tx.TxIns = append(tx.TxIns, ctxi)
}
txoutskeys := []interface{}{}
txoutsspentkeys := []interface{}{}
for i := range iter.N(int(tx.TxOutCnt)) {
txoutskeys = append(txoutskeys, fmt.Sprintf("txo:%v:%v", tx.Hash, i))
txoutsspentkeys = append(txoutsspentkeys, fmt.Sprintf("txo:%v:%v:spent", tx.Hash, i))
}
txoutsjson, _ := redis.Strings(c.Do("MGET", txoutskeys...))
txoutsspentjson, _ := redis.Strings(c.Do("MGET", txoutsspentkeys...))
for txoindex, txoutjson := range txoutsjson {
ctxo := new(TxOut)
err = json.Unmarshal([]byte(txoutjson), ctxo)
if txoutsspentjson[txoindex] != "" {
cspent := new(TxoSpent)
err = json.Unmarshal([]byte(txoutsspentjson[txoindex]), cspent)
ctxo.Spent = cspent
}
tx.TxOuts = append(tx.TxOuts, ctxo)
}
return
}
示例13: AddTXPayloadToQueue
// AddTXPayloadToQueue adds the given TXPayload to the queue.
func AddTXPayloadToQueue(p *redis.Pool, payload models.TXPayload) error {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(payload); err != nil {
return fmt.Errorf("encode tx-payload for node %s error: %s", payload.DevEUI, err)
}
c := p.Get()
defer c.Close()
exp := int64(common.NodeTXPayloadQueueTTL) / int64(time.Millisecond)
key := fmt.Sprintf(nodeTXPayloadQueueTempl, payload.DevEUI)
c.Send("MULTI")
c.Send("LPUSH", key, buf.Bytes())
c.Send("PEXPIRE", key, exp)
_, err := c.Do("EXEC")
if err != nil {
return fmt.Errorf("add tx-payload to queue for node %s error: %s", payload.DevEUI, err)
}
log.WithFields(log.Fields{
"dev_eui": payload.DevEUI,
"reference": payload.Reference,
}).Info("tx-payload added to queue")
return nil
}
示例14: NewTransaction
func NewTransaction(pool *redis.Pool) *Transaction {
t := &Transaction{
conn: pool.Get(),
Actions: make([]*Action, 0),
}
return t
}
示例15: Ping
func (this *testRedis) Ping(pool *redis.Pool) error {
connection := pool.Get()
defer connection.Close()
_, err := connection.Do("PING")
return err
}