|
9 | 9 | import org.slf4j.Logger;
|
10 | 10 | import org.slf4j.LoggerFactory;
|
11 | 11 |
|
| 12 | +/** |
| 13 | + * Class used to ingest and use the flag and bandit configurations retrieved from Eppo This class |
| 14 | + * uses the Singleton pattern. First the singleton must be initialized via it's Builder's |
| 15 | + * buildAndInit() method. Then call getInstance() to access the singleton and call methods to get |
| 16 | + * assignments and bandit actions. |
| 17 | + */ |
12 | 18 | public class EppoClient extends BaseEppoClient {
|
13 | 19 | private static final Logger log = LoggerFactory.getLogger(EppoClient.class);
|
14 | 20 |
|
@@ -40,63 +46,92 @@ private EppoClient(
|
40 | 46 | apiKey, host, sdkName, sdkVersion, assignmentLogger, banditLogger, isGracefulModel, false);
|
41 | 47 | }
|
42 | 48 |
|
| 49 | + /** Stops the client from polling Eppo for updated flag and bandit configurations */ |
43 | 50 | public static void stopPolling() {
|
44 | 51 | if (pollTimer != null) {
|
45 | 52 | pollTimer.cancel();
|
46 | 53 | }
|
47 | 54 | }
|
48 | 55 |
|
| 56 | + /** Builder pattern to initialize the EppoClient singleton */ |
49 | 57 | public static class Builder {
|
50 | 58 | private String apiKey;
|
51 |
| - private String host = DEFAULT_HOST; |
52 | 59 | private AssignmentLogger assignmentLogger;
|
53 | 60 | private BanditLogger banditLogger;
|
54 | 61 | private boolean isGracefulMode = DEFAULT_IS_GRACEFUL_MODE;
|
55 | 62 | private boolean forceReinitialize = DEFAULT_FORCE_REINITIALIZE;
|
56 | 63 | private long pollingIntervalMs = DEFAULT_POLLING_INTERVAL_MS;
|
| 64 | + private String host = DEFAULT_HOST; |
57 | 65 |
|
| 66 | + /** Sets the API Key--created within the eppo application--to use. This is required. */ |
58 | 67 | public Builder apiKey(String apiKey) {
|
59 | 68 | this.apiKey = apiKey;
|
60 | 69 | return this;
|
61 | 70 | }
|
62 | 71 |
|
63 |
| - public Builder host(String host) { |
64 |
| - this.host = host; |
65 |
| - return this; |
66 |
| - } |
67 |
| - |
| 72 | + /** |
| 73 | + * Assignment logger to use to record when variations were assigned. This is needed if you want |
| 74 | + * Eppo to analyze experiments controlled by flags. |
| 75 | + */ |
68 | 76 | public Builder assignmentLogger(AssignmentLogger assignmentLogger) {
|
69 | 77 | this.assignmentLogger = assignmentLogger;
|
70 | 78 | return this;
|
71 | 79 | }
|
72 | 80 |
|
| 81 | + /** |
| 82 | + * Bandit logger to use to record when a bandit has assigned an action. This is needed if you |
| 83 | + * are using contextual multi-armed bandits. |
| 84 | + */ |
73 | 85 | public Builder banditLogger(BanditLogger banditLogger) {
|
74 | 86 | this.banditLogger = banditLogger;
|
75 | 87 | return this;
|
76 | 88 | }
|
77 | 89 |
|
| 90 | + /** |
| 91 | + * Sets the initial graceful mode of the client. When on (which is the default), flag evaluation |
| 92 | + * errors will be caught, and the default value returned. When off, the errors will be rethrown. |
| 93 | + */ |
78 | 94 | public Builder isGracefulMode(boolean isGracefulMode) {
|
79 | 95 | this.isGracefulMode = isGracefulMode;
|
80 | 96 | return this;
|
81 | 97 | }
|
82 | 98 |
|
| 99 | + /** |
| 100 | + * Sets whether the singleton client should be recreated if one already has been. fetch an |
| 101 | + * updated configuration. If true, a new client will be instantiated and a new fetch for |
| 102 | + * configurations will be performed. If false (which is the default), initialization will be |
| 103 | + * ignored and the previously initialized client will be used. |
| 104 | + */ |
83 | 105 | public Builder forceReinitialize(boolean forceReinitialize) {
|
84 | 106 | this.forceReinitialize = forceReinitialize;
|
85 | 107 | return this;
|
86 | 108 | }
|
87 | 109 |
|
| 110 | + /** |
| 111 | + * Sets how often the client should check for updated configurations, in milliseconds. The |
| 112 | + * default is 30,000 (poll every 30 seconds). |
| 113 | + */ |
88 | 114 | public Builder pollingIntervalMs(long pollingIntervalMs) {
|
89 | 115 | this.pollingIntervalMs = pollingIntervalMs;
|
90 | 116 | return this;
|
91 | 117 | }
|
92 | 118 |
|
| 119 | + /** |
| 120 | + * Overrides the host from where it fetches configurations. This typically should not be |
| 121 | + * explicitly set so that the default of the Fastly CDN is used. |
| 122 | + */ |
| 123 | + public Builder host(String host) { |
| 124 | + this.host = host; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
93 | 128 | public EppoClient buildAndInit() {
|
94 | 129 | AppDetails appDetails = AppDetails.getInstance();
|
95 | 130 | String sdkName = appDetails.getName();
|
96 | 131 | String sdkVersion = appDetails.getVersion();
|
97 | 132 |
|
98 | 133 | if (instance != null) {
|
99 |
| - if (forceReinitialize) { // TODO: unit test this |
| 134 | + if (forceReinitialize) { |
100 | 135 | log.warn(
|
101 | 136 | "Eppo SDK is already initialized, reinitializing since forceReinitialize is true");
|
102 | 137 | } else {
|
|
0 commit comments