<commit_before>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package leadership

import (
	"time"

	"github.com/juju/errors"

	"github.com/juju/juju/leadership"
)

// claim is used to deliver leadership-claim requests to a manager's loop
// goroutine on behalf of ClaimLeadership.
type claim struct {
	leaseName  string
	holderName string
	duration   time.Duration
	response   chan bool
	abort      <-chan struct{}
}

// validate returns an error if any fields are invalid or missing.
func (c claim) validate(secretary Secretary) error {
	if err := secretary.CheckLease(c.leaseName); err != nil {
		return errors.Annotate(err, "lease name")
	}
	if err := secretary.CheckHolder(c.holderName); err != nil {
		return errors.Annotate(err, "holder name")
	}
	if c.duration <= 0 {
		return errors.NotValidf("duration %v", c.duration)
	}
	if c.response == nil {
		return errors.NotValidf("nil response channel")
	}
	if c.abort == nil {
		return errors.NotValidf("nil abort channel")
	}
	return nil
}

// invoke sends the claim on the supplied channel and waits for a response.
func (c claim) invoke(secretary Secretary, ch chan<- claim) error {
	if err := c.validate(secretary); err != nil {
		return errors.Annotatef(err, "cannot claim lease")
	}
	for {
		select {
		case <-c.abort:
			return errStopped
		case ch <- c:
			ch = nil
		case success := <-c.response:
			if !success {
				// XXX
				return leadership.ErrClaimDenied
			}
			return nil
		}
	}
}

// respond causes the supplied success value to be sent back to invoke.
func (c claim) respond(success bool) {
	select {
	case <-c.abort:
	case c.response <- success:
	}
}<commit_msg>remove unwanted XXX tag<commit_after>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package leadership

import (
	"time"

	"github.com/juju/errors"

	"github.com/juju/juju/leadership"
)

// claim is used to deliver leadership-claim requests to a manager's loop
// goroutine on behalf of ClaimLeadership.
type claim struct {
	leaseName  string
	holderName string
	duration   time.Duration
	response   chan bool
	abort      <-chan struct{}
}

// validate returns an error if any fields are invalid or missing.
func (c claim) validate(secretary Secretary) error {
	if err := secretary.CheckLease(c.leaseName); err != nil {
		return errors.Annotate(err, "lease name")
	}
	if err := secretary.CheckHolder(c.holderName); err != nil {
		return errors.Annotate(err, "holder name")
	}
	if c.duration <= 0 {
		return errors.NotValidf("duration %v", c.duration)
	}
	if c.response == nil {
		return errors.NotValidf("nil response channel")
	}
	if c.abort == nil {
		return errors.NotValidf("nil abort channel")
	}
	return nil
}

// invoke sends the claim on the supplied channel and waits for a response.
func (c claim) invoke(secretary Secretary, ch chan<- claim) error {
	if err := c.validate(secretary); err != nil {
		return errors.Annotatef(err, "cannot claim lease")
	}
	for {
		select {
		case <-c.abort:
			return errStopped
		case ch <- c:
			ch = nil
		case success := <-c.response:
			if !success {
				return leadership.ErrClaimDenied
			}
			return nil
		}
	}
}

// respond causes the supplied success value to be sent back to invoke.
func (c claim) respond(success bool) {
	select {
	case <-c.abort:
	case c.response <- success:
	}
}
{'fraction_non_alphanumeric': 0.1099554234769688, 'fraction_numerical': 0.003566121842496285, 'mean_word_length': 3.4289473684210527, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3069678', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1080, 'n_words': 374}