Skip to content

Commit

Permalink
Reapply "feat: automatically generated instanceId (#637)" (#640) (#641)
Browse files Browse the repository at this point in the history
This reverts commit a3f3326.
  • Loading branch information
nunogois authored Jul 10, 2024
1 parent a317f80 commit 5b9da4e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 32 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ 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 @@ -230,6 +229,10 @@ 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 @@ -379,13 +382,12 @@ 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 @@ -404,7 +406,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 @@ -417,7 +419,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: 0 additions & 18 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { userInfo, hostname } from 'os';
import * as murmurHash3 from 'murmurhash3js';
import { Context } from './context';

Expand Down Expand Up @@ -26,23 +25,6 @@ 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: 0 additions & 1 deletion src/unleash-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { RepositoryInterface } from './repository';
export interface UnleashConfig {
appName: string;
environment?: string;
instanceId?: string;
url: string;
refreshInterval?: number;
projectName?: string;
Expand Down
11 changes: 3 additions & 8 deletions src/unleash.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { tmpdir } from 'os';
import { randomUUID } from 'crypto';
import { EventEmitter } from 'events';
import Client from './client';
import Repository, { RepositoryInterface } from './repository';
Expand All @@ -8,12 +9,7 @@ import { Strategy, defaultStrategies } from './strategy';

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

const unleashUrl = this.cleanUnleashUrl(url);

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

this.staticContext = { appName, environment };

Expand Down

0 comments on commit 5b9da4e

Please sign in to comment.