Skip to content

feat: establish terminal reconnection foundation #18693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

BrunoQuaresma
Copy link
Collaborator

  • Update ConnectionStatus type: replace 'initializing' with 'connecting'
  • Create useRetry hook with exponential backoff logic
  • Add comprehensive tests for useRetry hook
  • Export useRetry from hooks index

Implements:

  • Initial delay: 1 second
  • Max delay: 30 seconds
  • Backoff multiplier: 2
  • Max retry attempts: 10

Related to: coder/internal#659

- Update ConnectionStatus type: replace 'initializing' with 'connecting'
- Create useRetry hook with exponential backoff logic
- Add comprehensive tests for useRetry hook
- Export useRetry from hooks index

Implements:
- Initial delay: 1 second
- Max delay: 30 seconds
- Backoff multiplier: 2
- Max retry attempts: 10

Co-authored-by: BrunoQuaresma <3165839+BrunoQuaresma@users.noreply.github.com>
@BrunoQuaresma BrunoQuaresma changed the title feat: Phase 1 - Terminal reconnection foundation feat: terminal reconnection foundation Jul 1, 2025
@BrunoQuaresma BrunoQuaresma changed the title feat: terminal reconnection foundation feat: establish terminal reconnection foundation Jul 1, 2025
blink-so bot and others added 5 commits July 1, 2025 14:34
- Fix startRetrying to immediately perform first retry
- Adjust retry scheduling conditions
- Fix delay calculation for exponential backoff

Still debugging test failures
- Fix attemptCount to represent attempts started, not completed
- Fix exponential backoff delay calculation
- Fix retry scheduling conditions for proper max attempts handling
- All 10 useRetry tests now pass
- No regressions in existing test suite

Implements correct behavior:
- attemptCount increments when retry starts
- Exponential backoff: 1s, 2s, 4s, 8s, 16s, 30s (capped)
- Respects maxAttempts limit
- Manual retry cancels automatic retries
- State resets properly on success

Co-authored-by: BrunoQuaresma <3165839+BrunoQuaresma@users.noreply.github.com>
- Add parentheses around arrow function parameter
- Fix indentation

Co-authored-by: BrunoQuaresma <3165839+BrunoQuaresma@users.noreply.github.com>
- Replace setTimeout/setInterval with window.setTimeout/window.setInterval
- Replace clearTimeout/clearInterval with window.clearTimeout/window.clearInterval
- Fixes TypeScript error: Type 'Timeout' is not assignable to type 'number'
- Ensures proper browser environment timer types

Co-authored-by: BrunoQuaresma <3165839+BrunoQuaresma@users.noreply.github.com>
Convert useRetry hook from multiple useState calls to a single useReducer
for cleaner state management. This improves code clarity and makes state
transitions more predictable.

Changes:
- Replace 5 useState calls with single useReducer
- Add RetryState interface and RetryAction union type
- Implement retryReducer function for all state transitions
- Update all state access to use state object
- Replace setState calls with dispatch calls throughout

Co-authored-by: BrunoQuaresma <3165839+BrunoQuaresma@users.noreply.github.com>
@BrunoQuaresma BrunoQuaresma requested review from code-asher and a team July 1, 2025 16:13
@blink-so blink-so bot force-pushed the feature/terminal-reconnection branch from 417b053 to dd7adda Compare July 1, 2025 16:31
Copy link
Member

@code-asher code-asher left a comment

Choose a reason for hiding this comment

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

I found the state hard to follow, is that just me? I think the code could be significantly simpler (simpler to me at least). For example (not tested):

import { useEffect, useRef, useState } from "react";
import { useEffectEvent } from "./hookPolyfills";

export type RetryState = "idle" | "retry" | "countdown";

interface RetryOptions {
	delayMs: number;
	enabled: boolean;
	maxDelayMs: number;
	multiplier: number;
	onRetry: () => Promise<void>;
}

