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

Add tests for listener Close + invoke only pool release #250

Merged
merged 1 commit into from Mar 2, 2024

Conversation

brandur
Copy link
Contributor

@brandur brandur commented Mar 2, 2024

Here, follow up #246 by adding a few more tests that verify a listener's
state after Close has been invoked, including if it returned an error,
which we're able to simulate by overriding pgx's DialFunc and
returning a stand-in stub for an underlying net.Conn.

Also, remove the explicit Close call on an underlying connection in
favor of just invoking the pool's Release function. In case of an
error condition, Release will detect that and do the right thing, and
pgx is better tested/vetted to make sure that right thing happens.

@brandur brandur force-pushed the brandur-pgxv5-listener-close-test branch 2 times, most recently from 2219b73 to b0eb5d3 Compare March 2, 2024 20:39
return c.closeFunc()
}

func testPoolConfig() *pgxpool.Config {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe riverinternaltest should be its own package so we can reuse this stuff, but ugh, skipped any further refactoring there for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

not more packages 😩

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha, ack.

Copy link
Contributor

@bgentry bgentry left a comment

Choose a reason for hiding this comment

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

LGTM aside from not closing the conn, lmk if you disagree on that

Comment on lines 478 to 481
err := l.conn.Conn().Close(ctx)

// Regardless of the error state returned above, always release and unset
// the listener's local connection.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not actually sure this is a good idea and I don't think we want to return the conn to the pool. If there are LISTEN subscriptions active which weren't properly UNLISTEN'd, the conn may still receive additional notifications that could interfere with other operations on it.

For pgbouncer compatibility reasons, we will probably also end up going a direction where the listener conn is created from a standalone config or at least a separate pool from the main river db pool.

Or maybe you can elaborate more on why you think this is a necessary change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, fair point on that one, I forgot about the potential taint from not correctly using Unlisten. I put the Close back in and added a comment to remind us why it's written this way. One of the test cases had to go because pgx's implementation is very specific, and AfterRelease doesn't get called if a connection was released that was already closed.

Want to take another look?

return c.closeFunc()
}

func testPoolConfig() *pgxpool.Config {
Copy link
Contributor

Choose a reason for hiding this comment

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

not more packages 😩

Here, follow up #246 by adding a few more tests that verify a listener's
state after `Close` has been invoked, including if it returned an error,
which we're able to simulate by overriding pgx's `DialFunc` and
returning a stand-in stub for an underlying `net.Conn`.

Also, remove the explicit `Close` call on an underlying connection in
favor of just invoking the pool's `Release` function. In case of an
error condition, `Release` will detect that and do the right thing, and
pgx is better tested/vetted to make sure that right thing happens.
@brandur brandur force-pushed the brandur-pgxv5-listener-close-test branch from 84fa0e8 to fb1e2b0 Compare March 2, 2024 22:38
@brandur
Copy link
Contributor Author

brandur commented Mar 2, 2024

ty.

@brandur brandur merged commit c8b0837 into master Mar 2, 2024
10 checks passed
@brandur brandur deleted the brandur-pgxv5-listener-close-test branch March 2, 2024 23:10
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.

None yet

2 participants