Skip to content

Optimize Translation#53

Merged
hangyiw merged 4 commits into
mainfrom
optimize-translation
Nov 10, 2025
Merged

Optimize Translation#53
hangyiw merged 4 commits into
mainfrom
optimize-translation

Conversation

@yza-cmu

@yza-cmu yza-cmu commented Nov 9, 2025

Copy link
Copy Markdown

Improvements:

  • Asynchronous Translation: Translation now happens in the background and updates the post when complete. This leads to instant post creation and removes the "block" posts received when waiting for translation
  • Request Timeout: Added timeout for translation to prevent indefinite hanging on slow API responses.
  • URL Encoding Fix: Fixed bug where special characters such as '=', '#', '?' broke translation API calls
  • Improved the overall error handling when translation fails.

This PR impacts performance, reliability and fixes bugs.

@yza-cmu yza-cmu self-assigned this Nov 9, 2025
@yza-cmu yza-cmu added the enhancement New feature or request label Nov 9, 2025
@coveralls

coveralls commented Nov 9, 2025

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 19223695342

Details

  • 10 of 14 (71.43%) changed or added relevant lines in 2 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.01%) to 78.172%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/posts/create.js 4 6 66.67%
src/translate/index.js 6 8 75.0%
Files with Coverage Reduction New Missed Lines %
src/controllers/admin/info.js 2 71.76%
Totals Coverage Status
Change from base Build 19210302143: -0.01%
Covered Lines: 24874
Relevant Lines: 29960

💛 - Coveralls

The 3-second timeout was too aggressive for the translation API,
especially for longer posts. Increasing to 10 seconds provides more
headroom while still preventing indefinite hangs
@hangyiw hangyiw self-requested a review November 10, 2025 07:13

@hangyiw hangyiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Let me look deeper into the details

Comment thread src/posts/create.js
const [isEnglish, translatedContent] = await translate.translate(data);

// Set defaults immediately - translation will happen in background
const isEnglish = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good: Setting immediate defaults ensures posts are created quickly without waiting for the translation API. This significantly improves perceived performance.

Comment thread src/posts/create.js
const pid = data.pid || await db.incrObjectField('global', 'nextPid');
let postData = { pid, uid, tid, content, sourceContent, timestamp, isEnglish, translatedContent };

// Start translation in background (non-blocking)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent pattern: Background translation with promise chaining. Consider: What happens if a user edits the post before the translation completes? Might want to add a check to avoid overwriting user edits.

Comment thread src/posts/create.js
// Update post asynchronously when translation completes
Posts.setPostFields(pid, {
isEnglish: detected,
translatedContent: translated,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice: Silent fail with error logging is appropriate here since the post is already created successfully. Consider emitting a socket event when translation completes so the UI can update without page refresh.

Comment thread src/translate/index.js

// Set up timeout to prevent hanging
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good improvement: 10-second timeout provides better balance than the original 3 seconds, especially for longer posts. This prevents hanging while giving the API sufficient time to respond.

Comment thread src/translate/index.js
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout

const response = await fetch(
`${TRANSLATOR_API}/?content=${encodeURIComponent(postData.content)}`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proper URL encoding with encodeURIComponent prevents injection issues and handles special characters correctly.

Comment thread src/translate/index.js
// Fallback to simple version if fetch fails
return ['is_english', postData];
// Fallback: assume English if translation fails (timeout, network error, etc.)
if (error.name === 'AbortError') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good separation: Distinguishing timeout errors from other failures helps with debugging. Consider: You could add a retry mechanism for transient network failures (non-timeout errors).

@hangyiw hangyiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! ✅

This is a well-implemented performance optimization that:

  • Makes post creation non-blocking by moving translation to background
  • Includes proper timeout handling (10s) to prevent hanging
  • Has graceful error handling with appropriate fallbacks
  • Uses URL encoding to prevent injection issues

The commit increasing timeout to 10 seconds is a good balance. Approved!

@hangyiw hangyiw merged commit 3f2fa61 into main Nov 10, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants