Skip to content

IND-2885 Linting #641

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 5 commits into
base: main
Choose a base branch
from
Open

IND-2885 Linting #641

wants to merge 5 commits into from

Conversation

KaushikiAnand
Copy link
Contributor

  • I have added linting to go-tests.yml.
  • The linting was done to ensure that the codebase is consistent and maintainable and helps in improving the code quality and reducing errors.

@KaushikiAnand KaushikiAnand requested review from a team as code owners April 8, 2025 06:27
@KaushikiAnand KaushikiAnand requested a review from jmurret April 8, 2025 06:27
net_transport.go Outdated
@@ -313,7 +314,9 @@ func (n *NetworkTransport) CloseStreams() {
// entry.
for k, e := range n.connPool {
for _, conn := range e {
conn.Release()
if err := conn.Release(); err != nil {
log.Print(err)

Choose a reason for hiding this comment

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

Suggestion: A more descriptive error message here would be helpful when tracing the error.

net_transport.go Outdated
@@ -441,7 +442,9 @@ func (n *NetworkTransport) returnConn(conn *netConn) {
if !n.IsShutdown() && len(conns) < n.maxPool {
n.connPool[key] = append(conns, conn)
} else {
conn.Release()
if err := conn.Release(); err != nil {
log.Print(err)

Choose a reason for hiding this comment

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

Same as above.

net_transport.go Outdated
var errors error
defer func() {
if err := conn.Release(); err != nil && errors == nil {
errors = err

Choose a reason for hiding this comment

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

Question: Does setting it to errors server any purpose? I don't see it being used anywhere below.

@@ -635,7 +635,9 @@ func TestNetworkTransport_InstallSnapshot(t *testing.T) {

// Try to read the bytes
buf := make([]byte, 10)
rpc.Reader.Read(buf)
if _, err := rpc.Reader.Read(buf); err != nil {
t.Errorf("unable to read bytes: %v", err)

Choose a reason for hiding this comment

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

Suggestion: Can we wrap the error instead of formatting it into the string i.e %v -> %w?

Choose a reason for hiding this comment

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

There are a bunch of similar instances in raft.go as well.

@@ -1859,7 +1859,7 @@ func TestRaft_Barrier(t *testing.T) {
// Ensure all the logs are the same
c.EnsureSame(t)
if len(getMockFSM(c.fsms[0]).logs) != 100 {
t.Fatalf(fmt.Sprintf("Bad log length: %d", len(getMockFSM(c.fsms[0]).logs)))
t.Fatalf("%s", fmt.Sprintf("Bad log length: %d", len(getMockFSM(c.fsms[0]).logs)))

Choose a reason for hiding this comment

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

Question: Why not use t.Fatal(...) instead?

Copy link
Member

@tgross tgross left a comment

Choose a reason for hiding this comment

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

We need to be very careful about introducing behavior changes and swallowing important errors in production code for the sake of silencing lints. If we can't justify each change aside from lints I'm a -1 on this PR.

net_transport.go Outdated
Comment on lines 811 to 813
if err := conn.Release(); err != nil {
return err
}
Copy link
Member

Choose a reason for hiding this comment

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

What's a circumstance where we're going to get an error here and want that to be the return value instead of the original error? (Similar for all these changes.)

Comment on lines +899 to +901
if err := n.conn.conn.SetWriteDeadline(time.Now().Add(timeout)); err != nil {
return nil, err
}
Copy link
Member

Choose a reason for hiding this comment

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

How does this change behavior in the caller? We're changing the error handling in many places in this PR. We're doing this for the sake of linting without much in the way of consideration for how it changes the expected behavior. "It silences linters" isn't a good enough reason to make this change without that consideration.

(This comment applies to a lot of these.)

raft.go Outdated
Comment on lines 1154 to 1157
if err := sink.Cancel(); err != nil {
return fmt.Errorf("failed to cancel snapshot: %v", err)
}
return fmt.Errorf("failed to write snapshot: %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

Similar to the case I described in net_transport.go, we're now swallowing the original error which is much more important for the caller to understand what's going on. This also applies anywhere else in this PR where we're doing this (there's a half dozen I can see).

@mukeshjc
Copy link
Contributor

mukeshjc commented Apr 9, 2025

@tgross
Thank you for the review. We have discussed this internally and plan to take below approach for moving forward:

Thoughts?

@tgross
Copy link
Member

tgross commented Apr 9, 2025

  • For all instances of teardown functions (eg: conn.Resolve(), sink.Cancel() etc.) that are flagged by the linter, we will exclude them to suppress the findings since they aren't needed for teardown type of functions.

That's a reasonable approach. Another option would be to figure out which teardown functions we do need to care about and get the added error context wrapped in the original error we return. But that's certainly more work and needs more attention.

Why? It seems totally safe to add the extra checks for unit tests, right?

@mukeshjc
Copy link
Contributor

Why? It seems totally safe to add the extra checks for unit tests, right?

The idea is fixing these linting checks in unit test files doesn't improve the actual library. I'm happy to include them too, no strong opinions here. We will keep them.

@tgross
Copy link
Member

tgross commented Apr 10, 2025

The idea is fixing these linting checks in unit test files doesn't improve the actual library. I'm happy to include them too, no strong opinions here. We will keep them.

This is a little all-or-nothing. Either the lint is useful, in which case you should apply it where it's safe to apply or it's not useful at all. I think it is useful, so applying it to test code in particular could be handy.

Your change in e1a5e6a leaves a number of cases in the production code, however.

@mukeshjc
Copy link
Contributor

mukeshjc commented Apr 11, 2025

Your change in e1a5e6a leaves a number of cases in the production code, however.

@tgross

We plan to re-visit the need for error wrapping of teardown functions after syncing with the group in #tech-raft slack channel. Based on the feedback we will keep/remove the suppressed findings.

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.

4 participants