Skip to content

Commit

Permalink
moving request config and peers variables to be passed in as params i…
Browse files Browse the repository at this point in the history
…n app init (#330)
  • Loading branch information
just-at-uber authored May 26, 2021
1 parent 3340ff0 commit c87ab9a
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,20 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

const PEERS_DEFAULT = ['127.0.0.1:7933'];
const PEERS_DEFAULT = '127.0.0.1:7933';

const REQUEST_RETRY_FLAGS = { onConnectionError: true };
const REQUEST_RETRY_FLAGS_DEFAULT = { onConnectionError: true };

const REQUEST_RETRY_LIMIT_DEFAULT = 3;

const REQUEST_TIMEOUT = 1000 * 60 * 5;
const REQUEST_TIMEOUT_DEFAULT = 1000 * 60 * 5;

const SERVICE_NAME_DEFAULT = 'cadence-frontend';

const REQUEST_CONFIG_DEFAULTS = {
serviceName: SERVICE_NAME_DEFAULT,
timeout: REQUEST_TIMEOUT,
retryFlags: REQUEST_RETRY_FLAGS,
retryLimit: REQUEST_RETRY_LIMIT_DEFAULT,
};

module.exports = {
PEERS_DEFAULT,
REQUEST_CONFIG_DEFAULTS,
REQUEST_RETRY_FLAGS,
REQUEST_RETRY_FLAGS_DEFAULT,
REQUEST_RETRY_LIMIT_DEFAULT,
REQUEST_TIMEOUT,
REQUEST_TIMEOUT_DEFAULT,
SERVICE_NAME_DEFAULT,
};
34 changes: 25 additions & 9 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,35 @@ const webpack = require('webpack');
const webpackConfig = require('../webpack.config');
const tchannelClient = require('./middleware/tchannel-client');
const router = require('./router');
const {
PEERS_DEFAULT,
REQUEST_RETRY_FLAGS_DEFAULT,
REQUEST_RETRY_LIMIT_DEFAULT,
REQUEST_TIMEOUT_DEFAULT,
SERVICE_NAME_DEFAULT,
} = require('./constants');

const staticRoot = path.join(__dirname, '../dist');
const app = new Koa();

app.webpackConfig = webpackConfig;

app.init = function(options) {
options = options || {};

const useWebpack =
'useWebpack' in options
? options.useWebpack
: process.env.NODE_ENV !== 'production';
app.init = function({
logErrors,
peers = process.env.CADENCE_TCHANNEL_PEERS || PEERS_DEFAULT,
retryFlags = REQUEST_RETRY_FLAGS_DEFAULT,
retryLimit = process.env.CADENCE_TCHANNEL_RETRY_LIMIT ||
REQUEST_RETRY_LIMIT_DEFAULT,
serviceName = process.env.CADENCE_TCHANNEL_SERVICE || SERVICE_NAME_DEFAULT,
timeout = REQUEST_TIMEOUT_DEFAULT,
useWebpack = process.env.NODE_ENV !== 'production',
} = {}) {
const requestConfig = {
retryFlags,
retryLimit,
serviceName,
timeout,
};

let compiler;

Expand All @@ -57,7 +73,7 @@ app.init = function(options) {
await next();
} catch (err) {
if (
options.logErrors !== false &&
logErrors !== false &&
(typeof err.statusCode !== 'number' || err.statusCode >= 500)
) {
console.error(err);
Expand All @@ -73,7 +89,7 @@ app.init = function(options) {
filter: contentType => !contentType.startsWith('text/event-stream'),
})
)
.use(tchannelClient)
.use(tchannelClient({ peers, requestConfig }))
.use(
useWebpack
? koaWebpack({
Expand Down
9 changes: 3 additions & 6 deletions server/middleware/tchannel-client/helpers/make-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@
const path = require('path');
const isIPv4 = require('is-ipv4-node');
const TChannelAsThrift = require('tchannel/as/thrift');
const { PEERS_DEFAULT } = require('../constants');
const lookupAsync = require('./lookup-async');

const makeChannels = async client => {
const PEERS = process.env.CADENCE_TCHANNEL_PEERS
? process.env.CADENCE_TCHANNEL_PEERS.split(',')
: PEERS_DEFAULT;
const makeChannels = async ({ client, peers }) => {
const peerList = peers.split(',');

const ipPeers = await Promise.all(
PEERS.map(peer => {
peerList.map(peer => {
const [host, port] = peer.split(':');

if (!isIPv4(host)) {
Expand Down
93 changes: 40 additions & 53 deletions server/middleware/tchannel-client/helpers/make-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,53 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

const { REQUEST_CONFIG_DEFAULTS } = require('../constants');
const formatBody = require('./format-body');
const formatMethod = require('./format-method');
const formatRequestName = require('./format-request-name');
const uiTransform = require('./ui-transform');

const makeRequest = ({ authTokenHeaders, channels, ctx }) => {
const REQUEST_CONFIG = {
...REQUEST_CONFIG_DEFAULTS,
...(process.env.CADENCE_TCHANNEL_SERVICE && {
serviceName: process.env.CADENCE_TCHANNEL_SERVICE,
}),
...(process.env.CADENCE_TCHANNEL_RETRY_LIMIT && {
retryLimit: process.env.CADENCE_TCHANNEL_RETRY_LIMIT,
}),
};

return ({
bodyTransform,
channelName = 'cadence',
method,
requestName,
responseTransform,
serviceName = 'WorkflowService',
}) => body =>
new Promise((resolve, reject) => {
try {
channels[channelName].request(REQUEST_CONFIG).send(
formatMethod({ method, serviceName }),
{
...authTokenHeaders,
},
{
[formatRequestName(requestName)]: formatBody({
body,
bodyTransform,
}),
},
(error, response) => {
try {
if (error) {
reject(error);
} else if (response.ok) {
resolve((responseTransform || uiTransform)(response.body));
} else {
ctx.throw(
response.typeName === 'entityNotExistError' ? 404 : 400,
null,
response.body || response
);
}
} catch (error) {
const makeRequest = ({ authTokenHeaders, channels, ctx, requestConfig }) => ({
bodyTransform,
channelName = 'cadence',
method,
requestName,
responseTransform,
serviceName = 'WorkflowService',
}) => body =>
new Promise((resolve, reject) => {
try {
channels[channelName].request(requestConfig).send(
formatMethod({ method, serviceName }),
{
...authTokenHeaders,
},
{
[formatRequestName(requestName)]: formatBody({
body,
bodyTransform,
}),
},
(error, response) => {
try {
if (error) {
reject(error);
} else if (response.ok) {
resolve((responseTransform || uiTransform)(response.body));
} else {
ctx.throw(
response.typeName === 'entityNotExistError' ? 404 : 400,
null,
response.body || response
);
}
} catch (error) {
reject(error);
}
);
} catch (error) {
reject(error);
}
});
};
}
);
} catch (error) {
reject(error);
}
});

module.exports = makeRequest;
Loading

0 comments on commit c87ab9a

Please sign in to comment.