Skip to content

Add sniffer callback - #1

Open
masnec wants to merge 1 commit into
release-branch.v5from
sniffer-callback
Open

Add sniffer callback#1
masnec wants to merge 1 commit into
release-branch.v5from
sniffer-callback

Conversation

@masnec

@masnec masnec commented Jun 25, 2024

Copy link
Copy Markdown
Owner

SnifferCallback allows callers to intercept in the sniffing process and decide which (healthy) nodes are considered for receiving requests.

When the sniffing process finds a healthy node, the callback is invoked with the NodesInfoNode struct passed into it. The callback can inspect that struct and return false when the node should be ignored. No requests are routed to that node by the client.

Example:

func MySnifferCallback(node *elastic.NodeInfoNode) bool {
        // This sniffer simply ignores node with name "node1"
        if node.Name == "node1" {
                return false
        }
        return true
}

...

client, err := elastic.NewClient(
        elastic.SetSnifferCallback(MySnifferCallback))
if err != nil {
        log.Fatal(err)
}

The official clients have a similar feature, e.g. the nodesToHostCallback in the Node.js
client
.

See olivere#388 for further discussion.

SnifferCallback allows callers to intercept in the sniffing process and
decide which (healthy) nodes are considered for receiving requests.

When the sniffing process finds a healthy node, the callback is invoked
with the [`NodesInfoNode`](https://github.com/olivere/elastic/blob/release-branch.v5/nodes_info.go#L136)
struct passed into it. The callback can inspect that struct and return
false when the node should be ignored. No requests are routed to that
node by the client.

Example:

```go
func MySnifferCallback(node *elastic.NodeInfoNode) bool {
        // This sniffer simply ignores node with name "node1"
        if node.Name == "node1" {
                return false
        }
        return true
}

...

client, err := elastic.NewClient(
        elastic.SetSnifferCallback(MySnifferCallback))
if err != nil {
        log.Fatal(err)
}
```

The official clients have a similar feature, e.g. the
[`nodesToHostCallback` in the Node.js
client](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html).

See olivere#388 for further discussion.
@masnec

masnec commented Jul 1, 2024

Copy link
Copy Markdown
Owner Author

test doublecheck for masnec

7 similar comments
@masnec

masnec commented Jul 1, 2024

Copy link
Copy Markdown
Owner Author

test doublecheck for masnec

@masnec

masnec commented Jul 1, 2024

Copy link
Copy Markdown
Owner Author

test doublecheck for masnec

@masnec

masnec commented Jul 1, 2024

Copy link
Copy Markdown
Owner Author

test doublecheck for masnec

@masnec

masnec commented Jul 1, 2024

Copy link
Copy Markdown
Owner Author

test doublecheck for masnec

@masnec

masnec commented Jul 1, 2024

Copy link
Copy Markdown
Owner Author

test doublecheck for masnec

@masnec

masnec commented Jul 1, 2024

Copy link
Copy Markdown
Owner Author

test doublecheck for masnec

@masnec

masnec commented Jul 1, 2024

Copy link
Copy Markdown
Owner Author

test doublecheck for masnec

@masnec-github-app

Copy link
Copy Markdown

Deputy for @masnec has completed the 2nd review of this Pull Request and has the following comments:

Deputy Reviews

  • The client.go file does not have any bugs that I can see.
  • The engineer has added unit tests for the TestClientSnifferCallback function in client_test.go.
  • The engineer has added a function SetSnifferCallback and a corresponding unit test for it.

Other suggestions

Expand for more details
  • Code Organization: Consider breaking down the Client struct in client.go into smaller structs or functions to improve readability and maintainability.
  • Comments: Add more comments to the code to explain the purpose and functionality of each field and function in the Client struct.
  • Error Handling: In client.go, consider adding more error handling to the sniffNode function to handle cases where the NodesInfoNode struct is empty or has missing fields.
  • Type Aliases: In client.go, consider using type aliases for the time.Duration and Logger types to make the code more readable and maintainable.
  • Code Duplication: In client_test.go, consider refactoring the TestClientSniffDisabled and TestClientSniffFailure functions to reduce code duplication.
  • Test Coverage: In client_test.go, consider adding more unit tests to cover different scenarios and edge cases.
  • API Documentation: If any functions related to User Data are added (e.g. getUserData, fetchUserData, deleteUserData), remind the author to add API doc to the team's shared folder.

@masnec

masnec commented Jul 12, 2024

Copy link
Copy Markdown
Owner Author

test doublecheck for masnec

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