Exemplo n.º 1
0
func putPageWithoutLock(w http.ResponseWriter, r *http.Request, u string, pageToLock string) {
	alreadyLocking, err := redis.Bool(rdo("EXISTS", "LOCK:"+u))
	if err != nil {
		log.Println("Unable to verify if user already has a page locked")
		fmt.Fprint(w, `{"editable":false, "reason":"Unable to verify if the page is locked"}`)
	} else if alreadyLocking {
		lockedPage, err := redis.String(rdo("GET", "LOCK:"+u))
		if err != nil {
			log.Println("Unable to get locked page for user:"******"editable":false, "reason":"Unable to locate the page currently locked by the user"}`)
		} else {
			if lockedPage == pageToLock {
				fmt.Fprint(w, `{"editable":true}`)
			} else {
				fmt.Fprint(w, `{"editable":false, "reason":"Your lock is being used on: `+lockedPage+`"}`)
			}
		}
	} else {
		lockObtained, err := redis.Bool(rdo("SETNX", "PAGE:"+pageToLock, u))
		if err != nil {
			log.Println("Failed to get lock on page where lock does not exist:", pageToLock, err)
			fmt.Fprint(w, `{"editable":false, "reason":"Failed to obtain lock on page"}`)
		} else {
			if lockObtained {
				rdo("SET", "LOCK:"+u, pageToLock)
				rdo("EXPIRE", "PAGE:"+pageToLock, pageLockTTL)
				rdo("EXPIRE", "LOCK:"+u, pageLockTTL)
				fmt.Fprint(w, `{"editable":true}`)
			} else {
				fmt.Fprint(w, `{"editable":false, "reason":"Unable to obtain lock on page"}`)
			}
		}
	}
}
Exemplo n.º 2
0
func TestRemoveRedisRecords(t *testing.T) {
	conn, clean := getConn()
	defer clean()

	// clean
	defer func() {
		conn.Do("FLUSHALL")
		conn.Close()
	}()

	_, err := conn.Do("SET", "abc", "123")
	check(err)
	_, err = conn.Do("SET", "a:b:c", "123")
	check(err)

	// remove file
	err = os.Remove(filepath("abc"))
	check(err)
	_, err = os.Stat(filepath("abc"))
	assert.True(t, os.IsNotExist(err))
	exist, err := redis.Bool(conn.Do("EXISTS", "abc"))
	assert.False(t, exist)

	// remove dir
	err = os.RemoveAll(filepath("a"))
	check(err)
	_, err = os.Stat(filepath("a"))
	check(err)
	exist, err = redis.Bool(conn.Do("EXISTS", "a:b:c"))
	assert.False(t, exist)
}
Exemplo n.º 3
0
func putPage(w http.ResponseWriter, r *http.Request, u string) {
	userExists, err := redis.Bool(rdo("EXISTS", "USER:"******"Attempt to edit with a non-existent account:", u, err)
		fmt.Fprint(w, `{"editable":false}`)
	} else {
		if userExists {
			setCookie(w, r, map[string]string{"u": u})
			pageToLock := r.URL.Path
			lockExists, err := redis.Bool(rdo("EXISTS", "PAGE:"+pageToLock))
			if err != nil {
				log.Println("Unable to test existence of lock for page:", pageToLock, err)
				fmt.Fprint(w, `{"editable":true}`)
			} else if lockExists {
				lockingUser, err := redis.String(rdo("GET", "PAGE:"+pageToLock))
				if err != nil {
					log.Println("Unable to get user for locked page:", pageToLock, err)
					fmt.Fprint(w, `{"editable":false, "reason":"Page is already locked by another user"}`)
				} else {
					if lockingUser == u {
						fmt.Fprint(w, `{"editable":true}`)
					} else {
						fmt.Fprint(w, `{"editable":false, "reason":"Page is currently being edited by someone else"}`)
					}
				}
			} else {
				putPageWithoutLock(w, r, u, pageToLock)
			}
		} else {
			log.Println("Attempt to edit with a user that doesn't exist:", u)
			fmt.Fprint(w, `{"editable":false}`)
		}
	}
}
Exemplo n.º 4
0
// QueueDocsetJob will queue a job to build a docset for an artifact, if there
// is not yet one built.
func QueueDocsetJob(groupId, artifactId string, version string) error {
	var redisConn redis.Conn = redisconn.Get()

	id := groupId + ":" + artifactId

	exists, err := redis.Bool(redisConn.Do("SISMEMBER", "docsets", id))
	if err != nil {
		return err
	}
	if exists == true && version != "" {
		verExists, err := redis.Bool(redisConn.Do("SISMEMBER", "docset:"+id, version))
		if err != nil || verExists {
			return err
		}
	} else if exists == true {
		return nil
	}

	if err := QueueJob(map[string]string{
		"Job":        "build-docset",
		"ArtifactId": artifactId,
		"GroupId":    groupId,
		"Version":    version,
	}); err != nil {
		return err
	}

	return nil
}
Exemplo n.º 5
0
func Test_MigrateAllKeysWithAPrefix(t *testing.T) {
	ClearRedis()

	config = Config{
		Source:  sourceServer.url,
		Dest:    destServer.url,
		Workers: 1,
		Batch:   10,
		Prefix:  "bar",
	}

	for i := 0; i < 100; i++ {
		key := fmt.Sprintf("bar:%d", i)
		sourceServer.conn.Do("SET", key, i)
	}

	sourceServer.conn.Do("SET", "baz:foo", "yolo")

	RunAction(migrateKeys)

	for i := 0; i < 100; i++ {
		key := fmt.Sprintf("bar:%d", i)
		exists, _ := redis.Bool(destServer.conn.Do("EXISTS", key))

		if !exists {
			t.Errorf("Could not find a key %d that should have been migrated", key)
		}
	}

	exists, _ := redis.Bool(destServer.conn.Do("EXISTS", "baz:foo"))

	if exists {
		t.Errorf("Found a key %s that should not have been migrated", "baz:foo")
	}
}
Exemplo n.º 6
0
func article_vote(user, article string, vt VTYPE) {
	now := time.Now()
	cutoff := now.Unix() - ONE_WEEK_IN_SECONDS
	t, _ := redis.Int64(c.Do("ZSCORE", "time:", article))
	if t < cutoff {
		return
	}
	id := getID(article)
	switch vt {
	case UPVOTE:
		//
		bm, _ := redis.Int(c.Do("SMOVE", "voted_down", "voted_up", user))
		if bm != 1 {
			//first vote
			b, _ := redis.Bool(c.Do("SADD", fmt.Sprintf("voted_up:%s", id), user))
			if b {
				c.Do("ZINCRBY", "score:", VOTE_SCORE, article)
				c.Do("HINCRBY", article, "votes", 1)
			} else {
				//already upvoted
				//cancel vote
				c.Do("ZINCRBY", "score:", -VOTE_SCORE, article)
				c.Do("HINCRBY", article, "votes", -1)
				c.Do("SREM", fmt.Sprintf("voted_up:%s", id), user)
			}
		} else {
			//switch from downvote to upvote
			c.Do("ZINCRBY", "score:", VOTE_SCORE, article)
			c.Do("HINCRBY", article, "votes", 1)
		}
	case DOWNVOTE:
		bm, _ := redis.Int(c.Do("SMOVE", "voted_up", "voted_down", user))
		if bm != 1 {
			//first vote
			b, _ := redis.Bool(c.Do("SADD", fmt.Sprintf("voted_down:%s", id), user))
			if b {
				c.Do("ZINCRBY", "score:", -VOTE_SCORE, article)
				c.Do("HINCRBY", article, "votes", -1)
			} else {
				//already downvoted
				//cancel vote
				c.Do("ZINCRBY", "score:", VOTE_SCORE, article)
				c.Do("HINCRBY", article, "votes", 1)
				//remove
				c.Do("SREM", fmt.Sprintf("voted_down:%s", id), user)
			}
		} else {
			//switch from upvote to downvote
			c.Do("ZINCRBY", "score:", -VOTE_SCORE, article)
			c.Do("HINCRBY", article, "votes", -1)
		}
	}

}
Exemplo n.º 7
0
func autoEnabled(conn redis.Conn, host, path string) (en bool, err error) {
	en, err = redis.Bool(conn.Do("GET", fmt.Sprintf(keyEnabled, host, path)))
	if err == redis.ErrNil {
		en, err = redis.Bool(conn.Do("SISMEMBER", keyAutoEnable, host))
		if err != nil {
			return
		}
		if en {
			conn.Do("SET", fmt.Sprintf(keyEnabled, host, path), "true")
		}
	}
	return
}
Exemplo n.º 8
0
func getSchedule(username string) string {
	schedule := ""

	client := getClient()
	defer client.Close()

	days := []string{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}

	everyWeek := []string{}
	thisWeek := []string{}
	notThisWeek := []string{}
	for _, day := range days {
		readableDay := []rune(day)
		readableDay[0] = unicode.ToUpper(readableDay[0])

		if remote, _ := redis.Bool(client.Do("SISMEMBER", "remote.every."+day, username)); remote {
			everyWeek = append(everyWeek, string(readableDay))
		}

		if remote, _ := redis.Bool(client.Do("SISMEMBER", "remote.this."+day, username)); remote {
			thisWeek = append(thisWeek, string(readableDay))
		}

		if notRemote, _ := redis.Bool(client.Do("SISMEMBER", "not.remote.this."+day, username)); notRemote {
			notThisWeek = append(notThisWeek, string(readableDay))
		}
	}

	if len(everyWeek) > 0 {
		schedule += "You are remote every " + strings.Join(everyWeek, ", ") + ".\n"
	}

	if len(thisWeek) > 0 {
		if len(everyWeek) > 0 {
			schedule += "In addition to your normal schedule, you are also remote this "
		} else {
			schedule += "You are remote this "
		}
		schedule += strings.Join(thisWeek, ", ") + ".\n"
	}

	if len(notThisWeek) > 0 {
		schedule += "You are not remote this " + strings.Join(notThisWeek, ", ") + ".\n"
	}

	if len(everyWeek) == 0 && len(thisWeek) == 0 {
		schedule += "You have no remote days scheduled.\n"
	}

	return schedule
}
Exemplo n.º 9
0
// Intended usage
func TestRedisSync_IntendedUsage(t *testing.T) {
	_, err := pool.Get().Do("FLUSHDB")
	if err != nil {
		t.Fatal("Error flushing database")
	}
	rs := &RedisSync{Key: "special flower", Pool: pool, ErrChan: make(chan error, 1), Timeout: 1 * time.Second}

	rs.Lock()
	err = <-rs.ErrChan
	if err != nil {
		t.Fatal("failed to obtain lock", err)
	}
	exists, err := redis.Bool(pool.Get().Do("EXISTS", "special flower.lock"))
	if err != nil {
		t.Fatal("eror checking if lock key exists")
	}
	if exists == false {
		t.Fatal("lock key doesn't exist after lock")
	}

	hasLock := rs.HasLock()
	err = <-rs.ErrChan
	if err != nil {
		t.Fatal("failed to check lock", rs.Key, err)
	}
	if !hasLock {
		t.Fatal("HasLock should return true")
	}

	rs.Unlock()
	err = <-rs.ErrChan
	if err != nil {
		t.Fatal("failed to unlock key", rs.Key, err)
	}
	exists, err = redis.Bool(pool.Get().Do("EXISTS", "special flower.lock"))
	if err != nil {
		t.Fatal("eror checking if lock key exists", err)
	}
	if exists == true {
		t.Fatal("lock key still exist after unlock")
	}

	hasLock = rs.HasLock()
	err = <-rs.ErrChan
	if err != nil {
		t.Fatal("failed to check lock", rs.Key, err)
	}
	if hasLock {
		t.Fatal("HasLock should return false")
	}
}
Exemplo n.º 10
0
// Stat ensures that the digest is a member of the specified repository and
// forwards the descriptor request to the global blob store. If the media type
// differs for the repository, we override it.
func (rsrbds *repositoryScopedRedisBlobDescriptorService) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
	if err := dgst.Validate(); err != nil {
		return distribution.Descriptor{}, err
	}

	conn := rsrbds.upstream.pool.Get()
	defer conn.Close()

	// Check membership to repository first
	member, err := redis.Bool(conn.Do("SISMEMBER", rsrbds.repositoryBlobSetKey(rsrbds.repo), dgst))
	if err != nil {
		return distribution.Descriptor{}, err
	}

	if !member {
		return distribution.Descriptor{}, distribution.ErrBlobUnknown
	}

	upstream, err := rsrbds.upstream.stat(ctx, conn, dgst)
	if err != nil {
		return distribution.Descriptor{}, err
	}

	// We allow a per repository mediatype, let's look it up here.
	mediatype, err := redis.String(conn.Do("HGET", rsrbds.blobDescriptorHashKey(dgst), "mediatype"))
	if err != nil {
		return distribution.Descriptor{}, err
	}

	if mediatype != "" {
		upstream.MediaType = mediatype
	}

	return upstream, nil
}
Exemplo n.º 11
0
Arquivo: job.go Projeto: h12w/redisq
func (p *JobQ) resume() error {
	c := p.pool.Get()
	defer c.Close()
	cntKey := p.Name + ":consumer:count"
	exists, err := redis.Bool(c.Do("EXISTS", cntKey))
	if err != nil {
		return err
	}
	if exists {
		cnt, err := redis.Int(c.Do("GET", cntKey))
		if err != nil {
			return err
		}
		for i := 0; i < cnt; i++ {
			n, err := p.consumers[i].Len()
			if err != nil {
				return err
			}
			for j := 0; j < n; j++ {
				if err := p.consumers[i].PopTo(p.inputQ, nil); err != nil {
					return err
				}
			}
		}
	}
	_, err = c.Do("SET", cntKey, len(p.consumers))
	return err
}
Exemplo n.º 12
0
func CreateUser(email, password string) (*User, error) {
	rd := pool.Get()
	defer rd.Close()

	exists, err := redis.Bool(rd.Do("EXISTS", Config.UserNamespace+email))

	if exists {
		return nil, fmt.Errorf("that account is already registered")
	} else if err != nil {
		return nil, err
	}

	// Create a token
	hash, err := abdi.Hash(password)
	if err != nil {
		return nil, err
	}
	user := User{
		Email:      email,
		HashedPass: hash,
	}

	data, err := json.Marshal(user)
	if err != nil {
		return nil, err
	}

	_, err = rd.Do("SET", Config.UserNamespace+email, data)
	if err != nil {
		return nil, err
	}
	return &user, nil
}
Exemplo n.º 13
0
func (c *CacheRedis) GetBool(key string) bool {
	val, err := redis.Bool(c.conn.Do("GET", key))
	if err != nil {
		return false
	}
	return val
}
Exemplo n.º 14
0
// Return a single random Fortune, from a random module
func RandomFortune(mod string) (*Fortune, error) {
	conn := Pool.Get()
	defer conn.Close()

	// ensure the specified module exists
	if mod != "" {
		member, err := redis.Bool(conn.Do("SISMEMBER", MODS_KEY, mod))
		if err != nil {
			return nil, err
		}
		if member == false {
			return nil, errors.New(fmt.Sprintf("module '%s' not found", mod))
		}
	}

	if mod == "" {
		mod2, err := redis.String(conn.Do("SRANDMEMBER", MODS_KEY))
		if err != nil {
			return nil, err
		}
		mod = mod2
	}

	fid, err := redis.Int(conn.Do("SRANDMEMBER", modKey(mod)))
	if err != nil {
		return nil, err
	}

	text, err := redis.String(conn.Do("GET", fortuneKey(fid)))
	if err != nil {
		return nil, err
	}

	return &Fortune{mod: mod, id: fid, text: text}, nil
}
Exemplo n.º 15
0
func saveComment(conn redis.Conn, req *commentSubmitRequest) (id int64, err error) {
	for {
		id = time.Now().Unix()
		var added bool
		added, err = redis.Bool(conn.Do("ZADD", fmt.Sprintf(keyAll, req.host, req.path), id, id))
		if err != nil {
			log.Println(err)
			return
		}
		if added {
			break
		}
		time.Sleep(time.Second)
	}
	var ok string
	ok, err = redis.String(conn.Do("HMSET", redis.Args{}.
		Add(fmt.Sprintf(keyComment, req.host, req.path, id)).
		AddFlat(req)...))
	if err != nil {
		return
	}
	if ok != "OK" {
		log.Println("Unexpected return value from HMSET: %q\n", ok)
	}
	return
}
Exemplo n.º 16
0
// Work runs an infinite loop, watching its database for new requests, starting job as requested,
// moving stream data back and forth, and updating job status as it changes.
func (w *Worker) Work() error {
	conn := w.pool.Get()
	defer conn.Close()
	for {
		Debugf("Waiting for job")
		// Get the list of current jobs
		// Wait for next start event
		vals, err := redis.Values(conn.Do("BLPOP", w.KeyPath("start"), "0"))
		if err != nil {
			return err
		}
		var id string
		if _, err := redis.Scan(vals[1:], &id); err != nil {
			return err
		}
		Debugf("Received instruction to start job %s", id)
		// Acquire lock on the job
		acquired, err := redis.Bool(conn.Do("SETNX", w.KeyPath(id), "me"))
		if err != nil {
			return err
		}
		Debugf("Acquiring lock for job %s... -> %s", id, acquired)
		// FIXME: set a dead man's switch with TTL & a periodic refresh
		if acquired {
			Debugf("Spawning goroutine for job %s", id)
			go func(id string) {
				if err := w.startJob(id); err != nil {
					fmt.Fprintf(os.Stderr, "Error starting job %s: %s\n", id, err)
				}
			}(id)
		}
	}
}
Exemplo n.º 17
0
func LoadUserAccessToken(token string) (int64, int64, string, error) {
	conn := redis_pool.Get()
	defer conn.Close()

	key := fmt.Sprintf("access_token_%s", token)
	var uid int64
	var appid int64
	var uname string

	exists, err := redis.Bool(conn.Do("EXISTS", key))
	if err != nil {
		return 0, 0, "", err
	}
	if !exists {
		return 0, 0, "", errors.New("token non exists")
	}

	reply, err := redis.Values(conn.Do("HMGET", key, "user_id", "app_id", "user_name"))
	if err != nil {
		log.Info("hmget error:", err)
		return 0, 0, "", err
	}

	_, err = redis.Scan(reply, &uid, &appid, &uname)
	if err != nil {
		log.Warning("scan error:", err)
		return 0, 0, "", err
	}
	return appid, uid, uname, nil
}
Exemplo n.º 18
0
// IsExist returns true if cached value exists.
func (r *RedisCache) IsExist(key string) bool {
	v, err := redigo.Bool(r.do("EXISTS", key))
	if err != nil {
		return false
	}
	return v
}
Exemplo n.º 19
0
// Contains does a membership check on the repository blob set in redis. This
// is used as an access check before looking up global path information. If
// false is returned, the caller should still check the backend to if it
// exists elsewhere.
func (rlic *redisLayerInfoCache) Contains(ctx context.Context, repo string, dgst digest.Digest) (bool, error) {
	conn := rlic.pool.Get()
	defer conn.Close()

	ctxu.GetLogger(ctx).Debugf("(*redisLayerInfoCache).Contains(%q, %q)", repo, dgst)
	return redis.Bool(conn.Do("SISMEMBER", rlic.repositoryBlobSetKey(repo), dgst))
}
Exemplo n.º 20
0
func Test_DoesNothingInDryRunModeForMigrate(t *testing.T) {
	ClearRedis()

	config = Config{
		Source:  sourceServer.url,
		Workers: 1,
		Batch:   10,
		Prefix:  "bar",
		DryRun:  true,
		Dest:    destServer.url,
	}

	for i := 0; i < 100; i++ {
		key := fmt.Sprintf("bar:%d", i)
		sourceServer.conn.Do("SET", key, i)
	}

	RunAction(migrateKeys)

	for i := 0; i < 100; i++ {
		key := fmt.Sprintf("bar:%d", i)
		exists, _ := redis.Bool(destServer.conn.Do("EXISTS", key))

		if exists {
			t.Errorf("In DryRun mode, but found a key %d that was actually migrated", key)
		}
	}
}
Exemplo n.º 21
0
func Test_MigrateAllKeysWithTTLs(t *testing.T) {
	ClearRedis()

	config = Config{
		Source:  sourceServer.url,
		Dest:    destServer.url,
		Workers: 1,
		Batch:   10,
		Prefix:  "bar",
	}

	for i := 0; i < 100; i++ {
		key := fmt.Sprintf("bar:%d", i)
		sourceServer.conn.Do("SET", key, i, "EX", 600)
	}

	RunAction(migrateKeys)

	for i := 0; i < 100; i++ {
		key := fmt.Sprintf("bar:%d", i)
		exists, _ := redis.Bool(destServer.conn.Do("EXISTS", key))

		if !exists {
			t.Errorf("Could not find a key %d that should have been migrated", key)
		}

		ttl, _ := redis.Int64(destServer.conn.Do("PTTL", key))

		if ttl < 1 || ttl > 600000 {
			t.Errorf("Could not find a TTL for key %d that should have been migrated", key)
		}
	}
}
Exemplo n.º 22
0
func ExampleGet() {

	c, _ := redis.Dial("tcp", ":6379")
	defer c.Close()

	hc := hdis.Conn{c}

	key := "object:1234567"
	hc.Set(key, "The value")
	value, _ := redis.String(hc.Get(key))

	fmt.Printf("%v\n", value)

	// Delete the hash if it exists
	if exists, _ := redis.Bool(hc.Do("HEXISTS", key)); exists {
		_, err := hc.Do("HDEL", key)
		if err != nil {
			fmt.Println(err)
		}
	}

	// Execute invalid hash command
	_, err := hc.Do("GET", "sample-key")
	if err == hdis.NotAHashCommandError {
		fmt.Println("Invalid hash command")
	} else {
		fmt.Println(err)
	}

	// Output:
	// The value
	// Invalid hash command
}
Exemplo n.º 23
0
func (this *HashMap) Exists(k string) bool {
	v, err := redis.Bool(Do("HEXISTS", this.Name, k))
	if err != nil {
		return false
	}
	return v
}
Exemplo n.º 24
0
func hasUserAttribute(userId string, attribute string) (bool, error) {
	c, err := getRedisConnection()
	if err != nil {
		return false, err
	}
	return redis.Bool(c.Do("HEXISTS", userId, attribute))
}
Exemplo n.º 25
0
func (s *scheduled) poll() {
	conn := Config.Pool.Get()

	now := nowToSecondsWithNanoPrecision()

	for _, key := range s.keys {
		key = Config.Namespace + key
		for {
			messages, _ := redis.Strings(conn.Do("zrangebyscore", key, "-inf", now, "limit", 0, 1))

			if len(messages) == 0 {
				break
			}

			message, _ := NewMsg(messages[0])

			if removed, _ := redis.Bool(conn.Do("zrem", key, messages[0])); removed {
				queue, _ := message.Get("queue").String()
				queue = strings.TrimPrefix(queue, Config.Namespace)
				message.Set("enqueued_at", nowToSecondsWithNanoPrecision())
				conn.Do("lpush", Config.Namespace+"queue:"+queue, message.ToJson())
			}
		}
	}

	conn.Close()
}
Exemplo n.º 26
0
func AddUser(user *UserInfo) (rs uint32, err error) {
	conn := userRedisPool.Get()
	defer conn.Close()
	var nkey string = fmt.Sprintf("n_%s", user.Name)
	var exist bool
	var seq int64
	if seq, err = redis.Int64(conn.Do("incr", UserIdSeq)); err != nil {
		return
	}
	if exist, err = redis.Bool(conn.Do("exists", nkey)); err != nil {
		return
	}
	if exist {
		err = UserExists
		return
	}
	var buf = make([]byte, 4)
	binary.LittleEndian.PutUint32(buf, uint32(seq))
	if _, err = redis.String(conn.Do("set", nkey, buf)); err != nil {
		return
	}
	rs = uint32(seq)
	user.Id = rs
	key := fmt.Sprintf("u%d", rs)
	var val []byte
	if val, err = json.Marshal(user); err != nil {
		return
	}
	if _, err = redis.String(conn.Do("set", key, val)); err != nil {
		return
	}
	return
}
Exemplo n.º 27
0
func (s *scheduled) poll(continuing bool) {
	if s.closed {
		return
	}

	conn := Config.Pool.Get()
	defer conn.Close()

	now := time.Now().Unix()

	for _, key := range s.keys {
		for {
			messages, _ := redis.Strings(conn.Do("zrangebyscore", key, "-inf", now, "limit", 0, 1))

			if len(messages) == 0 {
				break
			}

			message, _ := NewMsg(messages[0])

			if removed, _ := redis.Bool(conn.Do("zrem", key, messages[0])); removed {
				queue, _ := message.Get("queue").String()
				conn.Do("lpush", fmt.Sprint("queue:", queue), message.ToJson())
			}
		}
	}

	if continuing {
		time.Sleep(POLL_INTERVAL * time.Second)
		s.poll(true)
	}
}
Exemplo n.º 28
0
// check - lock - check
func (provider *RedisProvider) startSession(sid string, conn redis.Conn, option *Options) (Sessioner, bool, error) {
	// 检查随机生成的session是否被占用了
	if exist, err := redis.Bool(conn.Do("EXISTS", getRedisKey(sid))); exist || err != nil {
		return nil, false, err
	}
	// 观察sid
	conn.Send("WATCH", getRedisKey(sid))
	conn.Send("MULTI")
	conn.Send("HSET", getRedisKey(sid), "createtime", time.Now().Unix())
	if option.Cookielifttime > 0 {
		conn.Send("EXPIRE", getRedisKey(sid), option.Cookielifttime)
	} else {
		conn.Send("EXPIRE", getRedisKey(sid), 30*24*60*60)
	}

	replay, err := conn.Do("EXEC")
	if err != nil {
		return nil, false, err
	}
	if replay == nil {
		return nil, false, nil
	}
	sessioner, err := provider.GetSessioner(sid)
	return sessioner, true, err
}
Exemplo n.º 29
0
func setMirrorState(r *redisobj, id string, state bool, reason string) error {
	conn := r.pool.Get()
	defer conn.Close()

	key := fmt.Sprintf("MIRROR_%s", id)

	previousState, err := redis.Bool(conn.Do("HGET", key, "up"))
	if err != nil {
		return err
	}

	var args []interface{}
	args = append(args, key, "up", state, "excludeReason", reason)

	if state != previousState {
		args = append(args, "stateSince", time.Now().Unix())
	}

	_, err = conn.Do("HMSET", args...)

	if state != previousState {
		// Publish update
		Publish(conn, MIRROR_UPDATE, id)
	}

	return err
}
Exemplo n.º 30
0
Arquivo: rater.go Projeto: arvinkx/too
// Add adds a rating by user for item
func (r Rater) Add(user User, item Item) error {
	yes, err := redis.Bool(r.e.c.Do("SISMEMBER", fmt.Sprintf("%s:%s:%s", r.e.class, item, r.kind), user))
	if err != nil {
		return err
	}

	if !yes {
		_, err = r.e.c.Do("ZINCRBY", fmt.Sprintf("%s:mosts:%s", r.e.class, r.kind), 1, item)
		if err != nil {
			return err
		}
	}

	_, err = r.e.c.Do("SADD", fmt.Sprintf("%s:%s:%s", r.e.class, user, r.kind), item)
	if err != nil {
		return err
	}

	_, err = r.e.c.Do("SADD", fmt.Sprintf("%s:%s:%s", r.e.class, item, r.kind), user)
	if err != nil {
		return err
	}

	err = r.e.Similars.update(user)
	if err != nil {
		return err
	}

	err = r.e.Suggestions.update(user)
	if err != nil {
		return err
	}

	return nil
}