-
Notifications
You must be signed in to change notification settings - Fork 6
v2 Proxy Implementation #3
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
Merged
francisf
merged 81 commits into
browserstack:review-branch
from
Archish27:context_redesign
Aug 27, 2021
Merged
Changes from all commits
Commits
Show all changes
81 commits
Select commit
Hold shift + click to select a range
97bade0
initial code for proxy
Ankit098 d0f54d9
modify proxy data object
Ankit098 7b71301
fixing retry logic for server ws connection
Ankit098 8f0402a
add check for options on proxy start
Ankit098 1977fbb
support client reconnection
Ankit098 9d9b5f0
add heartbeat ping
Ankit098 dc894e4
add(wip): sender class for sending to upstream
yashLadha 7de671d
add(wip): Adding receiver mode
yashLadha 769858b
add: cluster module and minor refactoring
yashLadha 7e88725
chore: README addition to the code
yashLadha d67ff57
chore: refactored incoming connection handling for sender
yashLadha aaf8a2a
fix: wrong enqueue in receiver mode from client
yashLadha bfe0300
fix: modify request url with target in sender mode
yashLadha 833f3f8
fix: connect to same url in receiver mode
yashLadha 6394513
chore: ignore temp directory for commit
yashLadha e2ff37f
fix: cluster fork method in restarting workers
yashLadha 7418438
chore: remove redundant information from child logger
yashLadha 5157336
chore: added LLD file and minor changes to upstream
yashLadha d020553
fix: replace watchFile with watch
yashLadha 4253a9e
chore: add diagram for receiver and sender mode
yashLadha 5eed2c9
add: init doc v2 design
yashLadha 3518ff1
chore: wip commit for defining the new design and implementation
yashLadha 9c8d23d
:fire: Proxy v2 implementation init
Archish27 dba72bc
:art: Renamed method name and fixed setting context
Archish27 b96b2ae
:art: proxy v2 implementation and major bug fixes
Archish27 bf7677e
:white_check_mark: tests fixes for queue
Archish27 2691682
:art: EOL fixes
Archish27 1076f44
:art: updated package.json
Archish27 362b180
:white_check_mark: tests added for util
Archish27 a773631
:white_check_mark: Added tests for context
097b5ec
:white_check_mark: Added test for proxy
97c959a
:rotating_light: added eslint & fixes
e4e7882
:art: Added process handler and alerting
0cc226c
:art: Updated design & README.md
Archish27 7a0d12f
test: add tests for incoming ws
Ankit098 6a9ca87
test: add tests for outgoing ws
Ankit098 662ce50
Merge branch 'context_redesign' into context_redesign
Ankit098 92a7106
chore: eslint fixes for test files
Ankit098 1864a71
Merge pull request #1 from Ankit098/context_redesign
Archish27 0bc9c15
:bug: fix for sending alert
Archish27 891351b
:art: added settings.json for vscode
Archish27 68c536c
:art: eslint fixes
Archish27 2efa319
:art: eol fixes
Archish27 b4800e1
:white_check_mark: removed redundant close statement and test fixes
Archish27 cf87dbe
:art: Formatted code and added prettier
Archish27 5f9b6e6
:art: Added log rotate
Archish27 22d6833
:art: Updated logging to info
Archish27 f1c4962
:white_check_mark: Added tests
Archish27 81d624b
:art: Formatted dir structure & added lint fixes
Archish27 878a93c
:art: added typeSanity checks
Archish27 b5c3253
:art: Refactorred cluster.js and added worker kill timer
Archish27 6399e76
:art: Added worker timer and watching restart.txt in master
Archish27 523645d
:art: CLogging format updated
Archish27 b21bb07
:art: Added metric and test fixes
Archish27 907e6ae
:bug: fix for spawning workers
Archish27 cc551cc
:rotating_light: eol fixes
Archish27 860f56d
:art: ABinding http server in proxy and test fixes
Archish27 8d34b0e
:sparkle: Added workflow action
Archish27 568716e
:art: Added logic for Pproxy restart and chore fixes
Archish27 f590ebb
:memo: Updated README.md
Archish27 5b2ffff
:bug: fix for workflow actions
Archish27 7775532
:fire: removed redundant code
Archish27 affeded
:bug: fix for workflow actions
Archish27 6ff6e1a
:hammer: updated package.json
Archish27 acc971f
:bug: fix for workflow actions
Archish27 5c2440b
:art: Removed tabs
Archish27 69eceae
:white_check_mark: Added tests
Archish27 6ea81fa
:rotating_light: eslint fixes
Archish27 ef32c2c
:art: Update .prettierignore
Archish27 ae5ae35
:fire: Added log path from env variable
Archish27 260fea9
Merge branch 'context_redesign' of https://github.com/Archish27/ws-re…
Archish27 b5d66aa
:bug: fix for binding hostname and moving to node lts version
Archish27 dfcabeb
:art: Update docs/design.md
Archish27 39b739d
:art: Update docs/design.md
Archish27 e53e05c
:art: Update docs/design.md
Archish27 1612aa1
:bug: fix for restart path
Archish27 0b74130
Merge branch 'context_redesign' of https://github.com/Archish27/ws-re…
Archish27 32ff671
:bug: fix for alert and instrumentation
Archish27 fd427d5
:fire: removed messages meteric
Archish27 c29a20b
:white_check_mark: test fixes
Archish27 439ef8d
:fire: Updated changes for alert manager and instrumentation
Archish27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| root = true | ||
|
|
||
| [*] | ||
| indent_style = space | ||
| indent_size = 2 | ||
| charset = utf-8 | ||
| trim_trailing_whitespace = true | ||
| insert_final_newline = true | ||
|
|
||
| [*.md] | ||
| trim_trailing_whitespace = false | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| module.exports = { | ||
| env: { | ||
| node: true, | ||
| commonjs: true, | ||
| es2021: true, | ||
| }, | ||
| extends: 'eslint:recommended', | ||
| parserOptions: { | ||
| ecmaVersion: 12, | ||
| }, | ||
| rules: { | ||
| indent: ['error', 2], | ||
| 'linebreak-style': ['error', 'unix'], | ||
| quotes: ['error', 'single'], | ||
| semi: ['error', 'always'], | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| ## Description | ||
|
|
||
| <!--- Describe your changes in detail --> | ||
|
|
||
| ## Motivation and Context | ||
|
|
||
| <!--- Why is this change required? What problem does it solve? --> | ||
| <!--- If it fixes an open issue, please link to the issue here. --> | ||
|
|
||
| ## How Has This Been Tested? | ||
|
|
||
| <!--- Please describe in detail how you tested your changes. --> | ||
| <!--- Include details of your testing environment, and the tests you ran to --> | ||
| <!--- see how your change affects other areas of the code, etc. --> | ||
|
|
||
| ## Screenshots (if appropriate): | ||
|
|
||
| ## Types of changes | ||
|
|
||
| <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> | ||
|
|
||
| - [ ] Bug fix (non-breaking change which fixes an issue) | ||
| - [ ] New feature (non-breaking change which adds functionality) | ||
| - [ ] Breaking change (fix or feature that would cause existing functionality to change) | ||
|
|
||
| ## Checklist: | ||
|
|
||
| <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> | ||
| <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> | ||
|
|
||
| - [ ] My code follows the code style of this project. | ||
| - [ ] I have added tests to cover my changes. | ||
| - [ ] All new and existing tests passed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ main ] | ||
| pull_request: | ||
| branches: [ main ] | ||
| workflow_dispatch: | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| name: Lint and Test | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| name: Check out repository | ||
| - uses: actions/setup-node@v2 | ||
| name: Setup Node.js | ||
| with: | ||
| node-version: '14.17.5' | ||
| - run: | | ||
| cp lib/config/config.json.sample lib/config/config.json | ||
| name: Setting up configuration | ||
| - run: | | ||
| npm ci | ||
| name: Installing Dependencies | ||
| - run: | | ||
| npm run eslint | ||
| name: Running ESLint checks on source | ||
| - run: | | ||
| npm run eslint:test | ||
| name: Running ESLint checks on tests | ||
| - run: | | ||
| npm run test | ||
| name: Running tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| node_modules | ||
| tmp/ | ||
| config.json | ||
| .nyc_output | ||
| logs/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| v14.17.5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| logs/ | ||
| .github/ | ||
| .nyc_output/ | ||
| .vscode/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "singleQuote": true, | ||
| "tabWidth": 2, | ||
| "endOfLine": "lf", | ||
| "semi": true, | ||
| "trailingComma": "es5", | ||
| "useTabs": false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| module.exports = { | ||
| filter(data) { | ||
| return data.req; | ||
| }, | ||
| output: { | ||
| path: './proxy.log', | ||
| options: { | ||
| path: './logs', | ||
| interval: '1d', | ||
| }, | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "type": "pwa-node", | ||
| "request": "launch", | ||
| "name": "ws-reconnect-proxy", | ||
| "skipFiles": ["<node_internals>/**"], | ||
| "program": "${workspaceFolder}/cluster.js", | ||
| "outputCapture": "std", | ||
| "console": "internalConsole", | ||
| "env": { | ||
| "NODE_ENV": "dev" | ||
| } | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,72 @@ | ||
| # ws-reconnect-proxy | ||
| Proxy Server that is between a ws server and a ws client. In case of either server / client disconnects graceful or otherwise - initiates/ supports reconnection . | ||
|
|
||
| Proxy Server that is between a ws server and a ws client. In case of either server / client disconnects graceful or otherwise - initiates/ supports reconnection. | ||
|
|
||
| Read more about [📝 design](docs/design.md) | ||
|
|
||
| ### README Contents: | ||
|
|
||
| - [How to contribute](#how-to-contribute) | ||
| - [Development Internals](#development-internals) | ||
|
|
||
| ### ✨ How to contribute | ||
|
|
||
| We are very happy to receive and merge your contributions into this repository! | ||
|
|
||
| To contribute via pull request, follow these steps: | ||
|
|
||
| 1. Create an issue describing the feature you want to work on (or | ||
| have a look at the [issues](https://github.com/browserstack/ws-reconnect-proxy/issues)) | ||
| 2. Write your code, tests and format them with `npm run format` | ||
| 3. Create a pull request describing your changes | ||
|
|
||
| Your pull request will be reviewed by a maintainer, who will get | ||
| back to you about any necessary changes or questions. | ||
|
|
||
| ## ⚡️ Development Internals | ||
|
|
||
| ### 🔨 Installing Dependencies | ||
|
|
||
| To install dependencies | ||
|
|
||
| ```bash | ||
| npm install | ||
| ``` | ||
|
|
||
| ### ✅ Running the Tests | ||
|
|
||
| In order to run the tests, make sure that you have installed dependencies: | ||
|
|
||
| ```bash | ||
| npm run test | ||
| ``` | ||
|
|
||
| ### 🎨 Formatting | ||
|
|
||
| To reformat files execute | ||
|
|
||
| ```bash | ||
| npm run format | ||
| ``` | ||
|
|
||
| ### 🚀 Run proxy | ||
|
|
||
| 🔧 Before, executing proxy create the `config.json` by running the following command: | ||
|
|
||
| ```bash | ||
| cp lib/config/config.json.sample lib/config/config.json | ||
| ``` | ||
|
|
||
| Additionally, you can configuration your proxy based on your needs. Refer here - [config.json.sample](lib/config/config.json.sample) | ||
|
|
||
| Then execute proxy by running the following command: | ||
|
|
||
| ```bash | ||
| npm run start | ||
| ``` | ||
|
|
||
| _NOTE: By default it runs in `dev` environment you can configure your env by the following command:_ | ||
|
|
||
| ```bash | ||
| NODE_ENV=<env> node cluster.js | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| 'use strict'; | ||
|
|
||
| const cluster = require('cluster'); | ||
| const { config } = require('./lib/config/constants.js'); | ||
| const { watch } = require('fs'); | ||
| const logger = require('./lib/util/loggerFactory.js'); | ||
| const Proxy = require('./lib/core/Proxy.js'); | ||
|
|
||
| const WORKER_CNT = config.workerVal; | ||
| const activeWorkers = []; | ||
|
|
||
| const path = require('path'); | ||
|
|
||
| const RESTART_FILE = path.join(__dirname, 'tmp', 'restart.txt'); | ||
|
|
||
| const forceKill = (worker) => { | ||
| if (!worker.isDead()) { | ||
| logger.info(`Worker ${worker.process.pid} is ${worker.state}, Killing it`); | ||
| worker.kill('SIGUSR2'); | ||
| } | ||
| }; | ||
|
|
||
| const disconnectOldWorkers = () => { | ||
| const len = activeWorkers.length; | ||
| for (let i = 0; i < len; i++) { | ||
| const oldWorker = activeWorkers.shift(); | ||
| oldWorker.disconnect(); | ||
| setTimeout(() => forceKill(oldWorker), config.workerKillTimer); | ||
| } | ||
| }; | ||
|
|
||
| const spawnNewWorkers = () => { | ||
| if (cluster.isMaster) { | ||
| for (let i = 0; i < WORKER_CNT; ++i) { | ||
| const worker = cluster.fork(); | ||
| worker.on('error', (err) => { | ||
| logger.error(`Received error event on ${worker.id} : ${err}`); | ||
| }); | ||
| logger.info(`Created worker with id ${worker.id}`); | ||
| activeWorkers.push(worker); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| if (cluster.isMaster) { | ||
| cluster.on('online', function (worker) { | ||
| logger.info(`Worker ${worker.process.pid} is online`); | ||
| }); | ||
|
|
||
| cluster.on('exit', (worker, code, signal) => { | ||
| logger.info( | ||
| `worker ${worker.process.pid} died with signal ${signal} code ${code}` | ||
| ); | ||
| if (activeWorkers.length == 0) spawnNewWorkers(); | ||
| }); | ||
|
|
||
| spawnNewWorkers(); | ||
|
|
||
| let currTime = Date.now(); | ||
| watch(RESTART_FILE, () => { | ||
| if (Date.now() > currTime) { | ||
| currTime = Date.now(); | ||
| disconnectOldWorkers(); | ||
| spawnNewWorkers(); | ||
| } | ||
| }); | ||
| } else { | ||
| new Proxy(); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.