Repository: prometheus/alertmanager
File: silence/silence.go (Go)

Code before:
// silence was created. The index is always sorted from lowest to highest version.
//
// The versionIndex allows clients of Silences.Query to incrementally update local caches
// of query results. Instead of a new version requiring the client to scan  everything
// again to get an up-to-date picture of Silences, they can use the versionIndex to figure
// out which silences have been added since the last version they saw. This means they can
// just scan the NEW silences, rather than all of them.
type versionIndex []silenceVersion

// add pushes a new silenceVersionMapping to the back of the silenceVersionIndex. It does not
// validate the input.
func (s *versionIndex) add(version int, sil string) {
	*s = append(*s, silenceVersion{version: version, id: sil})
}

// find uses a log(n) search to find the first index of the versionIndex which has a
// version higher than version. If any entries with a higher version exist, it returns true
// and the starting index (which is guaranteed to be a valid index into the slice). Otherwise
// it returns false.
func (s versionIndex) find(version int) (int, bool) {
	startIdx := sort.Search(len(s), func(i int) bool {
		return s[i].version > version
	})

	return startIdx, startIdx < len(s)
}

// Silencer binds together a AlertMarker and a Silences to implement the Muter
// interface.
type Silencer struct {
	silences *Silences
	marker   types.AlertMarker
	logger   *slog.Logger
}

// NewSilencer returns a new Silencer.
func NewSilencer(s *Silences, m types.AlertMarker, l *slog.Logger) *Silencer {
	return &Silencer{
		silences: s,
		marker:   m,
		logger:   l,
	}
}

// Mutes implements the Muter interface.
func (s *Silencer) Mutes(lset model.LabelSet) bool {
	fp := lset.Fingerprint()
	activeIDs, pendingIDs, markerVersion, _ := s.marker.Silenced(fp)

	var (
		oldSils    []*pb.Silence

Review comment (suggestion):
nit: we can use more verbose method naming and return variable naming to make it more clear:
```suggestion
func (s versionIndex) findBiggerVersionIndex(version int) (index int, found bool) {
	startIdx := sort.Search(len(s), func(i int) bool {
		return s[i].version > version
	})

	return startIdx, startIdx < len(s)
}
```

Code after:
// silence was created. The index is always sorted from lowest to highest version.
//
// The versionIndex allows clients of Silences.Query to incrementally update local caches
// of query results. Instead of a new version requiring the client to scan  everything
// again to get an up-to-date picture of Silences, they can use the versionIndex to figure
// out which silences have been added since the last version they saw. This means they can
// just scan the NEW silences, rather than all of them.
type versionIndex []silenceVersion

// add pushes a new silenceVersionMapping to the back of the silenceVersionIndex. It does not
// validate the input.
func (s *versionIndex) add(version int, sil string) {
	*s = append(*s, silenceVersion{version: version, id: sil})
}

// findVersionGreaterThan uses a log(n) search to find the first index of the versionIndex
// which has a version higher than version. If any entries with a higher version exist,
// it returns true and the starting index (which is guaranteed to be a valid index into
// the slice). Otherwise it returns false.
func (s versionIndex) findVersionGreaterThan(version int) (index int, found bool) {
	startIdx := sort.Search(len(s), func(i int) bool {
		return s[i].version > version
	})
	return startIdx, startIdx < len(s)
}

// Silencer binds together a AlertMarker and a Silences to implement the Muter
// interface.
type Silencer struct {
	silences *Silences
	marker   types.AlertMarker
	logger   *slog.Logger
}

// NewSilencer returns a new Silencer.
func NewSilencer(s *Silences, m types.AlertMarker, l *slog.Logger) *Silencer {
	return &Silencer{
		silences: s,
		marker:   m,
		logger:   l,
	}
}

// Mutes implements the Muter interface.
func (s *Silencer) Mutes(lset model.LabelSet) bool {
	fp := lset.Fingerprint()
	activeIDs, pendingIDs, markerVersion, _ := s.marker.Silenced(fp)

	var (
		oldSils    []*pb.Silence
		newSils    []*pb.Silence