export const useRetry = ({ delayMs, enabled, maxDelayMs, multiplier, onRetry }: RetryOptions) => {
	const [retryState, setRetryState] = useState<RetryState>("idle");
	const [attempt, setAttempt] = useState(0);
	const [countdown, setCountdown] = useState(0);
	const onRetryEvent = useEffectEvent(onRetry);

	useEffect(() => {
		setRetryState(enabled ? "countdown" : "idle");
	}, [enabled]);

	useEffect(() => {
		switch (retryState) {
			case "idle":
				setAttempt(0);
				break;
			case "retry":
				let aborted = false;
				onRetryEvent().then(() => {
					if (!aborted) {
						setRetryState("idle");
					}
				}).catch(() => {
					if (!aborted) {
						setRetryState("countdown");
						setAttempt(attempt + 1); // probably better to set earlier or together with the state
					}
				});
				return () => {
					aborted = true;
				};
			case "countdown":
				const delay = Math.min(delayMs * multiplier ** (attempt - 1), maxDelayMs);
				const timer = setTimeout(() => setRetryState("retry"), delay);
				const start = Date.now();
				const interval = setInterval(() => setCountdown(Math.max(0, delay - (Date.now() - start))), 1000);
				return () => {
					clearTimeout(timer);
					clearInterval(interval);
				};
		}
	}, [attempt, retryState, delayMs, multiplier, maxDelayMs, onRetryEvent]);

	return {
		attempt,
		// countdown will be null if the next retry is not scheduled.
		countdown: retryState === "countdown" ? countdown : null,
	};
};

/**
* Maximum number of retry attempts
*/
maxAttempts: number;
Copy link
Member

Choose a reason for hiding this comment

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

Does there need to be a max number of attempts? Any reason we cannot keep trying?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

IMO it is nice to have to avoid spam the API with unecessary requests. eg. A user leaves the terminal page open for a few hours. Wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

If the user has the terminal page open, that means they want to stay connected, right?

It is possible they could have forgotten to close the window, but at least for me, if I have a terminal up I would expect it to be still connected when I come back.

It would be nice to have parity with SSH at least, I think we have a really long timeout there. I forget exactly what, I think it is at least a day or two though? Something else to consider is that our other ways of connecting, through the IDEs, all will continue to reconnect (I think???), so the web terminal would be the odd one out.

/**
* Initial delay in milliseconds
*/
initialDelay: number;
Copy link
Member

Choose a reason for hiding this comment

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

This is probably just a matter of opinion so feel free to ignore, but I was at first confused with initialDelay because we use it in every loop (multiplied), not just the initial one, so I wonder if we could call this delay (or delayMs) instead.

/**
* Whether a retry is currently in progress (manual or automatic)
*/
isRetrying: boolean;
Copy link
Member

@code-asher code-asher Jul 1, 2025

Choose a reason for hiding this comment

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

Without having seen the planned usage yet, my assumption is that when users pass in callback they will already have a variable for whether the callback is running, so I wonder if we need isRetrying since it gives info that the caller already has?

For example the terminal will already have a connection status state it can use, making this one redundant, since you could setTerminalState("connecting") inside onRetry(), and then use terminalState instead of isRetrying.

Actually, the caller will always need to have their own state anyway, so they know when to call start to begin the retry.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not exactly. It is only retrying if it is calling the function at a second time. Eg. when connecting at the firt time, it should not be considered as a retry, only after that. Makes sense?

Copy link
Member

Choose a reason for hiding this comment

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

I definitely could be missed something, but what I saw in the code is that isRetrying is set the every time onRetry() is called, not the second time. startRetrying() calls performRetry() which calls dispatch("START_RETRY") which sets isRetrying: true and then calls onRetryEvent().

Since isRetrying is always set when onRetry() is called, the caller could simply do setTerminalState("reconnecting") as part of onRetry() to get a reconnecting state.

@BrunoQuaresma
Copy link
Collaborator Author

@code-asher thanks a lot for the review! I think I have a better sense on how to simplify the hook usage and make code more understandable.

blink-so bot and others added 8 commits July 2, 2025 13:36
- Created useWithRetry hook with simple interface (call, retryAt, isLoading)
- Implements exponential backoff with configurable options
- Includes comprehensive tests covering all scenarios
- Added usage examples for different configurations
- Follows existing code patterns and uses constants for configuration

