Skip to content

Commit

Permalink
Revert "feat: automatically generated instanceId (#637)" (#640)
Browse files Browse the repository at this point in the history
This reverts commit 279cd2e.
  • Loading branch information
nunogois committed Jul 10, 2024
1 parent 25040fd commit a3f3326
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ The initialize method takes the following arguments:
- **environment** - The value to put in the Unleash context's `environment` property. Automatically
populated in the Unleash Context (optional). This does **not** set the SDK's
[Unleash environment](https://docs.getunleash.io/reference/environments).
- **instanceId** - A unique identifier, should/could be somewhat unique.
- **refreshInterval** - The poll interval to check for updates. Defaults to 15000ms.
- **metricsInterval** - How often the client should send metrics to Unleash API. Defaults to
60000ms.
Expand All @@ -229,10 +230,6 @@ The initialize method takes the following arguments:
- **tags** - Only fetch feature toggles tagged with the list of tags. E.g.:
`[{type: 'simple', value: 'proxy'}]`.

### instanceId

As of version 5.0.0, `instanceId` is now automatically generated.

## Custom strategies

### 1. implement the custom strategy:
Expand Down Expand Up @@ -382,12 +379,13 @@ import {
initialize,
getFeatureToggleDefinition,
getFeatureToggleDefinitions,
} from 'unleash-client';
} from "unleash-client";

initialize({
url: 'http://unleash.herokuapp.com/api/',
customHeaders: { Authorization: '<YOUR_API_TOKEN>' },
appName: 'my-app-name',
instanceId: 'my-unique-instance-id',
});

const featureToggleX = getFeatureToggleDefinition('app.ToggleX');
Expand All @@ -406,7 +404,7 @@ provider or a custom store provider implemented by you.
**1. Use InMemStorageProvider**

```js
import { initialize, InMemStorageProvider } from 'unleash-client';
import { initialize, InMemStorageProvider } from "unleash-client";

const client = initialize({
appName: 'my-application',
Expand All @@ -419,7 +417,7 @@ const client = initialize({
**2. Custom Store Provider backed by redis**

```js
import { initialize, InMemStorageProvider } from 'unleash-client';
import { initialize, InMemStorageProvider } from "unleash-client";

import { createClient } from 'redis';

Expand Down
18 changes: 18 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { userInfo, hostname } from 'os';
import * as murmurHash3 from 'murmurhash3js';
import { Context } from './context';

Expand Down Expand Up @@ -25,6 +26,23 @@ export function safeName(str: string = '') {
return str.replace(/\//g, '_');
}

export function generateInstanceId(instanceId?: string): string {
if (instanceId) {
return instanceId;
}
let info;
try {
info = userInfo();
} catch (e) {
// unable to read info;
}

const prefix = info
? info.username
: `generated-${Math.round(Math.random() * 1000000)}-${process.pid}`;
return `${prefix}-${hostname()}`;
}

export function generateHashOfConfig(o: Object): string {
const oAsString = JSON.stringify(o);
return murmurHash3.x86.hash128(oAsString);
Expand Down
1 change: 1 addition & 0 deletions src/unleash-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { RepositoryInterface } from './repository';
export interface UnleashConfig {
appName: string;
environment?: string;
instanceId?: string;
url: string;
refreshInterval?: number;
projectName?: string;
Expand Down
11 changes: 8 additions & 3 deletions src/unleash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { tmpdir } from 'os';
import { randomUUID } from 'crypto';
import { EventEmitter } from 'events';
import Client from './client';
import Repository, { RepositoryInterface } from './repository';
Expand All @@ -9,7 +8,12 @@ import { Strategy, defaultStrategies } from './strategy';

import { EnhancedFeatureInterface, FeatureInterface } from './feature';
import { Variant, defaultVariant, VariantWithFeatureStatus } from './variant';
import { FallbackFunction, createFallbackFunction, generateHashOfConfig } from './helpers';
import {
FallbackFunction,
createFallbackFunction,
generateInstanceId,
generateHashOfConfig,
} from './helpers';
import { resolveBootstrapProvider } from './repository/bootstrap-provider';
import { ImpressionEvent, UnleashEvents } from './events';
import { UnleashConfig } from './unleash-config';
Expand Down Expand Up @@ -49,6 +53,7 @@ export class Unleash extends EventEmitter {
appName,
environment = 'default',
projectName,
instanceId,
url,
refreshInterval = 15 * 1000,
metricsInterval = 60 * 1000,
Expand Down Expand Up @@ -96,7 +101,7 @@ export class Unleash extends EventEmitter {

const unleashUrl = this.cleanUnleashUrl(url);

const unleashInstanceId = randomUUID();
const unleashInstanceId = generateInstanceId(instanceId);

this.staticContext = { appName, environment };

Expand Down

0 comments on commit a3f3326

Please sign in to comment.