From 6d04b4add30524756c8d42c13b10c7ef0dafe120 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Tue, 19 Sep 2017 15:28:03 -0600 Subject: [PATCH 01/25] Redis Cluster Support - Deprecated unused `redis` options in favour of options from `ioredis`. - Added support for ioredis Cluster. - Enabled more extended redis client configuration. --- config.js | 37 ++++++++++++++++---- docs/_docs/02-01-configuration.md | 19 +++++----- services/redis.js | 58 ++++++++++++------------------- 3 files changed, 64 insertions(+), 50 deletions(-) diff --git a/config.js b/config.js index 1a54ece5a..3d8b6213c 100644 --- a/config.js +++ b/config.js @@ -86,14 +86,17 @@ const CONFIG = { MONGO_URL: process.env.TALK_MONGO_URL, REDIS_URL: process.env.TALK_REDIS_URL, - // REDIS_RECONNECTION_MAX_ATTEMPTS is the amount of attempts that a redis - // connection will attempt to reconnect before aborting with an error. - REDIS_RECONNECTION_MAX_ATTEMPTS: parseInt(process.env.TALK_REDIS_RECONNECTION_MAX_ATTEMPTS || '100'), + // REDIS_CLIENT_CONFIG is the optional configuration that is merged with the + // function config to provide deep control of the redis connection beheviour. + REDIS_CLIENT_CONFIG: process.env.TALK_REDIS_CLIENT_CONFIGURATION || '{}', - // REDIS_RECONNECTION_MAX_RETRY_TIME is the time in string format for the - // maximum amount of time that a client can be considered "connecting" before - // attempts at reconnection are aborted with an error. - REDIS_RECONNECTION_MAX_RETRY_TIME: ms(process.env.TALK_REDIS_RECONNECTION_MAX_RETRY_TIME || '1 min'), + // REDIS_CLUSTER_MODE allows configuration on the type of cluster mode enabled + // on the redis client. Can be either `NONE` or `CLUSTER`. + REDIS_CLUSTER_MODE: process.env.TALK_REDIS_CLUSTER_MODE || 'NONE', + + // REDIS_CLUSTER_CONFIGURATION contains the json string for the redis cluster + // configuration. + REDIS_CLUSTER_CONFIGURATION: process.env.TALK_REDIS_CLUSTER_CONFIGURATION || '[]', // REDIS_RECONNECTION_BACKOFF_FACTOR is the factor that will be multiplied // against the current attempt count inbetween attempts to connect to redis. @@ -238,6 +241,26 @@ if (process.env.NODE_ENV === 'test' && !CONFIG.REDIS_URL) { CONFIG.REDIS_URL = 'redis://localhost/1'; } +// REDIS_CLUSTER_CONFIGURATION should be parsed when the cluster mode !== none. +if (CONFIG.REDIS_CLUSTER_MODE === 'CLUSTER') { + try { + CONFIG.REDIS_CLUSTER_CONFIGURATION = JSON.parse(CONFIG.REDIS_CLUSTER_CONFIGURATION); + } catch (err) { + throw new Error('TALK_REDIS_CLUSTER_CONFIGURATION is not valid JSON, see https://github.com/luin/ioredis#cluster for valid syntax of the list of cluster nodes'); + } + + if (!Array.isArray(CONFIG.REDIS_CLUSTER_CONFIGURATION)) { + throw new Error('TALK_REDIS_CLUSTER_MODE is CLUSTER, but the TALK_REDIS_CLUSTER_CONFIGURATION is invalid, see https://github.com/luin/ioredis#cluster for valid syntax of the list of cluster nodes'); + } + + if (CONFIG.REDIS_CLUSTER_CONFIGURATION.length === 0) { + throw new Error('TALK_REDIS_CLUSTER_CONFIGURATION must have at least one node specified in the cluster, see https://github.com/luin/ioredis#cluster for valid syntax of the list of cluster nodes'); + } +} + +// Client config is a JSON encoded string, defaulting to `{}`. +CONFIG.REDIS_CLIENT_CONFIG = JSON.parse(CONFIG.REDIS_CLIENT_CONFIG); + //------------------------------------------------------------------------------ // Recaptcha configuration //------------------------------------------------------------------------------ diff --git a/docs/_docs/02-01-configuration.md b/docs/_docs/02-01-configuration.md index 021735fb9..b3e637e27 100644 --- a/docs/_docs/02-01-configuration.md +++ b/docs/_docs/02-01-configuration.md @@ -55,18 +55,21 @@ These are only used during the webpack build. #### Advanced {:.no_toc} -- `TALK_REDIS_RECONNECTION_MAX_ATTEMPTS` (_optional_) - the amount of attempts - that a redis connection will attempt to reconnect before aborting with an - error. (Default `100`) -- `TALK_REDIS_RECONNECTION_MAX_RETRY_TIME` (_optional_) - the time in string - format for the maximum amount of time that a client can be considered - "connecting" before attempts at reconnection are aborted with an error. - (Default `1 min`) +- `TALK_REDIS_CLIENT_CONFIG` (_optional_) - configuration overrides for the + redis client configuration in a JSON encoded string. Configuration is + overridden as the second parameter to the redis client constructor, and is + merged with default configuration. (Default `{}`) - `TALK_REDIS_RECONNECTION_BACKOFF_FACTOR` (_optional_) - the time factor that will be multiplied against the current attempt count inbetween attempts to connect to redis. (Default `500 ms`) - `TALK_REDIS_RECONNECTION_BACKOFF_MINIMUM_TIME` (_optional_) - the minimum time used to delay before attempting to reconnect to redis. (Default `1 sec`) +- `TALK_REDIS_CLUSTER_MODE` (_optional_) - the cluster mode of the redis client. + Can be either `NONE` or `CLUSTER`. (Default `NONE`) +- `TALK_REDIS_CLUSTER_CONFIGURATION` (_optional_) - the json serialized form of + the cluster nodes. Only required when `TALK_REDIS_CLUSTER_MODE=CLUSTER`. See + https://github.com/luin/ioredis#cluster for configuration details. + (Default `[]`) ### Server @@ -131,7 +134,7 @@ is not needed in most situations. use to set a cookie containing a JWT that was issued by Talk. (Default `process.env.TALK_JWT_COOKIE_NAME`) - `TALK_JWT_COOKIE_NAMES` (_optional_) - the different cookie names to check for - a JWT token in, seperated by `,`. By default, we always use the + a JWT token in, separated by `,`. By default, we always use the `process.env.TALK_JWT_COOKIE_NAME` and `process.env.TALK_JWT_SIGNING_COOKIE_NAME` for this value. Any additional cookie names specified here will be appended to the list of cookie names to inspect. diff --git a/services/redis.js b/services/redis.js index 5fd1b78d3..9b00b19db 100644 --- a/services/redis.js +++ b/services/redis.js @@ -1,12 +1,14 @@ const Redis = require('ioredis'); +const merge = require('lodash/merge'); const debug = require('debug')('talk:services:redis'); const enabled = require('debug').enabled('talk:services:redis'); const { REDIS_URL, - REDIS_RECONNECTION_MAX_ATTEMPTS, - REDIS_RECONNECTION_MAX_RETRY_TIME, REDIS_RECONNECTION_BACKOFF_FACTOR, REDIS_RECONNECTION_BACKOFF_MINIMUM_TIME, + REDIS_CLIENT_CONFIG, + REDIS_CLUSTER_MODE, + REDIS_CLUSTER_CONFIGURATION, } = require('../config'); const attachMonitors = (client) => { @@ -16,8 +18,8 @@ const attachMonitors = (client) => { if (enabled) { client.on('connect', () => debug('client connected')); client.on('ready', () => debug('client ready')); - client.on('reconnecting', () => debug('client connection lost, attempting to reconnect')); client.on('close', () => debug('client closed the connection')); + client.on('reconnecting', () => debug('client connection lost, attempting to reconnect')); client.on('end', () => debug('client ended')); } @@ -27,44 +29,30 @@ const attachMonitors = (client) => { console.error('Error connecting to redis:', err); } }); + client.on('node error', (err) => debug('node error', err)); }; -const connectionOptions = { - retry_strategy: function(options) { - if (options.error && options.error.code !== 'ECONNREFUSED') { +function retryStrategy(times) { + const delay = Math.max(times * REDIS_RECONNECTION_BACKOFF_FACTOR, REDIS_RECONNECTION_BACKOFF_MINIMUM_TIME); - debug('retry strategy: none, an error occured'); + debug(`retry strategy: try to reconnect ${delay} ms from now`); - // End reconnecting on a specific error and flush all commands with a individual error - return options.error; - } - if (options.total_retry_time > REDIS_RECONNECTION_MAX_RETRY_TIME) { - - debug('retry strategy: none, exhausted retry time'); - - // End reconnecting after a specific timeout and flush all commands with a individual error - return new Error('Retry time exhausted'); - } - - if (options.attempt > REDIS_RECONNECTION_MAX_ATTEMPTS) { - - debug('retry strategy: none, exhausted retry attempts'); - - // End reconnecting with built in error - return undefined; - } - - // reconnect after - const delay = Math.max(options.attempt * REDIS_RECONNECTION_BACKOFF_FACTOR, REDIS_RECONNECTION_BACKOFF_MINIMUM_TIME); - - debug(`retry strategy: try to reconnect ${delay} ms from now`); - - return delay; - } -}; + return delay; +} const createClient = () => { - let client = new Redis(REDIS_URL, connectionOptions); + let client; + if (REDIS_CLUSTER_MODE === 'NONE') { + client = new Redis(REDIS_URL, merge({}, REDIS_CLIENT_CONFIG, { + retryStrategy, + })); + } else if (REDIS_CLUSTER_MODE === 'CLUSTER') { + client = new Redis.Cluster(REDIS_CLUSTER_CONFIGURATION, merge({ + scaleReads: 'slave', + }, REDIS_CLIENT_CONFIG, { + clusterRetryStrategy: retryStrategy, + })); + } // Attach the monitors that will print debug messages to the console. attachMonitors(client); From e7c78a2465054755cfd5a2aa99ce1fbfa34a7284 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Wed, 20 Sep 2017 22:53:04 +0700 Subject: [PATCH 02/25] Pluginize timestamp --- .../src/components/Comment.js | 11 +++++++++-- .../src/containers/Comment.js | 3 ++- .../components}/PubDate.js | 5 +++++ .../containers/ProfileContainer.js | 3 ++- client/talk-plugin-history/Comment.js | 16 +++++++++++----- plugin-api/beta/client/components/index.js | 1 + .../client/components/CreateUsernameDialog.js | 2 +- .../client/components/FakeComment.js | 2 +- .../client/components/Comment.css | 6 +++++- .../client/components/Comment.js | 19 +++++++++++++------ .../client/containers/Comment.js | 1 + 11 files changed, 51 insertions(+), 18 deletions(-) rename client/{talk-plugin-pubdate => coral-framework/components}/PubDate.js (72%) diff --git a/client/coral-embed-stream/src/components/Comment.js b/client/coral-embed-stream/src/components/Comment.js index c6a3b740d..e5272f2db 100644 --- a/client/coral-embed-stream/src/components/Comment.js +++ b/client/coral-embed-stream/src/components/Comment.js @@ -2,7 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import TagLabel from 'talk-plugin-tag-label/TagLabel'; -import PubDate from 'talk-plugin-pubdate/PubDate'; +import PubDate from 'coral-framework/components/PubDate'; import {ReplyBox, ReplyButton} from 'talk-plugin-replies'; import {FlagComment} from 'talk-plugin-flags'; import {can} from 'coral-framework/services/perms'; @@ -465,7 +465,14 @@ export default class Comment extends React.Component { /> - + { (comment.editing && comment.editing.edited) ?  ({t('comment.edited')}) diff --git a/client/coral-embed-stream/src/containers/Comment.js b/client/coral-embed-stream/src/containers/Comment.js index bd3160643..694314bd7 100644 --- a/client/coral-embed-stream/src/containers/Comment.js +++ b/client/coral-embed-stream/src/containers/Comment.js @@ -17,7 +17,8 @@ const slots = [ 'commentReactions', 'commentAvatar', 'commentAuthorName', - 'commentAuthorTags' + 'commentAuthorTags', + 'commentTimestamp', ]; /** diff --git a/client/talk-plugin-pubdate/PubDate.js b/client/coral-framework/components/PubDate.js similarity index 72% rename from client/talk-plugin-pubdate/PubDate.js rename to client/coral-framework/components/PubDate.js index 7afd45e2c..843909852 100644 --- a/client/talk-plugin-pubdate/PubDate.js +++ b/client/coral-framework/components/PubDate.js @@ -1,4 +1,5 @@ import React from 'react'; +import PropTypes from 'prop-types'; import {timeago} from 'coral-framework/services/i18n'; const name = 'talk-plugin-pubdate'; @@ -7,4 +8,8 @@ const PubDate = ({created_at}) =>
{timeago(created_at)}
; +PubDate.propTypes = { + created_at: PropTypes.string, +}; + export default PubDate; diff --git a/client/coral-settings/containers/ProfileContainer.js b/client/coral-settings/containers/ProfileContainer.js index aab0e2a6a..1391fa83f 100644 --- a/client/coral-settings/containers/ProfileContainer.js +++ b/client/coral-settings/containers/ProfileContainer.js @@ -95,8 +95,9 @@ class ProfileContainer extends Component { const slots = [ 'profileSections', - // TODO: This Slot should be included in `talk-plugin-history` instead. + // TODO: These Slots should be included in `talk-plugin-history` instead. 'commentContent', + 'historyCommentTimestamp', ]; const CommentFragment = gql` diff --git a/client/talk-plugin-history/Comment.js b/client/talk-plugin-history/Comment.js index 51ab5c84a..8152e431a 100644 --- a/client/talk-plugin-history/Comment.js +++ b/client/talk-plugin-history/Comment.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import {Icon} from '../coral-ui'; import styles from './Comment.css'; import Slot from 'coral-framework/components/Slot'; -import PubDate from '../talk-plugin-pubdate/PubDate'; +import PubDate from 'coral-framework/components/PubDate'; import CommentContent from '../coral-embed-stream/src/components/CommentContent'; import cn from 'classnames'; import {getTotalReactionsCount} from 'coral-framework/utils'; @@ -15,6 +15,7 @@ class Comment extends React.Component { render() { const {comment, link, data, root} = this.props; const reactionCount = getTotalReactionsCount(comment.action_summaries); + const queryData = {root, comment, asset: comment.asset}; return (
@@ -24,7 +25,7 @@ class Comment extends React.Component { defaultComponent={CommentContent} className={cn(styles.commentBody, 'my-comment-body')} data={data} - queryData={{root, comment, asset: comment.asset}} + queryData={queryData} />
@@ -37,7 +38,7 @@ class Comment extends React.Component { - {comment.replyCount} + {comment.replyCount} {comment.replyCount === 1 ? t('common.reply') : t('common.replies')} @@ -59,9 +60,14 @@ class Comment extends React.Component {
  • -
  • diff --git a/plugin-api/beta/client/components/index.js b/plugin-api/beta/client/components/index.js index 4367d3420..634859e06 100644 --- a/plugin-api/beta/client/components/index.js +++ b/plugin-api/beta/client/components/index.js @@ -3,3 +3,4 @@ export {default as ClickOutside} from 'coral-framework/components/ClickOutside'; export {default as IfSlotIsEmpty} from 'coral-framework/components/IfSlotIsEmpty'; export {default as IfSlotIsNotEmpty} from 'coral-framework/components/IfSlotIsNotEmpty'; export {default as CommentAuthorName} from 'coral-framework/components/CommentAuthorName'; +export {default as PubDate} from 'coral-framework/components/PubDate'; diff --git a/plugins/talk-plugin-auth/client/components/CreateUsernameDialog.js b/plugins/talk-plugin-auth/client/components/CreateUsernameDialog.js index 4b0babed6..5c289c1c7 100644 --- a/plugins/talk-plugin-auth/client/components/CreateUsernameDialog.js +++ b/plugins/talk-plugin-auth/client/components/CreateUsernameDialog.js @@ -31,7 +31,7 @@ const CreateUsernameDialog = ({

    diff --git a/plugins/talk-plugin-auth/client/components/FakeComment.js b/plugins/talk-plugin-auth/client/components/FakeComment.js index 6997ca984..9d195b0ec 100644 --- a/plugins/talk-plugin-auth/client/components/FakeComment.js +++ b/plugins/talk-plugin-auth/client/components/FakeComment.js @@ -1,9 +1,9 @@ import React from 'react'; import t from 'coral-framework/services/i18n'; import {ReplyButton} from 'talk-plugin-replies'; -import PubDate from 'talk-plugin-pubdate/PubDate'; import styles from './FakeComment.css'; import {Icon} from 'plugin-api/beta/client/components/ui'; +import {PubDate} from 'plugin-api/beta/client/components'; export const FakeComment = ({username, created_at, body}) => (

    diff --git a/plugins/talk-plugin-featured-comments/client/components/Comment.css b/plugins/talk-plugin-featured-comments/client/components/Comment.css index b800401e3..9481b4396 100644 --- a/plugins/talk-plugin-featured-comments/client/components/Comment.css +++ b/plugins/talk-plugin-featured-comments/client/components/Comment.css @@ -64,4 +64,8 @@ .actionsContainer { text-align: right; -} \ No newline at end of file +} + +.timestamp { + padding-left: 6px; +} diff --git a/plugins/talk-plugin-featured-comments/client/components/Comment.js b/plugins/talk-plugin-featured-comments/client/components/Comment.js index 779844177..8c355192f 100644 --- a/plugins/talk-plugin-featured-comments/client/components/Comment.js +++ b/plugins/talk-plugin-featured-comments/client/components/Comment.js @@ -1,8 +1,8 @@ import React from 'react'; import cn from 'classnames'; import styles from './Comment.css'; -import {t, timeago} from 'plugin-api/beta/client/services'; -import {Slot, CommentAuthorName} from 'plugin-api/beta/client/components'; +import {t} from 'plugin-api/beta/client/services'; +import {Slot, CommentAuthorName, PubDate} from 'plugin-api/beta/client/components'; import {Icon} from 'plugin-api/beta/client/components/ui'; import {pluginName} from '../../package.json'; import FeaturedButton from '../containers/FeaturedButton'; @@ -15,6 +15,7 @@ class Comment extends React.Component { render() { const {comment, asset, root, data} = this.props; + const queryData = {comment, asset, root}; return (
    @@ -28,14 +29,20 @@ class Comment extends React.Component { className={cn(styles.username, `${pluginName}-comment-username`)} fill="commentAuthorName" defaultComponent={CommentAuthorName} - queryData={{comment, asset, root}} + queryData={queryData} data={data} inline /> - - ,{' '}{timeago(comment.created_at)} - +