// Once returns a Rule that executes the receiver only once.
423481
func (r Rule) Once() Rule {
	if r == nil {
		return nil
	}
	var once sync.Once
	return func(ctx context.Context, e *scheduler.Event, err error, ch Chain) (context.Context, *scheduler.Event, error) {
		ruleInvoked := false
		once.Do(func() {
			ctx, e, err = r(ctx, e, err, ch)
			ruleInvoked = true
		})
		if !ruleInvoked {
			ctx, e, err = ch(ctx, e, err)
		}
		return ctx, e, err
	}
}
1132646
func (limit *simpleLimiter) Count(n int) error {
	now := time.Now()
	if now.After(limit.timeRead) {
		limit.numRead = 0
		limit.timeRead = now.Add(limit.Frequency)
	}
	limit.numRead += n
	if limit.numRead > limit.Amount {
		return ErrRateExceeded
	}
	return nil
}
746495
func (ref *Reference) OnceValue() (*DataSnapshot, error) {
	// TODO: find from cached first
	b, err := ref.invokeRequest(http.MethodGet, nil)
	if err != nil {
		return nil, err
	}
	return &DataSnapshot{
		ref: ref,
		raw: b,
	}, nil
}
409588
func (o *Once) Stop(f func() error) error {
	if o.state.CAS(int32(Idle), int32(Stopped)) {
		close(o.startCh)
		close(o.stoppingCh)
		close(o.stopCh)
		return nil
	}

	<-o.startCh

	if o.state.CAS(int32(Running), int32(Stopping)) {
		close(o.stoppingCh)

		var err error
		if f != nil {
			err = f()
		}

		if err != nil {
			o.setError(err)
			o.state.Store(int32(Errored))
		} else {
			o.state.Store(int32(Stopped))
		}
		close(o.stopCh)
		return err
	}

	<-o.stopCh
	return o.loadError()
}
949377
func (h *Handler) close() {
	h.once.Do(func() {
		for _, fn := range h.notify {
			fn()
		}
	})
}
244443
func (h *ClientHandler) Rule(r rule.Rule) *ClientHandler {
	if r == nil {
		// if nothing passed then use the allow-everything rule
		r = rule.Satisfied()
	}
	h.rule = r

	return h
}
719372
func (i *Info) After(t time.Time) bool {
	return i.genTimeMin().After(t)
}
1352518
func (e *eventEmitter) Once(ev EventType, h EventHandler) {
	e.addHandlerToMap(ev, h, e.once)
}
22013
func (s *Sender) Watch(key string, ctx context.Context) <-chan *Event {
	s.Lock()
	defer s.Unlock()

	log.Infof("A new receiver watching to %s", key)
	r := &Receiver{
		key:     key,
		ctx:     ctx,
		ch:      make(chan *Event, 1),
		counter: 2,
	}
	s.receivers = append(s.receivers, r)
	return (<-chan *Event)(r.ch)
}
431034
func Do(timeout time.Duration, operation DoCondition) error {
	if timeout <= 0 {
		panic("timeout must be larger than zero")
	}

	interval := time.Duration(math.Ceil(float64(timeout) / 15.0))
	if interval > maxInterval {
		interval = maxInterval
	}

	timeout = applyCIMultiplier(timeout)

	var err error
	if err = operation(); err == nil {
		fmt.Println("+")
		return nil
	}
	fmt.Print("*")

	stopTimer := time.NewTimer(timeout)
	defer stopTimer.Stop()
	retryTick := time.NewTicker(interval)
	defer retryTick.Stop()

	for {
		select {
		case <-stopTimer.C:
			fmt.Println("-")
			return fmt.Errorf("try operation failed: %s", err)
		case <-retryTick.C:
			fmt.Print("*")
			if err = operation(); err == nil {
				fmt.Println("+")
				return err
			}
		}
	}
}
936779
func (t *Trainer) Init() (err error) {
	f := func() {
		err = t.nn.init()
	}
	t.once.Do(f)
	return
}
216118
func retry(ctx context.Context, f func() bool, delay time.Duration) error {
	if f() {
		return nil
	}

	ticker := time.NewTicker(delay)
	errChan := make(chan error)

	go func() {
		defer close(errChan)

		for {
			select {
			case <-ctx.Done():
				errChan <- ctx.Err()
				return
			case <-ticker.C:
				if f() {
					return
				}
			}
		}
	}()

	return <-errChan
}
1145978
func (h *haManager) timer() <-chan time.Time {
	if s := h.state(); s == seesaw.HADisabled || s == seesaw.HAUnknown {
		return make(chan time.Time)
	}
	// TODO(angusc): Make this clock-jump safe.
	h.statusLock.RLock()
	deadline := h.status.LastUpdate.Add(h.timeout)
	h.statusLock.RUnlock()
	return time.After(deadline.Sub(time.Now()))
}
299617
func (e *exponentialBackoff) Duration(attempts uint) time.Duration {
	spread := (1 << attempts) * e.first.Nanoseconds()
	if spread <= 0 || spread > e.max {
		spread = e.max
	}
	// Adding 1 to the spread ensures that the upper bound of the range of
	// possible durations includes the maximum.
	return time.Duration(e.rand.Int63n(spread + 1))
}
957059
func (m *EventHandler) Wait(d time.Duration) bool {
	select {
	case <-m.Recv:
		return true
	case <-time.After(d):
		return false
	}
}
820742
func (ctx *Context) Timing(dt time.Duration, fn func(context.Context)) (err error) {
	ct, cancel := ctx.WithTimeout(dt)
	defer cancel()

	ch := make(chan error, 1) // not block tryRunTiming
	go tryRunTiming(ct, fn, ch)

	select {
	case <-ct.Done():
		err = ct.Err()
	case err = <-ch:
	}
	return
}
1275274
func (om *OnceMutex) Lock() bool {
	om.mu.Lock()
	if om.used {
		om.mu.Unlock()
		return false
	}
	return true
}