File size: 1,364 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import * as Timing from './timing';
import type { ExperimentAssignment } from '../types';

/**
 * Check if an ExperimentAssignment is still alive (as in the TTL).
 * @param experimentAssignment The experiment assignment to check
 */
export function isAlive( experimentAssignment: ExperimentAssignment ): boolean {
	return (
		Timing.monotonicNow() <
		experimentAssignment.ttl * Timing.MILLISECONDS_PER_SECOND +
			experimentAssignment.retrievedTimestamp
	);
}

/**
 * The minimum ttl (in seconds) for any ExperimentAssignment.
 * This limits the number of requests being sent to our server in the case of our server failing to return a working assignment
 * and will be the minimum amount of time in-between requests per experiment.
 */
export const minimumTtl = 60;

/**
 * A fallback ExperimentAssignment we return when we can't retrieve one.
 * As it is used in fallback situations, this function must never throw.
 * @param experimentName The name of the experiment
 * @param ttl The time-to-live for the ExperimentAssignment, defaults to 60s
 */
export const createFallbackExperimentAssignment = (
	experimentName: string,
	ttl: number = minimumTtl
): ExperimentAssignment => ( {
	experimentName: experimentName,
	variationName: null,
	retrievedTimestamp: Timing.monotonicNow(),
	ttl: Math.max( minimumTtl, ttl ),
	isFallbackExperimentAssignment: true,
} );