Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable ws lives #1

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pusher-js",
"version": "8.0.1",
"name": "@goguardian/pusher-js",
"version": "0.0.1",
"description": "Pusher Channels JavaScript library for browsers, React Native, NodeJS and web workers",
"main": "dist/node/pusher.js",
"browser": "dist/web/pusher.js",
Expand Down
6 changes: 5 additions & 1 deletion src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface Config {
wsPath: string;
wsPort: number;
wssPort: number;
wsLives: number;
userAuthenticator: UserAuthenticationHandler;
channelAuthorizer: ChannelAuthorizationHandler;

Expand All @@ -48,6 +49,7 @@ export interface Config {
ignoreNullOrigin?: boolean;
nacl?: nacl;
timelineParams?: any;
reportDeathCallback?: (info: Object) => void;
}

// getConfig mainly sets the defaults for the options that are not provided
Expand All @@ -64,14 +66,16 @@ export function getConfig(opts: Options, pusher): Config {
wsPath: opts.wsPath || Defaults.wsPath,
wsPort: opts.wsPort || Defaults.wsPort,
wssPort: opts.wssPort || Defaults.wssPort,
wsLives: opts.wsLives === undefined ? Defaults.wsLives : opts.wsLives,

enableStats: getEnableStatsConfig(opts),
httpHost: getHttpHost(opts),
useTLS: shouldUseTLS(opts),
wsHost: getWebsocketHost(opts),

userAuthenticator: buildUserAuthenticator(opts),
channelAuthorizer: buildChannelAuthorizer(opts, pusher)
channelAuthorizer: buildChannelAuthorizer(opts, pusher),
reportDeathCallback: opts.reportDeathCallback
};

if ('disabledTransports' in opts)
Expand Down
2 changes: 2 additions & 0 deletions src/core/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface DefaultConfig {
wsPort: number;
wssPort: number;
wsPath: string;
wsLives: number;
httpHost: string;
httpPort: number;
httpsPort: number;
Expand All @@ -35,6 +36,7 @@ var Defaults: DefaultConfig = {
wsPort: 80,
wssPort: 443,
wsPath: '',
wsLives: 2,
// DEPRECATED: SockJS fallback parameters
httpHost: 'sockjs.pusher.com',
httpPort: 80,
Expand Down
2 changes: 2 additions & 0 deletions src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export interface Options {
wsPath?: string;
wsPort?: number;
wssPort?: number;
wsLives?: number;
reportDeathCallback?: (info: Object) => void;
}

export function validateOptions(options) {
Expand Down
11 changes: 9 additions & 2 deletions src/core/transports/assistant_to_the_transport_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,19 @@ export default class AssistantToTheTransportManager {

if (closeEvent.code === 1002 || closeEvent.code === 1003) {
// we don't want to use transports not obeying the protocol
this.manager.reportDeath();
this.manager.reportDeath({
reason: 'close code',
close_code: closeEvent.code
});
} else if (!closeEvent.wasClean && openTimestamp) {
// report deaths only for short-living transport
var lifespan = Util.now() - openTimestamp;
if (lifespan < 2 * this.maxPingDelay) {
this.manager.reportDeath();
this.manager.reportDeath({
reason: 'closed uncleanly',
close_code: closeEvent.code,
lifespan
});
this.pingDelay = Math.max(lifespan / 2, this.minPingDelay);
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/core/transports/transport_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Factory from '../utils/factory';

export interface TransportManagerOptions extends PingDelayOptions {
lives?: number;
transportType?: string;
reportDeathCallback?: (info: Object) => void;
}

/** Keeps track of the number of lives left for a transport.
Expand All @@ -19,6 +21,7 @@ export interface TransportManagerOptions extends PingDelayOptions {
export default class TransportManager {
options: TransportManagerOptions;
livesLeft: number;
reportDeathCallback?: (info: Object) => void;

constructor(options: TransportManagerOptions) {
this.options = options || {};
Expand Down Expand Up @@ -46,7 +49,15 @@ export default class TransportManager {
}

/** Takes one life from the transport. */
reportDeath() {
reportDeath(info: Object = {}) {
this.livesLeft -= 1;

if (typeof this.options.reportDeathCallback === 'function') {
this.options.reportDeathCallback({
...info,
transport_type: this.options.transportType || 'unknown',
lives_left: this.livesLeft
});
}
}
}
10 changes: 7 additions & 3 deletions src/runtimes/isomorphic/default_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,18 @@ var getDefaultStrategy = function(
};

var ws_manager = new TransportManager({
lives: 2,
transportType: 'ws',
lives: config.wsLives,
minPingDelay: 10000,
maxPingDelay: config.activityTimeout
maxPingDelay: config.activityTimeout,
reportDeathCallback: config.reportDeathCallback
});
var streaming_manager = new TransportManager({
transportType: 'streaming',
lives: 2,
minPingDelay: 10000,
maxPingDelay: config.activityTimeout
maxPingDelay: config.activityTimeout,
reportDeathCallback: config.reportDeathCallback
});

var ws_transport = defineTransportStrategy(
Expand Down
10 changes: 7 additions & 3 deletions src/runtimes/web/default_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,18 @@ var getDefaultStrategy = function(
};

var ws_manager = new TransportManager({
lives: 2,
transportType: 'ws',
lives: config.wsLives,
minPingDelay: 10000,
maxPingDelay: config.activityTimeout
maxPingDelay: config.activityTimeout,
reportDeathCallback: config.reportDeathCallback
});
var streaming_manager = new TransportManager({
transportType: 'streaming',
lives: 2,
minPingDelay: 10000,
maxPingDelay: config.activityTimeout
maxPingDelay: config.activityTimeout,
reportDeathCallback: config.reportDeathCallback
});

var ws_transport = defineTransportStrategy(
Expand Down
2 changes: 2 additions & 0 deletions types/src/core/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Config {
wsPath: string;
wsPort: number;
wssPort: number;
wsLives: number;
userAuthenticator: UserAuthenticationHandler;
channelAuthorizer: ChannelAuthorizationHandler;
forceTLS?: boolean;
Expand All @@ -27,5 +28,6 @@ export interface Config {
ignoreNullOrigin?: boolean;
nacl?: nacl;
timelineParams?: any;
reportDeathCallback?: (info: Object) => void;
dkarella marked this conversation as resolved.
Show resolved Hide resolved
}
export declare function getConfig(opts: Options, pusher: any): Config;
1 change: 1 addition & 0 deletions types/src/core/defaults.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface DefaultConfig {
wsPort: number;
wssPort: number;
wsPath: string;
wsLives: number;
httpHost: string;
httpPort: number;
httpsPort: number;
Expand Down
2 changes: 2 additions & 0 deletions types/src/core/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ export interface Options {
wsPath?: string;
wsPort?: number;
wssPort?: number;
wsLives?: number;
reportDeathCallback?: (info: Object) => void;
}
export declare function validateOptions(options: any): void;
5 changes: 4 additions & 1 deletion types/src/core/transports/transport_manager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import Transport from './transport';
import PingDelayOptions from './ping_delay_options';
export interface TransportManagerOptions extends PingDelayOptions {
lives?: number;
transportType?: string;
reportDeathCallback?: (info: Object) => void;
}
export default class TransportManager {
options: TransportManagerOptions;
livesLeft: number;
reportDeathCallback?: (info: Object) => void;
constructor(options: TransportManagerOptions);
getAssistant(transport: Transport): AssistantToTheTransportManager;
isAlive(): boolean;
reportDeath(): void;
reportDeath(info?: Object): void;
}
4 changes: 3 additions & 1 deletion webpack/hosting_config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const officialPusherJSVersion = '8.0.1';

module.exports = {
version: process.env.VERSION || require('../package').version,
version: officialPusherJSVersion,
cdn_http: process.env.CDN_HTTP || 'http://js.pusher.com',
cdn_https: process.env.CDN_HTTPS || 'https://js.pusher.com',
dependency_suffix: process.env.MINIFY ? '.min' : ''
Expand Down