-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from owenfarrell/offset
Ensure even distribution of versions across interval/range
- Loading branch information
Showing
4 changed files
with
455 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package resource | ||
|
||
import ( | ||
"fmt" | ||
"hash/fnv" | ||
"math" | ||
"os" | ||
"time" | ||
|
||
"github.com/concourse/time-resource/lord" | ||
) | ||
|
||
const BUILD_TEAM_NAME = "BUILD_TEAM_NAME" | ||
const BUILD_PIPELINE_NAME = "BUILD_PIPELINE_NAME" | ||
const offsetHashFormat = "%s/%s" | ||
|
||
const maxHashValue = int64(math.MaxUint32) | ||
|
||
var msPerMinute = time.Minute.Milliseconds() | ||
|
||
func Offset(tl lord.TimeLord, reference time.Time) time.Time { | ||
str := fmt.Sprintf(offsetHashFormat, os.Getenv(BUILD_TEAM_NAME), os.Getenv(BUILD_PIPELINE_NAME)) | ||
hasher := fnv.New32a() | ||
if _, err := hasher.Write([]byte(str)); err != nil { | ||
fmt.Fprintln(os.Stderr, "hash error:", err.Error()) | ||
os.Exit(1) | ||
} | ||
hash := int64(hasher.Sum32()) | ||
|
||
start, stop := tl.LatestRangeBefore(reference) | ||
rangeDuration := stop.Sub(start) | ||
|
||
if tl.Interval != nil { | ||
if intervalDuration := time.Duration(*tl.Interval); intervalDuration < rangeDuration { | ||
rangeDuration = intervalDuration | ||
start = reference.Truncate(rangeDuration) | ||
} | ||
} | ||
|
||
if rangeDuration <= time.Minute { | ||
return start | ||
} | ||
|
||
hashPerMinute := maxHashValue / (rangeDuration.Milliseconds() / msPerMinute) | ||
offsetDuration := time.Duration(hash/hashPerMinute) * time.Minute | ||
|
||
return start.Add(offsetDuration) | ||
} |
Oops, something went wrong.