Co-authored-by: BrunoQuaresma <3165839+BrunoQuaresma@users.noreply.github.com>
- Remove example file as requested
- Fix circular dependency issue using useRef pattern
- Ensure proper cleanup and timer management
- Implementation follows existing codebase patterns

Co-authored-by: BrunoQuaresma <3165839+BrunoQuaresma@users.noreply.github.com>
- Remove useRetry.ts and useRetry.test.ts files
- Update hooks index.ts to remove useRetry export
- useWithRetry provides simpler interface for retry functionality

Co-authored-by: BrunoQuaresma <3165839+BrunoQuaresma@users.noreply.github.com>
- Remove options parameter - hook now uses fixed configuration
- Update max attempts to 10 (from 3)
- Update max delay to 10 minutes (from 8 seconds)
- Remove countdown logic - no interval ref needed
- Consolidate state into single RetryState object
- Calculate delay inline where it's used
- Remove separate executeFunction - logic moved into call function
- Only use functions for reusable logic (clearTimer)
- Update tests to match new implementation
- Run formatting and linting checks

Co-authored-by: BrunoQuaresma <3165839+BrunoQuaresma@users.noreply.github.com>
- Do not reset attemptCount when no more attempts are possible
- Add attemptCount to UseWithRetryResult interface
- Return attemptCount in hook result for tracking
- Update tests to verify attemptCount preservation
- Add comprehensive test for attemptCount behavior

This allows consumers to track how many attempts were made,
even after all retry attempts have been exhausted.

Co-authored-by: BrunoQuaresma <3165839+BrunoQuaresma@users.noreply.github.com>
@BrunoQuaresma BrunoQuaresma requested a review from code-asher July 2, 2025 14:38
@BrunoQuaresma
Copy link
Collaborator Author

@code-asher I just came up with a better solution, I guess. Please let me know what you think 🙏

Copy link
Member

@code-asher code-asher left a comment

Choose a reason for hiding this comment

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

I like the idea to externalize the countdown!

The new hook looks great to me, I do have one concern/question about unmounting while fn is in flight.

clearTimeout();

const executeAttempt = async (attempt: number): Promise<void> => {
setState((prev) => ({ ...prev, isLoading: true, attemptCount: attempt }));
Copy link
Member

Choose a reason for hiding this comment

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

Is it be weird for retryAt to still be set since it will be a time in the past now?

Also I wonder if we explicitly do retryAt: prev.retryAt, I feel like the spread makes it too easy to forget to set some state (like in the previous version).

Oh also if the user manually calls this then the retryAt will be wrong for a while (until fn() finishes).

};

await executeAttempt(0);
}, [fn, clearTimeout]);
Copy link
Member

Choose a reason for hiding this comment

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

Should we use that useEffectEvent thing or is the expectation that the user memoizes the function they pass in?

}));

timeoutRef.current = window.setTimeout(() => {
setState((prev) => ({ ...prev, retryAt: undefined }));
Copy link
Member

Choose a reason for hiding this comment

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

Oh I see you unset retryAt here. Should we do it in executeAttempt() to also cover manually calling?

);

setState((prev) => ({
...prev,
Copy link
Member

Choose a reason for hiding this comment

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

Should we explicitly attemptCount: prev.attemptCount to avoid accidentally forgetting props that need to be set?

}, delayMs);
} else {
setState((prev) => ({
...prev,
Copy link
Member

Choose a reason for hiding this comment

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

Same here

retryAt: new Date(Date.now() + delayMs),
}));

timeoutRef.current = window.setTimeout(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Could this code still run after an unmount, if fn was in flight while unmounting? I think it would, right? So we might start a new timeout even though the component is unmounted?

}
}, []);

const call = useCallback(async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Oh wait one more thing, just want to check on the async intent here. Is the idea that call should return a promise that resolves once fn completes successfully? Right now it resolves after the first run of fn (whether it succeeds or fails). Did you have a use in mind for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants