Example #1
0
File: idlog.go Project: crooks/yamn
func (i *IDLog) Expire() (count, deleted int) {
	var err error
	now := time.Now()
	iter := i.db.NewIterator(nil, nil)
	var timestamp time.Time
	for iter.Next() {
		key := iter.Key()
		err = timestamp.GobDecode(iter.Value())
		if err != nil {
			panic(err)
		}
		if now.After(timestamp) {
			i.db.Delete(key, nil)
			deleted++
		} else {
			count++
		}
	}
	iter.Release()
	err = iter.Error()
	if err != nil {
		panic(err)
	}
	return
}
Example #2
0
func ToTime(in []byte) (time.Time, []byte, error) {
	var t time.Time
	l := in[0]
	err := t.GobDecode(in[1 : l+1])
	if err != nil {
		return t, nil, err
	}
	return t, in[l+1:], nil
}
Example #3
0
func (ce *ChatEngine) GetLastSeen(username string) string {
	encodedTime, err := ce.chatState.userInfo.Get(username, "lastseen")
	if err == nil {
		var then time.Time
		err = then.GobDecode([]byte(encodedTime))
		if err == nil {
			timestamp := then.String()
			return timestamp[11:19]
		}
	}
	return "never"
}
// Reserve will reserve an item with the id in the DefaultReservation for the given amount of
// time.
func (dr *DefaultReservation) Reserve(d time.Duration) error {
	log.Debugf("[Sync:Reservation] Attempting to reserve '%s' for %s...", dr.id, d)

	lockpath := constructPath(dr.path, dr.id)

	// Check if the reservation already exists
	b, _, err := zookeeper.Get(lockpath)
	if err == nil {

		// It exists, check if expired
		var expired time.Time
		expired.GobDecode(b)

		log.Debugf("[Sync:Reservation] Read existing node for '%s', expires at %s", dr.id, expired)

		if expired.Before(time.Now()) {
			log.Debugf("[Sync:Reservation] Deleting expired lock '%s'", dr.id)

			// It has expired, delete the node and get the reservation as usual
			zookeeper.Delete(lockpath, -1)
		} else {
			return ErrReserved
		}
	}

	expires := time.Now().Add(d)
	expiresBytes, err := expires.GobEncode()
	if err != nil {
		return err
	}

	for {
		_, err = zookeeper.Create(lockpath, expiresBytes, 0, dr.acl)
		if err == gozk.ErrNoNode {
			createParents(dr.path)
		} else if err == nil {
			break
		} else {
			log.Warnf("[Reservation] ZK error creating ephemeral lock node for reservation: %v", err)
			return err
		}
	}

	log.Debugf("[Sync:Reservation] Created lock node for '%s', expires at %s", dr.id, expires)

	return nil

}
Example #5
0
func (ce *ChatEngine) IsChatting(username string) bool {
	encodedTime, err := ce.chatState.userInfo.Get(username, "lastseen")
	if err == nil {
		var then time.Time
		err = then.GobDecode([]byte(encodedTime))
		if err == nil {
			elapsed := time.Since(then)
			if elapsed.Minutes() > 20 {
				// 20 minutes since last seen saying anything, set as not chatting
				ce.SetChatting(username, false)
				return false
			}
		}
	}
	// TODO: Just return true instead of retrieving the data
	return ce.state.BooleanField(username, "chatting")
}
Example #6
0
// findLowestSequenceNode within a particular lock path
func findLowestSequenceNode(path string, seq int) (lowestSeq int, prevSeqPath string, err error) {
	// Read all the children of the node
	// This is why we create sequential nodes under a parent node based on the lock ID
	// If not, say we stored all ephemeral locks under a higher level parent,
	// we would be checking nodes of every lock currently in play, rather than locks
	// on this ID
	children, _, err := zookeeper.Children(path)
	if err != nil {
		return -1, "", err
	}

	var ttl time.Time
	lowestSeq = seq
	prevSeq := 0
	prevSeqPath = ""
	for _, p := range children {
		// Check if this lock has timed out
		data, _, _ := zookeeper.Get(path + "/" + p)
		if len(data) > 0 {
			ttl.GobDecode(data)
			if ttl.Before(time.Now()) {
				log.Tracef("[RegionLock] Deleting expired lock '%s'", path+"/"+p)
				zookeeper.Delete(path+"/"+p, -1)
				continue
			}
		}

		s, err := parseSeq(p)
		if err != nil {
			return -1, "", err
		}
		if s < lowestSeq {
			lowestSeq = s
		}
		if s < seq && s > prevSeq {
			prevSeq = s
			prevSeqPath = p
		}
	}

	return lowestSeq, prevSeqPath, err
}
Example #7
0
// Checks if the user has been seen lately (within 12 hours ago)
func (ce *ChatEngine) SeenLately(username string) bool {
	encodedTime, err := ce.chatState.userInfo.Get(username, "lastseen")
	if err != nil {
		return false
	}
	var then time.Time
	err = then.GobDecode([]byte(encodedTime))
	if err != nil {
		return false
	}
	notTooLongDuration, err := time.ParseDuration("-12h")
	if err != nil {
		return false
	}
	notTooLongAgo := time.Now().Add(notTooLongDuration)
	if then.After(notTooLongAgo) {
		return true
	}
	return false
}
Example #8
0
func TestSqlDump(t *testing.T) {
	var test Test

	ti := time.Date(2013, time.April, 16, 10, 22, 0, 0, time.UTC)
	test.T = ti
	test.D = "test"
	_, values := SQL_dump(&test)

	if values[0] != "'2013-04-16 10:22:00'" {
		t.Error("time dump failed")
	}

	if values[1] != "'0'" {
		t.Error("int dump failed")
	}

	if values[2] != "'0'" {
		t.Error("uint dump failed")
	}

	if values[3] != "'0'" {
		t.Error("uint dump failed")
	}

	if values[4] != "'test'" {
		t.Error("string dump failed")
	}

	ti2, _ := time.Parse("2006-01-02 15:04:05", "2013-04-16 10:22:00")
	gob, _ := ti2.GobEncode()
	var ti3 time.Time
	ti3.GobDecode(gob)

	if ti3.Format("2006-01-02 15:04:05") != "2013-04-16 10:22:00" {
		t.Error("time gob failed")
	}
}
Example #9
0
func (r *Transceiver) handleEchoReply(packet []byte) error {
	msg, err := r.factory.NewEchoReply()
	if err != nil {
		return err
	}
	if err := msg.UnmarshalBinary(packet); err != nil {
		return err
	}

	data := msg.Data()
	if data == nil || len(data) != 8 {
		return errors.New("unexpected ECHO_REPLY data")
	}
	timestamp := time.Time{}
	if err := timestamp.GobDecode(data); err != nil {
		return err
	}
	// Update network latency
	r.latency = time.Now().Sub(timestamp)
	// Reset ping counter to zero
	r.pingCounter = 0

	return nil
}
Example #10
0
func Fuzz(data []byte) int {
	var t time.Time
	if err := t.UnmarshalText(data); err != nil {
		return 0
	}
	data1, err := t.MarshalText()
	if err != nil {
		panic(err)
	}
	var t1 time.Time
	if err := t1.UnmarshalText(data1); err != nil {
		panic(err)
	}
	if !fuzz.DeepEqual(t, t1) {
		fmt.Printf("t0: %#v\n", t)
		fmt.Printf("t1: %#v\n", t1)
		panic("bad MarshalText")
	}

	data2, err := t.GobEncode()
	if err != nil {
		panic(err)
	}
	var t2 time.Time
	if err := t2.GobDecode(data2); err != nil {
		panic(err)
	}
	if !fuzz.DeepEqual(t, t2) {
		fmt.Printf("t0: %#v\n", t)
		fmt.Printf("t2: %#v\n", t2)
		panic("bad GobEncode")
	}

	data3, err := t.MarshalBinary()
	if err != nil {
		panic(err)
	}
	var t3 time.Time
	if err := t3.UnmarshalBinary(data3); err != nil {
		panic(err)
	}
	if !fuzz.DeepEqual(t, t3) {
		fmt.Printf("t0: %#v\n", t)
		fmt.Printf("t3: %#v\n", t3)
		panic("bad MarshalBinary")
	}

	data4, err := t.MarshalJSON()
	if err != nil {
		panic(err)
	}
	var t4 time.Time
	if err := t4.UnmarshalJSON(data4); err != nil {
		panic(err)
	}
	if !fuzz.DeepEqual(t, t4) {
		fmt.Printf("t0: %#v\n", t)
		fmt.Printf("t4: %#v\n", t4)
		panic("bad MarshalJSON")
	}

	data5, err := t.MarshalText()
	if err != nil {
		panic(err)
	}
	var t5 time.Time
	if err := t5.UnmarshalText(data5); err != nil {
		panic(err)
	}
	if !fuzz.DeepEqual(t, t5) {
		fmt.Printf("t0: %#v\n", t)
		fmt.Printf("t5: %#v\n", t5)
		panic("bad MarshalText")
	}

	data6 := t.Format(time.RFC3339Nano)
	t6, err := time.Parse(time.RFC3339Nano, data6)
	if err != nil {
		panic(err)
	}
	if !fuzz.DeepEqual(t, t6) {
		fmt.Printf("t0: %#v\n", t)
		fmt.Printf("t6: %#v\n", t6)
		panic("bad Format")
	}
	return 1
}