382
}
383
}
385
}
386
387
// Adds jitter to a nominal time, deterministically (by hashing the given time and a seed).
388
>
func (cs *CompiledSpec) addJitter(seed string, nominal time.Time, maxJitter time.Duration) time.Time {
spec.go
389
>
if maxJitter < 0 {
390
maxJitter = 0
391
}
392
393
>
bin, err := nominal.MarshalBinary()
spec.go
394
>
if err != nil {
395
return nominal
396
}
397
398
>
bin = append(bin, []byte(seed)...)
spec.go
399
>
400
>
// we want to fit the result of a multiply in 64 bits, and use 32 bits of hash, which
401
>
// leaves 32 bits for the range. if we use nanoseconds or microseconds, our range is
402
>
// limited to only a few seconds or hours. using milliseconds supports up to 49 days.
403
>
fp := uint64(farm.Fingerprint32(bin))
404
>
ms := min(uint64(maxJitter.Milliseconds()), math.MaxUint32)
405
>
jitter := time.Duration((fp*ms)>>32) * time.Millisecond
406
>
return nominal.Add(jitter)
407
}