<commit_before>package expire

import "time"

// An Periodic is a combination of a ticker and utility functions
// determining when to do extra work, and when to stream/flush
// accumulated work.
type Periodic struct {
	C       chan time.Time
	c       <-chan time.Time
	done    chan bool
	ticker  *time.Ticker
	last    int64
	overdue time.Duration
	fuzz    time.Duration
	time    time.Time
	stopped bool
}

// New creates a Periodic with a ticker set to the recheck duration
// and with the overdue duration provided.  Fuzz is used reduce latency
// by providing a window (+/- fuzz) around the expiration time.
// All parameters are in milliseconds.
func New(recheckMs, overdueMs, fuzzMs time.Duration) *Periodic {
	recheck, overdue, fuzz :=
		recheckMs*time.Millisecond,
		overdueMs*time.Second,
		fuzzMs*time.Millisecond
	tic := time.NewTicker(recheck)
	bell := &Periodic{
		C:       make(chan time.Time, 1),
		c:       tic.C,
		last:    time.Now().UnixNano(),
		ticker:  tic,
		overdue: overdue,
		fuzz:    fuzz,
		done:    make(chan bool, 1),
		time:    time.Now(),
		stopped: false,
	}
	go func() {
		for {
			select {
			case bell.time = <-bell.c:
				bell.C <- bell.time
			case <-bell.done:
				return
			}
		}
	}()
	return bell
}

// Stop turn off the Periodic.  After Stop no more ticks will be
// sent via the ticker. Stop does not close the channel to prevent
// a read from the channel to succeed incorrectly.
func (e *Periodic) Stop() {
	if e.stopped {
		return
	}
	e.stopped = true
	e.ticker.Stop()
	e.done <- true
}

// Determines if more time the the overdue duration has elapsed.
func (e *Periodic) IsOverdue() bool {
	now := e.time.UnixNano()
	delta := e.last - now

	return time.Duration(delta) < (e.overdue + e.fuzz)
}

// Reset starts the expiration period over.
func (e *Periodic) Reset() {
	e.last = time.Now().UnixNano()
}<commit_msg>Forgot to add these changes.<commit_after>package expire

import (
	"time"
)

// An Periodic is a combination of a ticker and utility functions
// determining when to do extra work, and when to stream/flush
// accumulated work.
type Periodic struct {
	C       chan time.Time
	c       <-chan time.Time
	done    chan bool
	ticker  *time.Ticker
	last    int64
	overdue time.Duration
	fuzz    time.Duration
	time    time.Time
	stopped bool
}

// New creates a Periodic with a ticker set to the recheck duration
// and with the overdue duration provided.  Fuzz is used reduce latency
// by providing a window (+/- fuzz) around the expiration time.
// All parameters are in milliseconds.
func New(recheckMs, overdueMs, fuzzMs time.Duration) *Periodic {
	recheck, overdue, fuzz :=
		recheckMs*time.Millisecond,
		overdueMs*time.Millisecond,
		fuzzMs*time.Millisecond
	tic := time.NewTicker(recheck)
	bell := &Periodic{
		C:       make(chan time.Time, 1),
		c:       tic.C,
		last:    time.Now().UnixNano(),
		ticker:  tic,
		overdue: overdue,
		fuzz:    fuzz,
		done:    make(chan bool, 1),
		time:    time.Now(),
		stopped: false,
	}
	go func() {
		for {
			select {
			case currTime := <-bell.c:
				now := currTime.UnixNano()
				delta := now - bell.last
				lower := bell.overdue - bell.fuzz
				upper := bell.overdue + bell.fuzz
				isOverdue := lower < time.Duration(delta) && time.Duration(delta) < upper
				if isOverdue {
					bell.last = now
					bell.C <- currTime
				}
			case <-bell.done:
				return
			}
		}
	}()
	return bell
}

// Stop turn off the Periodic.  After Stop no more ticks will be
// sent via the ticker. Stop does not close the channel to prevent
// a read from the channel to succeed incorrectly.
func (e *Periodic) Stop() {
	if e.stopped {
		return
	}
	e.stopped = true
	e.ticker.Stop()
	e.done <- true
}

// Reset starts the expiration period over.
func (e *Periodic) Reset() {
	e.last = time.Now().UnixNano()
}
{'fraction_non_alphanumeric': 0.11014492753623188, 'fraction_numerical': 0.002108036890645586, 'mean_word_length': 2.8382204246713854, 'pattern_counts': {'":': 0, '<': 16, '<?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': '6842548', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1295, 'n_words': 440}