// WithRestartPolicy sets container's restart policy
335749
func WithRestartPolicy(policy string) func(c *TestContainerConfig) {
	return func(c *TestContainerConfig) {
		c.HostConfig.RestartPolicy.Name = policy
	}
}
759040
func WithRestoreRuntime(ctx context.Context, id string, client *Client, checkpoint Image, index *imagespec.Index) NewContainerOpts {
	return func(ctx context.Context, client *Client, c *containers.Container) error {
		name, ok := index.Annotations[checkpointRuntimeNameLabel]
		if !ok {
			return ErrRuntimeNameNotFoundInIndex
		}

		// restore options if present
		m, err := GetIndexByMediaType(index, images.MediaTypeContainerd1CheckpointRuntimeOptions)
		if err != nil {
			if err != ErrMediaTypeNotFound {
				return err
			}
		}
		var options *ptypes.Any
		if m != nil {
			store := client.ContentStore()
			data, err := content.ReadBlob(ctx, store, *m)
			if err != nil {
				return errors.Wrap(err, "unable to read checkpoint runtime")
			}
			if err := proto.Unmarshal(data, options); err != nil {
				return err
			}
		}

		c.Runtime = containers.RuntimeInfo{
			Name:    name,
			Options: options,
		}
		return nil
	}
}
1324498
func (c *Controller) OnRestartFailed(fn func() error) {
	c.instance.OnRestartFailed = append(c.instance.OnRestartFailed, fn)
}
264380
func (s *PutResolverRulePolicyInput) SetResolverRulePolicy(v string) *PutResolverRulePolicyInput {
	s.ResolverRulePolicy = &v
	return s
}
184689
func NewRestartFrameArgs(callFrameID CallFrameID) *RestartFrameArgs {
	args := new(RestartFrameArgs)
	args.CallFrameID = callFrameID
	return args
}
170376
func (f *Facade) rollingRestart(ctx datastore.Context, svc *service.Service, timeout time.Duration, cancel <-chan interface{}) error {
	logger := plog.WithFields(log.Fields{
		"serviceid":   svc.ID,
		"servicename": svc.Name,
	})

	// Run through and set all instances to "Pending Restart"
	for instanceID := 0; instanceID < svc.Instances; instanceID++ {
		err := f.zzk.UpdateInstanceCurrentState(ctx, svc.PoolID, svc.ID, instanceID, service.StatePendingRestart)
		if err != nil {
			logger.WithError(err).Debug("Failed to update instance current state to pending restart")
			return err
		}
	}

	// Keep track of which instances haven't been restarted yet, so we can revert the current state if we exit
	// prematurely
	nextInstance := 0
	defer func() {
		for instance := nextInstance; instance < svc.Instances; instance++ {
			err := f.zzk.UpdateInstanceCurrentState(ctx, svc.PoolID, svc.ID, instance, service.StateRunning)
			if err != nil {
				logger.WithField("instance", instance).WithError(err).Error("Failed to revert instance current state to started")
			}
		}
	}()

	// Build the service health object to use for getting instance health
	svch := service.BuildServiceHealth(*svc)
	var punctualLock sync.RWMutex
	punctualInstances := 0

	for instanceID := 0; instanceID < svc.Instances; instanceID++ {
		ilogger := logger.WithField("instance", instanceID)
		ilogger.Debug("Restarting instance")

		select {
		case <-cancel:
			return nil
		default:
		}

		// Set up the timeout
		cancelWait := make(chan struct{})
		done := make(chan struct{})
		timer := time.NewTimer(timeout)
		go func() {
			select {
			case <-timer.C:
				ilogger.Warn("Timeout waiting for instance to restart")
			case <-done:
				punctualLock.Lock()
				punctualInstances++
				punctualLock.Unlock()
			case <-cancel:
				ilogger.Debug("Rolling restart cancelled")
			}
			timer.Stop()
			close(cancelWait)
		}()

		// Before we restart, check the current instance's container ID
		state, err := f.zzk.GetServiceState(ctx, svc.PoolID, svc.ID, instanceID)
		if err != nil {
			close(done)
			logger.WithError(err).Debug("Failed to get service's current container ID")
			return err
		}

		oldContainer := state.ContainerID

		if err = f.zzk.RestartInstance(ctx, svc.PoolID, svc.ID, instanceID); err != nil {
			close(done)
			logger.WithError(err).Debug("Failed to restart instance")
			return err
		}

		// Increment nextInstance now, since we have started the restart process for this instance
		nextInstance++

		// Wait for the instance's containerID to change
		checkContainer := func(s *zkservice.State, exists bool) bool {
			if !exists {
				return true
			}
			if s.ContainerID != "" && s.ContainerID != oldContainer {
				return service.InstanceCurrentState(s.Status) == service.StateRunning
			}
			return false
		}
		if err = f.zzk.WaitInstance(ctx, svc, instanceID, checkContainer, cancelWait); err != nil {
			close(done)
			logger.WithError(err).Debug("Failed to wait on instance")
			return err
		}

		// Check if we're ready to move on to the next instance on an interval
		ready := func() bool {
			// I don't care if it is the last instance
			if instanceID == svc.Instances-1 {
				return true
			}

			// Wait for health checks to pass
			statuses := f.getInstanceHealth(svch, instanceID)
			for key, status := range statuses {
				ilogger.WithFields(log.Fields{
					"status": status,
					"key":    key,
				}).Debug("Got health status for instance")
				if status != health.OK {
					return false
				}
			}

			return true
		}

		hctimer := time.NewTimer(500 * time.Millisecond)

		// See if we have timed out
		cancelled := false
		select {
		case <-cancelWait:
			cancelled = true
		default:
		}

		for !cancelled && !ready() {
			ilogger.Debug("Instance not ready yet, checking again in 500 ms")
			select {
			case <-cancelWait:
				cancelled = true
			case <-hctimer.C:
				hctimer.Stop()
				hctimer.Reset(500 * time.Millisecond)
			}
		}
		hctimer.Stop()
		ilogger.Debug("Done restarting instance")
		close(done)
		<-cancelWait
	}

	punctualLock.RLock()
	instances := punctualInstances
	punctualLock.RUnlock()
	if instances == svc.Instances {
		f.SetServicesCurrentState(ctx, service.SVCCSRunning, svc.ID)
	}

	return nil
}
812670
func (s *HlsBasicPutSettings) SetRestartDelay(v int64) *HlsBasicPutSettings {
	s.RestartDelay = &v
	return s
}
580611
func WithLoadBalancePolicy(value lbp.LoadBalancePolicy) options.Opt {
	return func(p options.Params) {
		if setter, ok := p.(loadBalancePolicySetter); ok {
			setter.SetLoadBalancePolicy(value)
		}
	}
}
1285374
func DefaultRetryPolicy(ctx context.Context, resp *http.Response, err error) (bool, error) {
	// do not retry on context.Canceled or context.DeadlineExceeded
	if ctx.Err() != nil {
		return false, ctx.Err()
	}

	if err != nil {
		return true, err
	}
	// Check the response code. We retry on 500-range responses to allow
	// the server time to recover, as 500's are typically not permanent
	// errors and may relate to outages on the server side. This will catch
	// invalid response codes as well, like 0 and 999.
	if resp.StatusCode == 0 || (resp.StatusCode >= 500 && resp.StatusCode != 501) {
		return true, nil
	}

	return false, nil
}
1041547
func (client *Client) SetRetryPolicy(policy retry.Policy) error {
	if policy == nil {
		return ErrInvalidRetryPolicy
	}
	client.retryPolicy = policy
	return nil
}
669479
func (e *SyncedEnforcer) SavePolicy() error {
	e.m.RLock()
	defer e.m.RUnlock()
	return e.Enforcer.SavePolicy()
}
312286
func (h *Service) NewRetentionPolicy(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	srcID, err := paramID("id", r)
	if err != nil {
		Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
		return
	}

	src, err := h.Store.Sources(ctx).Get(ctx, srcID)
	if err != nil {
		notFound(w, srcID, h.Logger)
		return
	}

	dbsvc := h.Databases
	if err = dbsvc.Connect(ctx, &src); err != nil {
		msg := fmt.Sprintf("unable to connect to source %d: %v", srcID, err)
		Error(w, http.StatusBadRequest, msg, h.Logger)
		return
	}

	postedRP := &chronograf.RetentionPolicy{}
	if err := json.NewDecoder(r.Body).Decode(postedRP); err != nil {
		invalidJSON(w, h.Logger)
		return
	}
	if err := ValidRetentionPolicyRequest(postedRP); err != nil {
		invalidData(w, err, h.Logger)
		return
	}

	db := httprouter.ParamsFromContext(ctx).ByName("db")
	rp, err := dbsvc.CreateRP(ctx, db, postedRP)
	if err != nil {
		Error(w, http.StatusBadRequest, err.Error(), h.Logger)
		return
	}
	res := rpResponse{
		Name:          rp.Name,
		Duration:      rp.Duration,
		Replication:   rp.Replication,
		ShardDuration: rp.ShardDuration,
		Default:       rp.Default,
	}
	res.WithLinks(srcID, db)
	encodeJSON(w, http.StatusCreated, res, h.Logger)
}
127609
func (s *GetStagesInput) SetRestApiId(v string) *GetStagesInput {
	s.RestApiId = &v
	return s
}
196926
func (b *Batch) RetryPolicy(r RetryPolicy) *Batch {
	b.rt = r
	return b
}
204207
func DefaultPolicy() Policy {
	return Policy{
		BlobType:   blob.TypeDefault,
		MaxUpdates: DefaultMaxUpdates,
		RewardRate: DefaultRewardRate,
		SweepFeeRate: lnwallet.SatPerKWeight(
			DefaultSweepFeeRate,
		),
	}
}
556729
func (cli *Client) ContainerRestart(ctx context.Context, containerID string, timeout *time.Duration) error {
	query := url.Values{}
	if timeout != nil {
		query.Set("t", timetypes.DurationToSecondsString(*timeout))
	}
	resp, err := cli.post(ctx, "/containers/"+containerID+"/restart", query, nil, nil)
	ensureReaderClosed(resp)
	return err
}
577571
func (orig OptionsConfiguration) RequiresRestartOnly() OptionsConfiguration {
	copy := orig
	blank := OptionsConfiguration{}
	util.CopyMatchingTag(&blank, &copy, "restart", func(v string) bool {
		if len(v) > 0 && v != "true" {
			panic(fmt.Sprintf(`unexpected tag value: %s. expected untagged or "true"`, v))
		}
		return v != "true"
	})
	return copy
}