Skip to content
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

Draft: feat: implement markDead and markAlive in Connection [CCI] #477

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Removed unnecessary `data` argument when invoking `OpenSearchClientError` ([#421](https://github.com/opensearch-project/opensearch-js/pull/421))
- Fixed typos in `ConnectionPool` ([#427](https://github.com/opensearch-project/opensearch-js/pull/427))
- Added the solution for the possible error during yarn installation on Windows OS ([#435](https://github.com/opensearch-project/opensearch-js/issues/435))
- Added `markAlive` and `markDead` methods to `Connection` ([#477](https://github.com/opensearch-project/opensearch-js/pull/477))

### Dependencies

Expand Down
11 changes: 11 additions & 0 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [Delete the document](#delete-the-document)
- [Delete the index](#delete-the-index)
- [Empty all Pool Connections](#empty-all-pool-connections)
- [Change Connection Status](#change-connection-status)

## Initializing a Client

Expand Down Expand Up @@ -265,3 +266,13 @@ pool.empty(() => {
// Do something after emptying the pool
});
```

## Change Connection Status

```javascript
var connection = new Connection({
url: new URL('http://localhost:9200'),
});
connection.markDead();
connection.markAlive();
Copy link
Member

Choose a reason for hiding this comment

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

So is this code allowed?

connection.markDead();
connection.markDead();

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see why not?

```
11 changes: 11 additions & 0 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ class Connection {
this._status = status;
}

markAlive() {
this._status = Connection.statuses.ALIVE;
this.deadCount = 0;
this.resurrectTimeout = 0;
}

markDead() {
this._status = Connection.statuses.DEAD;
this.deadCount++;
}

buildRequestObject(params) {
const url = this.url;
const request = {
Expand Down
25 changes: 25 additions & 0 deletions test/unit/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1116,3 +1116,28 @@ test('Abort with a slow body', (t) => {

setImmediate(() => request.abort());
});

test('Connection markDead', (t) => {
t.plan(2);

const connection = new Connection({
url: new URL('http://localhost:9200'),
});

connection.markDead();
t.equal(connection.status, Connection.statuses.DEAD);
t.equal(connection.deadCount, 1);
});

test('Connection markAlive', (t) => {
t.plan(2);

const connection = new Connection({
url: new URL('http://localhost:9200'),
});

connection.markDead();
connection.markAlive();
t.equal(connection.status, Connection.statuses.ALIVE);
t.equal(connection.deadCount, 0);
});