Skip to content

Commit

Permalink
Merge pull request steemit#2790 from steemit/332-reduce-ssr-fs-reads
Browse files Browse the repository at this point in the history
reduce server-side fs reads, refs steemit#332
  • Loading branch information
gl2748 authored Jun 4, 2018
2 parents ad59ada + 8be90f9 commit 9d0db5c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
38 changes: 12 additions & 26 deletions src/server/app_render.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,19 @@ import { serverRender } from '../shared/UniversalRender';
import models from 'db/models';
import secureRandom from 'secure-random';
import ErrorPage from 'server/server-error';
import fs from 'fs';
import { determineViewMode } from '../app/utils/Links';
import { getSupportedLocales } from './utils/misc';

const path = require('path');
const ROOT = path.join(__dirname, '../..');
const DB_RECONNECT_TIMEOUT =
process.env.NODE_ENV === 'development' ? 1000 * 60 * 60 : 1000 * 60 * 10;

function getSupportedLocales() {
const locales = [];
const files = fs.readdirSync(path.join(ROOT, 'src/app/locales'));
for (const filename of files) {
const match_res = filename.match(/(\w+)\.json?$/);
if (match_res) locales.push(match_res[1]);
}
return locales;
}

const supportedLocales = getSupportedLocales();

async function appRender(ctx) {
async function appRender(ctx, locales = false, resolvedAssets = false) {
ctx.state.requestTimer.startTimer('appRender_ms');

const store = {};

// This is the part of SSR where we make session-specific changes:
try {
let userPreferences = {};
Expand All @@ -48,6 +36,7 @@ async function appRender(ctx) {
if (!userPreferences.locale) {
let locale = ctx.getLocaleFromHeader();
if (locale) locale = locale.substring(0, 2);
const supportedLocales = locales ? locales : getSupportedLocales();
const localeIsSupported = supportedLocales.find(l => l === locale);
if (!localeIsSupported) locale = 'en';
userPreferences.locale = locale;
Expand Down Expand Up @@ -75,7 +64,6 @@ async function appRender(ctx) {
}
}
// ... and that's the end of user-session-related SSR

const initial_state = {
app: {
viewMode: determineViewMode(ctx.request.search),
Expand All @@ -91,19 +79,17 @@ async function appRender(ctx) {
ctx.state.requestTimer
);

// Assets name are found in `webpack-stats` file
const assets_filename =
ROOT +
(process.env.NODE_ENV === 'production'
? '/tmp/webpack-stats-prod.json'
: '/tmp/webpack-stats-dev.json');
const assets = require(assets_filename);

// Don't cache assets name on dev
if (process.env.NODE_ENV === 'development') {
let assets;
// If resolvedAssets argument parameter is falsey we infer that we are in
// development mode and therefore resolve the assets on each render.
if (!resolvedAssets) {
// Assets name are found in `webpack-stats` file
const assets_filename = ROOT + '/tmp/webpack-stats-dev.json';
assets = require(assets_filename);
delete require.cache[require.resolve(assets_filename)];
} else {
assets = resolvedAssets;
}

const props = { body, assets, title, meta };
ctx.status = statusCode;
ctx.body =
Expand Down
16 changes: 14 additions & 2 deletions src/server/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import path from 'path';
import fs from 'fs';
import Koa from 'koa';
import mount from 'koa-mount';
import helmet from 'koa-helmet';
Expand Down Expand Up @@ -28,6 +27,7 @@ import { routeRegex } from 'app/ResolveRoute';
import secureRandom from 'secure-random';
import userIllegalContent from 'app/utils/userIllegalContent';
import koaLocale from 'koa-locale';
import { getSupportedLocales } from './utils/misc';

if (cluster.isMaster) console.log('application server starting, please wait.');

Expand Down Expand Up @@ -79,6 +79,18 @@ if (env === 'development') {
);
}

let resolvedAssets = false;
let supportedLocales = false;

if (process.env.NODE_ENV === 'production') {
resolvedAssets = require(path.join(
__dirname,
'../..',
'/tmp/webpack-stats-prod.json'
));
supportedLocales = getSupportedLocales();
}

app.use(isBot());

// set number of processes equal to number of cores
Expand Down Expand Up @@ -265,7 +277,7 @@ if (env === 'production') {
if (env !== 'test') {
const appRender = require('./app_render');
app.use(function*() {
yield appRender(this);
yield appRender(this, supportedLocales, resolvedAssets);
// if (app_router.dbStatus.ok) recordWebEvent(this, 'page_load');
const bot = this.state.isBot;
if (bot) {
Expand Down
15 changes: 15 additions & 0 deletions src/server/utils/misc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path';
import fs from 'fs';
import { esc } from 'db/models';

const emailRegex = /^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/;
Expand Down Expand Up @@ -57,9 +59,22 @@ function checkCSRF(ctx, csrf) {
return true;
}

function getSupportedLocales() {
const locales = [];
const files = fs.readdirSync(
path.join(__dirname, '../../..', 'src/app/locales')
);
for (const filename of files) {
const match_res = filename.match(/(\w+)\.json?$/);
if (match_res) locales.push(match_res[1]);
}
return locales;
}

module.exports = {
emailRegex,
getRemoteIp,
rateLimitReq,
checkCSRF,
getSupportedLocales,
};

0 comments on commit 9d0db5c

Please sign in to comment.