Skip to content

Commit

Permalink
Implement http timeout on webUI
Browse files Browse the repository at this point in the history
  • Loading branch information
andrevmatos committed Sep 1, 2017
1 parent cf275b0 commit 0c160ec
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

import { RaidenConfig } from '../../services/raiden.config';
import { RaidenService } from '../../services/raiden.service';
import { Event, EventsParam } from '../../models/event';

const INTERVAL = 5000;
const BLOCK_START = 1509634; // block where the registry contract was deployed

@Component({
selector: 'app-event-list',
Expand All @@ -21,10 +20,13 @@ export class EventListComponent implements OnInit {
public events$: Observable<Event[]>;
public loading = true;

constructor(private raidenService: RaidenService) { }
constructor(
private raidenConfig: RaidenConfig,
private raidenService: RaidenService
) { }

ngOnInit() {
let fromBlock: number = BLOCK_START;
let fromBlock: number = this.raidenConfig.config.block_start;
let first = true;
const data_excl = ['event_type', 'block_number', 'timestamp'];
const firerSub: BehaviorSubject<void> = new BehaviorSubject(null);
Expand Down Expand Up @@ -72,7 +74,7 @@ export class EventListComponent implements OnInit {
}
return events;
}, [])
.do(() => setTimeout(() => firerSub.next(null), INTERVAL))
.do(() => setTimeout(() => firerSub.next(null), this.raidenConfig.config.poll_interval))
.do(() => this.loading = false);
}
}
21 changes: 16 additions & 5 deletions raiden/ui/web/src/app/services/raiden.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ declare var Web3;
interface RDNConfig {
raiden: string;
web3: string;
web3_fallback?: string;
poll_interval?: number;
block_start?: number;
http_timeout?: number;
};

const WEB3_FALLBACK = 'http://localhost:8545';
const default_config: RDNConfig = {
raiden: '/api/1',
web3: '/web3',
web3_fallback: 'http://localhost:8545',
poll_interval: 5000,
block_start: 1509634,
http_timeout: 120000,
};

