Skip to content

Commit

Permalink
fix(memory-leak): fix container statistics memory leak (unresolved pr…
Browse files Browse the repository at this point in the history
…omise)
  • Loading branch information
jkuri committed Feb 24, 2018
1 parent a473225 commit 5410c33
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

22 changes: 13 additions & 9 deletions src/api/docker-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function getContainersStats(): Observable<any> {
return listContainers()
.then(containers => Promise.all(containers.map(c => getContainerStats(c))));
})
.map(stats => observer.next({ type: 'containersStats', data: stats }))
.map(stats => observer.next({ type: 'containersStats', data: stats.filter(Boolean) }))
.subscribe();

return () => {
Expand All @@ -25,14 +25,18 @@ export function getContainersStats(): Observable<any> {

function getContainerStats(container: any): Promise<any> {
return calculateContainerStats(container, processes).then(stats => {
return {
id: stats.id,
name: stats.name,
cpu: getCpuData(stats.data),
network: getNetworkData(stats.data),
memory: getMemory(stats.data),
debug: stats.debug
};
if (stats) {
return {
id: stats.id,
name: stats.name,
cpu: getCpuData(stats.data),
network: getNetworkData(stats.data),
memory: getMemory(stats.data),
debug: stats.debug
};
} else {
return null;
}
});
}

Expand Down
54 changes: 21 additions & 33 deletions src/api/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { ProcessOutput } from './process';
import * as envVars from './env-variables';
import chalk from 'chalk';
import * as style from 'ansi-styles';
import { platform } from 'os';

export let docker = new dockerode();
export const docker = new dockerode();
const binds = platform() === 'darwin' ? [] : ['/var/run/docker.sock:/var/run/docker.sock'];

export function createContainer(
name: string,
Expand All @@ -24,7 +26,7 @@ export function createContainer(
OpenStdin: true,
StdinOnce: false,
Env: envVars.serialize(envs) || [],
Binds: ['/var/run/docker.sock:/var/run/docker.sock'],
Binds: binds,
Privileged: true,
ExposedPorts: {
'22/tcp': {},
Expand Down Expand Up @@ -290,37 +292,23 @@ export function calculateContainerStats(
container: dockerode.ContainerInfo,
processes: any
): Promise<any> {
return docker.getContainer(container.Id).stats()
.then(stream => {
let json = '';
return new Promise((resolve, reject) => {
stream.on('data', buf => {
let rawJson = json + buf.toString();
try {
let data = JSON.parse(rawJson);

if (data && data.precpu_stats.system_cpu_usage) {
let jobId = container.Names[0].split('_')[2] || -1;
let job = processes.find(p => p.job_id === Number(jobId));
let debug = false;
if (job) {
debug = job.debug || false;
}

let stats = {
id: container.Id,
name: container.Names[0].substr(1) || '',
debug: debug,
data: data
};
return docker.getContainer(container.Id).stats({ stream: false })
.then(stats => {
const data = stats;
if (data && data.precpu_stats.system_cpu_usage) {
const jobId = container.Names[0].split('_')[2] || -1;
const job = processes.find(p => p.job_id === Number(jobId));
const debug = job && job.debug || false;
const stats = {
id: container.Id,
name: container.Names[0].substr(1) || '',
debug: debug,
data: data
};

stream.destroy();
resolve(stats);
}
} catch (e) {
json = rawJson;
}
});
});
return stats;
} else {
return null;
}
});
}

0 comments on commit 5410c33

Please sign in to comment.