// AttemptFromID produces an empty Attempt model from the AttemptID.
975157
func AttemptFromID(aid *dm.Attempt_ID) *Attempt {
	ret := &Attempt{}
	ret.ID = *aid
	return ret
}
241587
func (r *PEXReactor) AttemptsToDial(addr *p2p.NetAddress) int {
	lAttempts, attempted := r.attemptsToDial.Load(addr.DialString())
	if attempted {
		return lAttempts.(_attemptsToDial).number
	}
	return 0
}
1225442
func FwdDepsFromList(c context.Context, base *dm.Attempt_ID, list *dm.AttemptList) []*FwdDep {
	from := ds.KeyForObj(c, &Attempt{ID: *base})
	keys := make(sort.StringSlice, 0, len(list.To))
	amt := 0
	for qst, nums := range list.To {
		keys = append(keys, qst)
		amt += len(nums.Nums)
	}
	keys.Sort()
	idx := uint32(0)
	ret := make([]*FwdDep, 0, amt)
	for _, key := range keys {
		for _, num := range list.To[key].Nums {
			dep := &FwdDep{Depender: from}
			dep.Dependee.Quest = key
			dep.Dependee.Id = num
			dep.BitIndex = idx
			idx++
			ret = append(ret, dep)
		}
	}
	return ret
}
1298309
func RetryIsErr(fn func(error) bool) RetryFn {
	return func(attempt Attempt) bool {
		return fn(attempt.Error)
	}
}
132648
func (c *NodeCommand) TryRun(cmd string, client auth.ClientI) (match bool, err error) {
	switch cmd {
	case c.nodeAdd.FullCommand():
		err = c.Invite(client)
	case c.nodeList.FullCommand():
		err = c.ListActive(client)

	default:
		return false, nil
	}
	return true, trace.Wrap(err)
}
236811
func (a *AddDeps) RollForward(c context.Context) (muts []tumble.Mutation, err error) {
	// Invalidate the execution key so that they can't make more API calls.
	atmpt, ex, err := model.InvalidateExecution(c, a.Auth)
	if err != nil {
		return
	}

	if err = ResetExecutionTimeout(c, ex); err != nil {
		logging.WithError(err).Errorf(c, "could not reset timeout")
		return
	}

	fwdDeps, err := filterExisting(c, model.FwdDepsFromList(c, a.Auth.Id.AttemptID(), a.Deps))
	err = errors.Annotate(err, "while filtering deps").Tag(grpcutil.Tag.With(codes.Internal)).Err()
	if err != nil || len(fwdDeps) == 0 {
		return
	}

	logging.Fields{"aid": atmpt.ID, "count": len(fwdDeps)}.Infof(c, "added deps")
	atmpt.DepMap = bit_field.Make(uint32(len(fwdDeps)))

	for i, fdp := range fwdDeps {
		fdp.BitIndex = uint32(i)
		fdp.ForExecution = atmpt.CurExecution
	}

	if err = ds.Put(c, fwdDeps, atmpt, ex); err != nil {
		err = errors.Annotate(err, "putting stuff").Tag(grpcutil.Tag.With(codes.Internal)).Err()
		return
	}

	mergeQuestMap := map[string]*MergeQuest(nil)
	if len(a.Quests) > 0 {
		mergeQuestMap = make(map[string]*MergeQuest, len(a.Quests))
		for _, q := range a.Quests {
			mergeQuestMap[q.ID] = &MergeQuest{Quest: q}
		}
	}

	muts = make([]tumble.Mutation, 0, len(fwdDeps)+len(a.Attempts.GetTo())+len(a.Quests))
	for _, dep := range fwdDeps {
		toAppend := &muts
		if mq := mergeQuestMap[dep.Dependee.Quest]; mq != nil {
			toAppend = &mq.AndThen
		}

		if nums, ok := a.Attempts.GetTo()[dep.Dependee.Quest]; ok {
			for _, n := range nums.Nums {
				if n == dep.Dependee.Id {
					*toAppend = append(*toAppend, &EnsureAttempt{ID: &dep.Dependee})
					break
				}
			}
		}
		*toAppend = append(*toAppend, &AddBackDep{
			Dep:      dep.Edge(),
			NeedsAck: true,
		})
	}

	// TODO(iannucci): This could run into datastore transaction limits. We could
	// allieviate this by only emitting a single mutation which does tail-calls to
	// decrease its own, unprocessed size by emitting new MergeQuest mutations.
	for _, mut := range mergeQuestMap {
		muts = append(muts, mut)
	}

	return
}
373043
func RetryIfPossible(ctx context.Context, g *libkb.GlobalContext, post func(ctx context.Context, attempt int) error) (err error) {
	defer g.CTraceTimed(ctx, "RetryIfPossible", func() error { return err })()
	const nRetries = 3
	for i := 0; i < nRetries; i++ {
		g.Log.CDebugf(ctx, "| RetryIfPossible(%v)", i)
		err = post(ctx, i)
		if isSigOldSeqnoError(err) {
			g.Log.CDebugf(ctx, "| retrying due to SigOldSeqnoError", i)
			continue
		}
		if isStaleBoxError(err) {
			g.Log.CDebugf(ctx, "| retrying due to StaleBoxError", i)
			continue
		}
		return err
	}
	g.Log.CDebugf(ctx, "| RetryIfPossible exhausted attempts")
	if err == nil {
		// Should never happen
		return fmt.Errorf("failed retryable team operation")
	}
	// Return the error from the final round
	return err
}
671272
func (r *Report) addFromError(err error, severity entryKind) {
	if err == nil {
		return
	}
	r.Add(Entry{
		Kind:    severity,
		Message: err.Error(),
	})
}
1128947
func (t *ActivatedTask) DepOn(to ...*dm.Attempt_ID) (bool, error) {
	req := &dm.EnsureGraphDataReq{RawAttempts: dm.NewAttemptList(nil)}
	req.RawAttempts.AddAIDs(to...)

	rsp, err := t.EnsureGraphData(req)
	return rsp.ShouldHalt, err
}
860477
func (b *Batch) Attempts() int {
	b.metrics.l.Lock()
	defer b.metrics.l.Unlock()

	var attempts int
	for _, metric := range b.metrics.m {
		attempts += metric.Attempts
	}
	return attempts
}
1174355
func (a *Attempt_ID) DMEncoded() string {
	return fmt.Sprintf("%s|%08x", a.Quest, flipMask^a.Id)
}
507185
func (e *EnsureQuestAttempts) RollForward(c context.Context) (muts []tumble.Mutation, err error) {
	if !e.DoNotMergeQuest {
		if _, err = (&MergeQuest{Quest: e.Quest}).RollForward(c); err != nil {
			return
		}
	}

	if len(e.AIDs) > 0 {
		lim := len(e.AIDs)
		if lim > MaxEnsureAttempts {
			lim = MaxEnsureAttempts + 1
		}
		muts = make([]tumble.Mutation, 0, lim)
		for i, aid := range e.AIDs {
			if i > MaxEnsureAttempts {
				muts = append(muts, &EnsureQuestAttempts{e.Quest, e.AIDs[i:], true})
				break
			}
			muts = append(muts, &EnsureAttempt{dm.NewAttemptID(e.Quest.ID, aid)})
		}
	}

	return
}
1314360
func (m UnblockingMutex) TryLock() (result bool) {
	select {
	case m.C <- true:
		return true
	default:
		return false
	}
}
1059938
func (c *cmdable) PFAdd(key string, els ...interface{}) *IntCmd {
	args := make([]interface{}, 2, 2+len(els))
	args[0] = "pfadd"
	args[1] = key
	args = appendArgs(args, els)
	cmd := NewIntCmd(args...)
	c.process(cmd)
	return cmd
}
910353
func (a *AddrManager) Attempt(addr *wire.NetAddress) {
	a.mtx.Lock()
	defer a.mtx.Unlock()

	// find address.
	// Surely address will be in tried by now?
	ka := a.find(addr)
	if ka == nil {
		return
	}
	// set last tried time to now
	ka.attempts++
	ka.lastattempt = time.Now()
}
1045425
func (r *PEXReactor) attemptDisconnects() {
	for _, peer := range r.Switch.Peers().List() {
		if peer.Status().Duration < r.config.SeedDisconnectWaitPeriod {
			continue
		}
		if peer.IsPersistent() {
			continue
		}
		r.Switch.StopPeerGracefully(peer)
	}
}
875158
func (mutex *Mutex) TryLock() error {
	ret := int(C.SDL_TryLockMutex(mutex.cptr()))
	if ret != 0 {
		return GetError()
	}
	return nil
}