// Run will either return ErrBreakerOpen immediately if the circuit-breaker is
// already open, or it will run the given function and pass along its return
// value. It is safe to call Run concurrently on the same Breaker.
504162
func (b *Breaker) Run(work func() error) error {
	state := atomic.LoadUint32(&b.state)

	if state == open {
		return ErrBreakerOpen
	}

	return b.doWork(state, work)
}
1128323
func (d *MultiDiscovery) Run(looper director.Looper) {
	var loopers []director.Looper

	for _, disco := range d.Discoverers {
		l := director.NewFreeLooper(director.FOREVER, make(chan error))
		loopers = append(loopers, l)
		disco.Run(l)
	}

	// Waiting for a quit on the Looper's channel
	looper.Loop(func() error {
		return nil
	})

	for _, l := range loopers {
		l.Quit()
	}
}
444036
func (r *Rabbus) Run(ctx context.Context) error {
	notifyClose := r.NotifyClose(make(chan *amqp.Error))

	for {
		select {
		case m, ok := <-r.emit:
			if !ok {
				return errors.New("unexpected close of emit channel")
			}

			r.produce(m)

		case err := <-notifyClose:
			if err == nil {
				// "… on a graceful close, no error will be sent."
				return nil
			}

			r.handleAmqpClose(err)

			// We have reconnected, so we need a new NotifyClose again.
			notifyClose = r.NotifyClose(make(chan *amqp.Error))

		case <-ctx.Done():
			return ctx.Err()
		}
	}
}
1107480
func (h *Heartbeat) Run() error {
	defer func() {
		h.reset(HeartbeatStateInit)
		h.checkTicker.Stop()
	}()
	for {
		if err := h.fetchAndAnnounce(); err != nil {
			h.Warningf("Heartbeat failed %v.", err)
		}
		select {
		case <-h.checkTicker.C:
		case <-h.sendC:
			h.Debugf("Asked check out of cycle")
		case <-h.cancelCtx.Done():
			h.Debugf("Heartbeat exited.")
			return nil
		}
	}
}
1007132
func Run(t *testing.T, text string, f func(*testing.T, G, S), opts ...Option) bool {
	t.Helper()
	cfg := options(opts).apply()
	n := &node{
		text:  []string{text},
		seed:  defaultZero64(cfg.seed, time.Now().Unix()),
		order: cfg.order.or(orderSequential),
		scope: cfg.scope.or(scopeLocal),
		nest:  cfg.nest.or(nestOff),
		pend:  cfg.pend,
		focus: cfg.focus,
	}
	report := cfg.report
	plan := n.parse(f)

	var specs chan Spec
	if report != nil {
		report.Start(t, plan)
		specs = make(chan Spec, plan.Total)
		done := make(chan struct{})
		defer func() {
			close(specs)
			<-done
		}()
		go func() {
			report.Specs(t, specs)
			close(done)
		}()
	}

	return n.run(t, func(t *testing.T, n node) {
		t.Helper()
		buffer := &bytes.Buffer{}
		defer func() {
			if specs == nil {
				return
			}
			specs <- Spec{
				Text:     n.text,
				Failed:   t.Failed(),
				Skipped:  t.Skipped(),
				Focused:  n.focus,
				Parallel: n.order == orderParallel,
				Out:      buffer,
			}
		}()
		switch {
		case n.pend, plan.HasFocus && !n.focus:
			t.SkipNow()
		case n.order == orderParallel:
			t.Parallel()
		}
		var (
			spec, group   func()
			before, after []func()
			afterIdx      int
		)
		group = func() {}

		f(t, func(_ string, f func(), _ ...Option) {
			switch {
			case len(n.loc) == 1, n.loc[0] > 0:
				n.loc[0]--
			case n.loc[0] == 0:
				group = func() {
					n.loc = n.loc[1:]
					afterIdx = 0
					group = func() {}
					f()
					group()
				}
				n.loc[0]--
			}
		}, func(_ string, f func(), opts ...Option) {
			cfg := options(opts).apply()
			switch {
			case cfg.out != nil:
				cfg.out(buffer)
			case cfg.before:
				before = append(before, f)
			case cfg.after:
				after = insert(after, f, afterIdx)
				afterIdx++
			case spec != nil:
			case len(n.loc) > 1, n.loc[0] > 0:
				n.loc[0]--
			default:
				spec = f
			}
		})
		group()

		if spec == nil {
			t.Fatal("Failed to locate spec.")
		}
		run(t, before...)
		defer run(t, after...)
		run(t, spec)
	})
}
239393
func (engine *Engine) Run(addr ...string) (err error) {
	defer func() { debugPrintError(err) }()

	address := resolveAddress(addr)
	debugPrint("Listening and serving HTTP on %s\n", address)
	err = http.ListenAndServe(address, engine)
	return
}
522191
func (s *Selector) run(ctx context.Context, t *Target) chan error {
	ch := make(chan error, 1)
	t.waitQueue <- func() bool {
		cur := t.cur
		cur.RLock()
		root := cur.Root
		cur.RUnlock()

		if root == nil {
			// not ready?
			return false
		}

		ids, err := s.by(ctx, root)
		if err != nil || len(ids) < s.exp {
			return false
		}
		nodes, err := s.wait(ctx, cur, ids...)
		// if nodes==nil, we're not yet ready
		if nodes == nil || err != nil {
			return false
		}
		if s.after != nil {
			if err := s.after(ctx, nodes...); err != nil {
				ch <- err
			}
		}
		close(ch)
		return true
	}
	return ch
}
81914
func Run(eng engine.Engine, handler ...engine.Handler) error {
	return Default.Run(eng, handler...)
}
517836
func (re *JSRE) Run(code string) (v otto.Value, err error) {
	re.Do(func(vm *otto.Otto) { v, err = vm.Run(code) })
	return v, err
}
475336
func (b *broker) run() error {
	listener, err := net.Listen("tcp", *addr)
	if err != nil {
		return err
	}
	defer listener.Close()
	fmt.Printf("Listening on %s\n", listener.Addr())
	for {
		conn, err := listener.Accept()
		if err != nil {
			debugf("Accept error: %v", err)
			continue
		}
		adapter := proton.NewMessagingAdapter(newHandler(&b.queues))
		// We want to accept messages when they are enqueued, not just when they
		// are received, so we turn off auto-accept and prefetch by the adapter.
		adapter.Prefetch = 0
		adapter.AutoAccept = false
		engine, err := proton.NewEngine(conn, adapter)
		if err != nil {
			debugf("Connection error: %v", err)
			continue
		}
		engine.Server() // Enable server-side protocol negotiation.
		debugf("Accepted connection %s", engine)
		go func() { // Start goroutine to run the engine event loop
			engine.Run()
			debugf("Closed %s", engine)
		}()
	}
}
471192
func (g *lockedNetNSGoroutine) run(f func()) {
	done := make(chan struct{})
	g.funcC <- func() {
		defer close(done)
		f()
	}
	<-done
}
1318342
func (b *AdapterBase) Run(stopCh <-chan struct{}) error {
	server, err := b.Server()
	if err != nil {
		return err
	}

	return server.GenericAPIServer.PrepareRun().Run(stopCh)
}
785770
func (b *Bench) Run(ctx context.Context) error {
	err := b.createConns(ctx)
	if err != nil {
		return err
	}

	b.createThreads(ctx)
	b.runTest(ctx)
	return nil
}
561522
func (d *DockerDiscovery) Run(looper director.Looper) {
	connQuitChan := make(chan bool)

	go d.manageConnection(connQuitChan)

	go func() {
		// Loop around, process any events which came in, and
		// periodically fetch the whole container list
		looper.Loop(func() error {
			select {
			case event := <-d.events:
				if event == nil {
					// This usually happens because of a Docker restart.
					// Sleep, let us reconnect in the background, then loop.
					return nil
				}
				log.Debugf("Event: %#v\n", event)
				d.handleEvent(*event)
			case <-time.After(d.sleepInterval):
				d.getContainers()
			case <-time.After(CacheDrainInterval):
				d.containerCache.Drain(len(d.services))
			}

			return nil
		})

		// Propagate quit channel message
		close(connQuitChan)
	}()
}
1211035
func (brw *blockRetrievalWorker) run() {
	defer close(brw.doneCh)
	for {
		err := brw.HandleRequest()
		// Only io.EOF is relevant to the loop; other errors are handled in
		// FinalizeRequest
		if err == io.EOF {
			return
		}
	}
}
577572
func (s *StepperDriver) Run() (err error) {
	//halt if already moving
	if s.moving == true {
		s.Halt()
	}

	s.mutex.Lock()
	s.moving = true
	s.mutex.Unlock()

	go func() {
		for {
			if s.moving == false {
				break
			}
			s.step()
		}
	}()

	return
}
392447
func (m *GRPCBroker) Run() {
	for {
		stream, err := m.streamer.Recv()
		if err != nil {
			// Once we receive an error, just exit
			break
		}

		// Initialize the waiter
		p := m.getStream(stream.ServiceId)
		select {
		case p.ch <- stream:
		default:
		}

		go m.timeoutWait(stream.ServiceId, p)
	}
}