<commit_before>package main

import (
	"log"
	"math"
	"os"
	"time"
)

func main() {
	rawDuration := os.Getenv("DURATION")
	if rawDuration == "" {
		rawDuration = "5m"
	}

	duration, err := time.ParseDuration(rawDuration)
	if err != nil {
		log.Fatalf("Could not parse duration %s", err)
	}

	log.Printf(
		"My plan is to chill for %ds then party hard for %ds seconds, then repeat",
		int(duration.Seconds()), int(duration.Seconds()),
	)

	for {
		now := time.Now()
		secondOfHour := now.Second() + (60 * now.Minute()) // between 0 and 3600

		if weShouldChill(secondOfHour, duration) {
			chillForSomeTime(time.Now().Sub(time.Now().Truncate(duration)))
		} else {
			partyHardForSomeTime(time.Now().Sub(time.Now().Truncate(duration)))
		}
	}
}

func chillForSomeTime(duration time.Duration) {
	until := time.Now().Truncate(time.Minute).Add(duration)

	log.Printf("Going to chill until %s", until)

	time.Sleep(duration)
}

func partyHardForSomeTime(duration time.Duration) {
	until := time.Now().Truncate(time.Minute).Add(duration)
	done := make(chan struct{})

	log.Printf("Going to party hard until %s", until)

	go func() {
		for {
			select {
			case <-done:
				return
			default:
			}
		}
	}()

	time.Sleep(duration)
	done <- struct{}{}
}

func weShouldChill(secondOfHour int, duration time.Duration) bool {
	// y = sin(t * pi) is a wave with a period of 2
	// where t is the current second of the hour
	// y is positive between 0 and 1 and negative between 1 and 2

	// if sin(t * pi / seconds) is >= 0 then we should chill, else party hard
	// this ensures that all instances of the app chill and in phase

	durationSeconds := duration.Seconds()
	return math.Sin(math.Pi*float64(secondOfHour)/durationSeconds) >= 0<commit_msg>app-autoscaler-cpu-usage example app: revamp for better reliability, simplicity<commit_after>package main

import (
	"log"
	"os"
	"time"
)

func main() {
	rawDuration := os.Getenv("DURATION")
	if rawDuration == "" {
		rawDuration = "5m"
	}

	duration, err := time.ParseDuration(rawDuration)
	if err != nil {
		log.Fatalf("Could not parse duration %s", err)
	}

	log.Printf(
		"My plan is to chill for %s then party hard for %s, then repeat",
		duration, duration,
	)

	for {
		// using a fixed "now" makes it easier to reason about behaviour
		// and removes an amount of nondeterminism
		uniformNow := time.Now()
		party, timeRemaining := shouldWePartyAndForHowLong(uniformNow, duration)

		if party {
			partyHardForSomeTime(timeRemaining)
		} else {
			chillForSomeTime(timeRemaining)
		}
	}
}

func chillForSomeTime(duration time.Duration) {
	until := time.Now().Truncate(time.Millisecond).Add(duration)

	log.Printf("Going to chill until %s", until)

	time.Sleep(duration)
}

func partyHardForSomeTime(duration time.Duration) {
	until := time.Now().Truncate(time.Millisecond).Add(duration)
	done := make(chan struct{})

	log.Printf("Going to party hard until %s", until)

	go func() {
		for {
			select {
			case <-done:
				return
			default:
			}
		}
	}()

	time.Sleep(duration)
	done <- struct{}{}
}

func shouldWePartyAndForHowLong(tNow time.Time, period time.Duration) (bool, time.Duration) {
	// this has to be calculated based on absolute time so that different
	// instances are in sync with each other, meaning we can't use
	// go's monotonic clock arithmetic, so in theory this isn't totally
	// clock-slew-safe, but i can live with that.

	// whether we should party is just whether the time tNow divided by the
	// period is odd
	shouldWe := (tNow.UnixMilli() / int64(period.Milliseconds())) % 2 != 0

	// the time remaining is the period minus the remainder of that division
	timeRemaining := period - time.Duration(float64(tNow.UnixMilli() % int64(period.Milliseconds())) * float64(time.Millisecond))

	return shouldWe, timeRemaining
{'fraction_non_alphanumeric': 0.1294241944004226, 'fraction_numerical': 0.007395668251452721, 'mean_word_length': 3.873873873873874, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '3400627', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1249, 'n_words': 373}