@Injectable()
export class RaidenConfig {
public config: RDNConfig;
public config: RDNConfig = default_config;
public api: string;
public web3: any;

Expand All @@ -21,15 +32,15 @@ export class RaidenConfig {
return new Promise((resolve) => {
this.http.get<RDNConfig>(url)
.subscribe((config) => {
this.config = config;
this.config = Object.assign({}, default_config, config);
this.api = this.config.raiden;
this.web3 = new Web3(new Web3.providers.HttpProvider(this.config.web3));
// make a simple test call to web3
this.web3.version.getNetwork((err, res) => {
if (err) {
console.error('Invalid web3 endpoint', err);
console.info('Switching to fallback: ' + WEB3_FALLBACK);
this.config.web3 = WEB3_FALLBACK;
console.info('Switching to fallback: ' + this.config.web3_fallback);
this.config.web3 = this.config.web3_fallback;
this.web3 = new Web3(new Web3.providers.HttpProvider(this.config.web3));
}
resolve();
Expand Down
52 changes: 28 additions & 24 deletions raiden/ui/web/src/app/services/raiden.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,38 @@ export class RaidenService {

constructor(
private http: HttpClient,
private config: RaidenConfig,
private zone: NgZone,
private raidenConfig: RaidenConfig,
private sharedService: SharedService,
private zone: NgZone) {
this.tokenContract = this.config.web3.eth.contract(tokenabi);
) {
this.tokenContract = this.raidenConfig.web3.eth.contract(tokenabi);
}

private zoneEncap(cb: CallbackFunc): CallbackFunc {
return (err, res) => this.zone.run(() => cb(err, res));
}

private httpEncap<T>(req: Observable<T>): Observable<T> {
return Observable.of(true)
let obs = Observable.of(true)
.do(() => this.requestStarted(req))
.switchMap(() => req)
.finally(() => this.requestFinished(req));
.switchMap(() => req);
if (this.raidenConfig.config.http_timeout) {
obs = obs.timeout(this.raidenConfig.config.http_timeout);
}
return obs.finally(() => this.requestFinished(req));
}

get identifier(): number {
return this._identifier++;
}

get blockNumber(): number {
return this.config.web3.eth.blockNumber;
return this.raidenConfig.web3.eth.blockNumber;
}

getBlockNumber(): Observable<number> {
return Observable.bindNodeCallback((cb: CallbackFunc) =>
this.config.web3.eth.getBlockNumber(this.zoneEncap(cb)))();
this.raidenConfig.web3.eth.getBlockNumber(this.zoneEncap(cb)))();
}

requestStarted(req) {
Expand All @@ -65,18 +69,18 @@ export class RaidenService {
}

public getRaidenAddress(): Observable<string> {
return this.httpEncap(this.http.get<{ our_address: string }>(`${this.config.api}/address`))
return this.httpEncap(this.http.get<{ our_address: string }>(`${this.raidenConfig.api}/address`))
.map((data) => this.raidenAddress = data.our_address)
.catch((error) => this.handleError(error));
}

public getChannels(): Observable<Array<Channel>> {
return this.httpEncap(this.http.get<Array<Channel>>(`${this.config.api}/channels`))
return this.httpEncap(this.http.get<Array<Channel>>(`${this.raidenConfig.api}/channels`))
.catch((error) => this.handleError(error));
}

public getTokensBalances(refresh: boolean = true): Observable<Array<Usertoken>> {
return this.httpEncap(this.http.get<Array<{ address: string }>>(`${this.config.api}/tokens`))
return this.httpEncap(this.http.get<Array<{ address: string }>>(`${this.raidenConfig.api}/tokens`))
.combineLatest(this.getChannels())
.map(([data, channels]): Array<Observable<Usertoken>> => {
const tokenArray = data;
Expand Down Expand Up @@ -111,7 +115,7 @@ export class RaidenService {
'balance': balance,
'settle_timeout': settleTimeout
};
return this.httpEncap(this.http.put<Channel>(`${this.config.api}/channels`, data))
return this.httpEncap(this.http.put<Channel>(`${this.raidenConfig.api}/channels`, data))
.catch((error) => this.handleError(error));
}

Expand All @@ -123,37 +127,37 @@ export class RaidenService {
'amount': amount,
'identifier': this.identifier
};
console.log(`${this.config.api}/transfers/${tokenAddress}/${partnerAddress}`);
return this.httpEncap(this.http.post(`${this.config.api}/transfers/${tokenAddress}/${partnerAddress}`, data))
console.log(`${this.raidenConfig.api}/transfers/${tokenAddress}/${partnerAddress}`);
return this.httpEncap(this.http.post(`${this.raidenConfig.api}/transfers/${tokenAddress}/${partnerAddress}`, data))
.catch((error) => this.handleError(error));
}

public depositToChannel(channelAddress: string, balance: number): Observable<any> {
const data = {
'balance': balance
};
return this.httpEncap(this.http.patch(`${this.config.api}/channels/${channelAddress}`, data))
return this.httpEncap(this.http.patch(`${this.raidenConfig.api}/channels/${channelAddress}`, data))
.catch((error) => this.handleError(error));
}

public closeChannel(channelAddress: string): Observable<any> {
const data = {
'state': 'closed'
};
return this.httpEncap(this.http.patch(`${this.config.api}/channels/${channelAddress}`, data))
return this.httpEncap(this.http.patch(`${this.raidenConfig.api}/channels/${channelAddress}`, data))
.catch((error) => this.handleError(error));
}

public settleChannel(channelAddress: string): Observable<any> {
const data = {
'state': 'settled'
};
return this.httpEncap(this.http.patch(`${this.config.api}/channels/${channelAddress}`, data))
return this.httpEncap(this.http.patch(`${this.raidenConfig.api}/channels/${channelAddress}`, data))
.catch((error) => this.handleError(error));
}

public registerToken(tokenAddress: string): Observable<Usertoken> {
return this.httpEncap(this.http.put(`${this.config.api}/tokens/${tokenAddress}`, {}))
return this.httpEncap(this.http.put(`${this.raidenConfig.api}/tokens/${tokenAddress}`, {}))
.switchMap(() => this.getUsertoken(tokenAddress)
.map((userToken) => {
if (userToken === null) {
Expand All @@ -169,12 +173,12 @@ export class RaidenService {
const data = {
'funds': funds
}
return this.httpEncap(this.http.put(`${this.config.api}/connection/${tokenAddress}`, data))
return this.httpEncap(this.http.put(`${this.raidenConfig.api}/connection/${tokenAddress}`, data))
.catch((error) => this.handleError(error));
}

public leaveTokenNetwork(tokenAddress: string): Observable<any> {
return this.httpEncap(this.http.delete(`${this.config.api}/connection/${tokenAddress}`))
return this.httpEncap(this.http.delete(`${this.raidenConfig.api}/connection/${tokenAddress}`))
.catch((error) => this.handleError(error));
}

Expand All @@ -197,7 +201,7 @@ export class RaidenService {
if (toBlock) {
params = params.set('to_block', '' + toBlock);
}
return this.httpEncap(this.http.get<Array<Event>>(`${this.config.api}/events/${path}`, { params }))
return this.httpEncap(this.http.get<Array<Event>>(`${this.raidenConfig.api}/events/${path}`, { params }))
.catch((error) => this.handleError(error));
}

Expand All @@ -209,7 +213,7 @@ export class RaidenService {
receiving_token: swap.receiving_token,
receiving_amount: swap.receiving_amount,
};
return this.httpEncap(this.http.put(`${this.config.api}/token_swaps/${swap.partner_address}/${swap.identifier}`,
return this.httpEncap(this.http.put(`${this.raidenConfig.api}/token_swaps/${swap.partner_address}/${swap.identifier}`,
data, { observe: 'response'}))
.switchMap((response) => response.ok ?
Observable.of(true) :
Expand All @@ -218,12 +222,12 @@ export class RaidenService {
}

public sha3(data: string): string {
return this.config.web3.sha3(data, { encoding: 'hex' });
return this.raidenConfig.web3.sha3(data, { encoding: 'hex' });
}

public blocknumberToDate(block: number): Observable<Date> {
return Observable.bindNodeCallback((b: number, cb: CallbackFunc) =>
this.config.web3.eth.getBlock(b, this.zoneEncap(cb)))(block)
this.raidenConfig.web3.eth.getBlock(b, this.zoneEncap(cb)))(block)
.map((blk) => new Date(blk.timestamp * 1000))
.first();
}
Expand Down
1 change: 1 addition & 0 deletions raiden/ui/web/src/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import 'rxjs/add/observable/zip';
import 'rxjs/add/observable/bindNodeCallback';

import 'rxjs/add/operator/first';
import 'rxjs/add/operator/timeout';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/map';
Expand Down

0 comments on commit 0c160ec

Please sign in to comment.