Skip to content

Reset timeout when we get a message and cleanup connect internal code#12

Merged
masad-frost merged 6 commits intomasterfrom
fm-reset-timeout
Feb 15, 2020
Merged

Reset timeout when we get a message and cleanup connect internal code#12
masad-frost merged 6 commits intomasterfrom
fm-reset-timeout

Conversation

@masad-frost
Copy link
Copy Markdown
Member

@masad-frost masad-frost commented Feb 14, 2020

Why

We started sending the status of the container startup from the backend when we start a connection. After discussing with the infra team (aka mason), it made sense to reset the connection timeout as long as we're receiving something from the backend, which signifies that the connection will eventually work.

While I was implementing the above, I realized how shit the code is for tryConnect which is the internal connect function, it had so much indirection and wasn't the most enjoyable to read, so I refactored it. I guess I could've implemented the timeout reset feature on a separate branch but the code was too smelly 💩

What changed

First thing I did I got rid of deferredReady from the client. deferredReady was used all over the client code because we would resolve/reject the connection promise from multiple places (indirection).

Instead, now we localized all the code for resolving the promise within the tryConnect function itself. We're still dealing with event emitters (the websocket and channels) so it feels like there's some indirection but it's a bit more natural now since everything is colocated in the same function.

I added code comments so you can follow along the tryConnect function. I'll add more info this PR as well.

Test plan

Try all the possible paths of failure, they all should work.

We really need to write unit tests for this as it's easily testable!

Much less indirection.
Does not include timeouts
we changed the way it works a bit in that if we ever get a message on the socket
we will reset the timeout
@masad-frost masad-frost added the boop A PR is ready for review label Feb 14, 2020
Comment thread src/client.ts
* @asMemberOf Channel
* @event
*/
declare function close(c: { closeEvent?: CloseEvent; expected: boolean }): void;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Just typing the close event

Comment thread src/client.ts
@@ -420,61 +386,38 @@ export class Client extends EventEmitter {
};

private onClose = ({ closeEvent, expected }: { closeEvent?: CloseEvent; expected: boolean }) => {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This kind of does the same thing except that it now is never called directly by the client unless we've had a successful connection.

The websocket cleanup is split up into a separate method so that we can call it when the connect function is rejected

Comment thread src/client.ts
Comment on lines +552 to +560
* The user might call `close` before we even connect
* we wanna make sure we reject the promise if that happens
* so we monkey patch our own `close` function ;)
*/
const originalClose = this.close;
this.close = () => {
this.debug({ type: 'breadcrumb', message: 'user close' });
onFailed(new Error('You called `Client.close` before you connected'));
};
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Feels like we can do better in this case, but I'm not sure what.

Comment thread src/client.ts
Comment on lines +448 to +460
/**
* success is only called when we get
* ContainerState.READY command
*/
let onSuccess: () => void;
/**
* Failure can happen due to a number of reasons
* 1- Abrupt socket closure
* 2- Timedout connection request
* 3- ContainerState.SLEEP command
* 4- Use calling `close` before we connect
*/
let onFailed: (err: Error) => void;
Copy link
Copy Markdown
Member Author

@masad-frost masad-frost Feb 14, 2020

Choose a reason for hiding this comment

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

These methods are assigned at the end of the function (inside the promise constructor), they are referenced in the code before they are assigned (see ws.onclose for example), but since all the ways they can be called is async that shouldn't be a problem! LMK if I'm wrong

@replbot replbot removed the boop A PR is ready for review label Feb 14, 2020
@replbot
Copy link
Copy Markdown

replbot commented Feb 14, 2020

unbooping: approved

@masad-frost masad-frost merged commit 72bd840 into master Feb 15, 2020
@masad-frost masad-frost deleted the fm-reset-timeout branch February 15, 2020 01:33
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.

3 participants