Ejemplo n.º 1
0
Archivo: token.go Proyecto: gaego/user
func (e *Token) Put(c appengine.Context) (err error) {
	if e.Key == nil {
		panic("token: Key not set.")
	}
	key, err := ds.Put(c, e.Key, e)
	e.Key = key
	return
}
Ejemplo n.º 2
0
// Put encodes the Values and saves the Config to the store.
func (cnfg *Config) Put(c appengine.Context) (err error) {
	if err = cnfg.encode(); err != nil {
		return
	}
	key, err := ds.Put(c, cnfg.Key, cnfg)
	cnfg.Key = key
	return
}
Ejemplo n.º 3
0
// Put is a convience method to save the Profile to the datastore and
// updated the Updated property to time.Now().
func (u *Profile) Put(c appengine.Context) error {
	// TODO add error handeling for empty Provider and ID
	u.SetKey(c)
	u.Updated = time.Now()
	u.Encode()
	key, err := ds.Put(c, u.Key, u)
	u.Key = key
	return err
}
Ejemplo n.º 4
0
Archivo: user.go Proyecto: gaego/user
// Put is a convience method to save the User to the datastore and
// updated the Updated property to time.Now(). This method should
// always be usdd when saving a user, fore it does some necessary
// preprocessing.
func (u *User) Put(c appengine.Context) (err error) {
	if err = u.SetKey(c); err != nil {
		return
	}
	u.Updated = time.Now()
	u.Encode()
	key, err := ds.Put(c, u.Key, u)
	u.Key = key
	return err
}
Ejemplo n.º 5
0
Archivo: email.go Proyecto: gaego/user
func (e *Email) Put(c appengine.Context) (err error) {
	e.Updated = time.Now()
	e.Address = strings.ToLower(e.Key.StringID())
	e.Key, err = ds.Put(c, e.Key, e)
	return
}
Ejemplo n.º 6
0
func actualInsertPeer(future *insertPeerFuture) {
	defer close(future.resultChannel)

	future.ctx.Debugf("[InsertPeer] Inserting peer: (hash: %s) (ID: %s) (IP: %s) (port: %d)", future.hash, future.peerID, future.ip, future.port)

	changed := false
	peer := new(Peer)

	err := datastore.RunInTransaction(future.ctx, func(tc appengine.Context) error {
		peerKey := getPeerKey(tc, future.hash, future.peerID)

		if err := ds.Get(tc, peerKey, peer); err == nil {
			future.ctx.Debugf("[InsertPeer] Peer already exists, updating...")

			if peer.IP != future.ip {
				peer.IP = future.ip
				changed = true
				future.ctx.Debugf("[InsertPeer] Peer's IP has changed")
			}

			if peer.Port != int32(future.port) {
				peer.Port = int32(future.port)
				changed = true
				future.ctx.Debugf("[InsertPeer] Peer's port has changed")
			}

			currentTime := time.Now()
			if !peer.Expires.After(currentTime) || peer.Expires.Sub(currentTime) <= PeerRefreshTime {
				peer.Expires = time.Now().Add(PeerExpireTime).Add(PeerExpireGrace)
				changed = true
				future.ctx.Debugf("[InsertPeer] Peer's expire time has changed")
			}

			if changed {
				future.ctx.Debugf("[InsertPeer] Putting peer...")
				_, err := ds.Put(tc, peerKey, peer)
				if err != nil {
					future.ctx.Errorf("[InsertPeer] Error when putting updated peer: %s", err)
				}
				return err
			} else {
				future.ctx.Debugf("[InsertPeer] No need to update peer.")
				return nil
			}
		} else if err != dserrors.ErrNoSuchEntity {
			peer = nil
			future.ctx.Errorf("[InsertPeer] Error when retrieving peer from datastore for update: %s", err)
			return err
		}

		future.ctx.Debugf("[InsertPeer] Peer doesn't exist, creating")

		peer = &Peer{
			Hash:    future.hash,
			ID:      future.peerID,
			IP:      future.ip,
			Port:    int32(future.port),
			Expires: time.Now().Add(PeerExpireTime).Add(PeerExpireGrace),
		}

		_, err := ds.Put(tc, peerKey, peer)
		if err != nil {
			future.ctx.Errorf("[InsertPeer] Error when putting new peer: %s", err)
		}
		return err
	}, nil)

	if err == nil && peer != nil {
		memcacheKey := getHashKey(future.ctx, future.hash)
		for updatesAttempted := 1; updatesAttempted <= 3; updatesAttempted++ {
			future.ctx.Debugf("[InsertPeer] Attempting cache update #%d", updatesAttempted)
			var peers []*Peer
			if peersItem, memcacheErr := memcache.Gob.Get(future.ctx, memcacheKey, &peers); memcacheErr == nil {
				exists := false
				for _, thisPeer := range peers {
					if thisPeer.ID == future.peerID {
						exists = true
						break
					}
				}

				if !exists {
					peers = append(peers, peer)

					peersItem.Object = peers

					memcacheErr := memcache.Gob.CompareAndSwap(future.ctx, peersItem)
					if memcacheErr == nil {
						future.ctx.Debugf("[InsertPeer] Added peer to cached peer list.")
						break
					} else if memcacheErr == memcache.ErrCASConflict {
						future.ctx.Debugf("[InsertPeer] Cached peer list conflicted during modification: %s", memcacheErr)
						continue
					} else if memcacheErr == memcache.ErrNotStored {
						future.ctx.Debugf("[InsertPeer] Cached peer list was deleted during modification: %s", memcacheErr)
						continue
					} else if appengine.IsCapabilityDisabled(memcacheErr) {
						future.ctx.Warningf("[InsertPeer] Memcache capability disabled when attempting to add peer to cached peer list")
						continue
					} else {
						future.ctx.Errorf("[InsertPeer] Error when attempting to set cached peer list using CAS: %s", memcacheErr)
						future.resultChannel <- memcacheErr
						return
					}
				} else {
					future.ctx.Debugf("[InsertPeer] Peer already exists in cached list.")
					break
				}
			} else if memcacheErr == memcache.ErrCacheMiss {
				future.ctx.Debugf("[InsertPeer] Peer list not cached. Creating...")

				peersItem := &memcache.Item{
					Key:    memcacheKey,
					Object: []*Peer{peer},
				}
				if memcacheErr := memcache.Gob.CompareAndSwap(future.ctx, peersItem); memcacheErr == nil {
					future.ctx.Debugf("[InsertPeer] Peer list created")
					break
				} else if memcacheErr == memcache.ErrCASConflict {
					future.ctx.Debugf("[InsertPeer] Cached peer list conflicted during creation: %s", memcacheErr)
					continue
				} else if memcacheErr == memcache.ErrNotStored {
					future.ctx.Debugf("[InsertPeer] Cached peer list was deleted during creation: %s", memcacheErr)
					continue
				} else if appengine.IsCapabilityDisabled(memcacheErr) {
					future.ctx.Warningf("[InsertPeer] Memcache capability disabled when attempting to add peer to create cached peer list")
					continue
				} else {
					future.ctx.Errorf("[InsertPeer] Error when creating peer list: %s", memcacheErr)
					future.resultChannel <- memcacheErr
					return
				}
			} else if appengine.IsCapabilityDisabled(memcacheErr) {
				future.ctx.Warningf("[InsertPeer] Memcache capability is disabled.")
				break
			} else {
				future.ctx.Errorf("[InsertPeer] Error when attempting to get cache list for insertion: %s", memcacheErr)
				future.resultChannel <- memcacheErr
				return
			}
		}

		future.ctx.Debugf("[InsertPeer] Successfully inserted peer")
	} else if err != nil {
		future.resultChannel <- err
	}
}
Ejemplo n.º 7
0
Archivo: acl.go Proyecto: gaego/acl
func put(c appengine.Context, key *datastore.Key) (p *Perm, err error) {
	p = &Perm{}
	_, err = ds.Put(c, key, p)
	return
}