Skip to content

Commit 53c4077

Browse files
authored
feat: Default port to 8015 if in-process resolver is used. #936 (#937)
Signed-off-by: Simon Schrottner <[email protected]>
1 parent c789878 commit 53c4077

File tree

3 files changed

+68
-18
lines changed

3 files changed

+68
-18
lines changed

libs/providers/flagd/README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,23 @@ Options can be defined in the constructor or as environment variables. Construct
2727

2828
### Available Configuration Options
2929

30-
| Option name | Environment variable name | Type | Default | Supported values |
31-
| -------------------------------------- | ------------------------------ | ------- | --------- | ---------------- |
32-
| host | FLAGD_HOST | string | localhost | |
33-
| port | FLAGD_PORT | number | 8013 | |
34-
| tls | FLAGD_TLS | boolean | false | |
35-
| socketPath | FLAGD_SOCKET_PATH | string | - | |
36-
| resolverType | FLAGD_RESOLVER | string | rpc | rpc, in-process |
37-
| offlineFlagSourcePath | FLAGD_OFFLINE_FLAG_SOURCE_PATH | string | - | |
38-
| selector | FLAGD_SOURCE_SELECTOR | string | - | |
39-
| cache | FLAGD_CACHE | string | lru | lru, disabled |
40-
| maxCacheSize | FLAGD_MAX_CACHE_SIZE | int | 1000 | |
30+
| Option name | Environment variable name | Type | Default | Supported values |
31+
| -------------------------------------- | ------------------------------ | ------- |----------------------------------------------------------------| ---------------- |
32+
| host | FLAGD_HOST | string | localhost | |
33+
| port | FLAGD_PORT | number | [resolver specific defaults](#resolver-type-specific-defaults) | |
34+
| tls | FLAGD_TLS | boolean | false | |
35+
| socketPath | FLAGD_SOCKET_PATH | string | - | |
36+
| resolverType | FLAGD_RESOLVER | string | rpc | rpc, in-process |
37+
| offlineFlagSourcePath | FLAGD_OFFLINE_FLAG_SOURCE_PATH | string | - | |
38+
| selector | FLAGD_SOURCE_SELECTOR | string | - | |
39+
| cache | FLAGD_CACHE | string | lru | lru, disabled |
40+
| maxCacheSize | FLAGD_MAX_CACHE_SIZE | int | 1000 | |
41+
42+
#### Resolver type-specific Defaults
43+
44+
| Option name | Environment variable name | in-process | rpc | default |
45+
| -------------------------------------- | ------------------------------ |-------------|------|---------|
46+
| port | FLAGD_PORT | 8015 | 8013 | 8013 |
4147

4248
Below are examples of usage patterns.
4349

@@ -72,7 +78,7 @@ Flag configurations for evaluation are obtained via gRPC protocol using [sync pr
7278
}))
7379
```
7480

75-
In the above example, the provider expects a flag sync service implementation to be available at `localhost:8013` (default host and port).
81+
In the above example, the provider expects a flag sync service implementation to be available at `localhost:8015` (default host and port).
7682

7783
In-process resolver can also work in an offline mode.
7884
To enable this mode, you should provide a valid flag configuration file with the option `offlineFlagSourcePath`.

libs/providers/flagd/src/lib/configuration.spec.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FlagdProviderOptions, getConfig } from './configuration';
1+
import { Config, FlagdProviderOptions, getConfig } from './configuration';
22
import { DEFAULT_MAX_CACHE_SIZE } from './constants';
33

44
describe('Configuration', () => {
@@ -77,4 +77,41 @@ describe('Configuration', () => {
7777
process.env['FLAGD_PORT'] = 'invalid number';
7878
expect(getConfig()).toStrictEqual(expect.objectContaining({ port: 8013 }));
7979
});
80+
81+
describe('port handling', () => {
82+
describe('for "in-process" evaluation', () => {
83+
const resolverType = 'in-process';
84+
const port = 8015;
85+
it('should use default in-process port if resolver type is set per envVar and no port is provided', () => {
86+
process.env['FLAGD_RESOLVER'] = resolverType;
87+
expect(getConfig()).toStrictEqual(expect.objectContaining({ port, resolverType }));
88+
});
89+
it('should use default in-process port if resolver type is set per options and no port is provided', () => {
90+
const options: Partial<Config> = { resolverType };
91+
expect(getConfig(options)).toStrictEqual(expect.objectContaining({ port, resolverType }));
92+
});
93+
it('should use provided port if resolver type is set per options and port', () => {
94+
const port = 1111;
95+
const options: Partial<Config> = { resolverType, port };
96+
expect(getConfig(options)).toStrictEqual(expect.objectContaining({ port, resolverType }));
97+
});
98+
});
99+
describe('for "rpc" evaluation', () => {
100+
const resolverType = 'rpc';
101+
const port = 8013;
102+
it('should use default in-process port if resolver type is set per envVar and no port is provided', () => {
103+
process.env['FLAGD_RESOLVER'] = resolverType;
104+
expect(getConfig()).toStrictEqual(expect.objectContaining({ port, resolverType }));
105+
});
106+
it('should use default in-process port if resolver type is set per options and no port is provided', () => {
107+
const options: Partial<Config> = { resolverType };
108+
expect(getConfig(options)).toStrictEqual(expect.objectContaining({ port, resolverType }));
109+
});
110+
it('should use provided port if resolver type is set per options and port', () => {
111+
const port = 1111;
112+
const options: Partial<Config> = { resolverType, port };
113+
expect(getConfig(options)).toStrictEqual(expect.objectContaining({ port, resolverType }));
114+
});
115+
});
116+
});
80117
});

libs/providers/flagd/src/lib/configuration.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,18 @@ export interface Config {
7272

7373
export type FlagdProviderOptions = Partial<Config>;
7474

75-
const DEFAULT_CONFIG: Config = {
75+
const DEFAULT_CONFIG: Omit<Config, 'port' | 'resolverType'> = {
7676
host: 'localhost',
77-
port: 8013,
7877
tls: false,
79-
resolverType: 'rpc',
8078
selector: '',
8179
cache: 'lru',
8280
maxCacheSize: DEFAULT_MAX_CACHE_SIZE,
8381
};
8482

83+
const DEFAULT_RPC_CONFIG: Config = { ...DEFAULT_CONFIG, resolverType: 'rpc', port: 8013 };
84+
85+
const DEFAULT_IN_PROCESS_CONFIG: Config = { ...DEFAULT_CONFIG, resolverType: 'in-process', port: 8015 };
86+
8587
enum ENV_VAR {
8688
FLAGD_HOST = 'FLAGD_HOST',
8789
FLAGD_PORT = 'FLAGD_PORT',
@@ -125,9 +127,14 @@ const getEnvVarConfig = (): Partial<Config> => ({
125127
});
126128

127129
export function getConfig(options: FlagdProviderOptions = {}) {
130+
const envVarConfig = getEnvVarConfig();
131+
const defaultConfig =
132+
options.resolverType == 'in-process' || envVarConfig.resolverType == 'in-process'
133+
? DEFAULT_IN_PROCESS_CONFIG
134+
: DEFAULT_RPC_CONFIG;
128135
return {
129-
...DEFAULT_CONFIG,
130-
...getEnvVarConfig(),
136+
...defaultConfig,
137+
...envVarConfig,
131138
...options,
132139
};
133140
}

0 commit comments

Comments
 (0)