Skip to content

Add client user connection limit#1137

Merged
JelteF merged 55 commits into
pgbouncer:masterfrom
AndrewJackson2020:user_client_limit
Oct 28, 2024
Merged

Add client user connection limit#1137
JelteF merged 55 commits into
pgbouncer:masterfrom
AndrewJackson2020:user_client_limit

Conversation

@AndrewJackson2020
Copy link
Copy Markdown
Contributor

@AndrewJackson2020 AndrewJackson2020 commented Aug 9, 2024

This PR adds a user level client connection limit. I believe that currently user connections can only be limited by how many people are given connections to the database backends but there is no way to limit the number of connections to pgbouncer itself. Combined with the global connection limit this means that one user could use up all connections on the pgbouncer instance. This PR proposes a fix to this problem.

Related to #1136 (because it partially fixes it)

Copy link
Copy Markdown
Member

@JelteF JelteF left a comment

Choose a reason for hiding this comment

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

This seems like a good solution to the described problem. A similar setting would probably be useful per database, but that doesn't need to be in this PR.

Some things that should be changed about the PR:

  1. Github Actions should be green (fix C warning and fix formatting)
  2. A test should be added (test/test_limits.py seems like a good place to add one)
  3. The new option should be documented in doc/config.md

Comment thread include/bouncer.h Outdated
Comment thread include/bouncer.h Outdated
Comment thread include/bouncer.h Outdated
Comment thread src/client.c Outdated
@AndrewJackson2020
Copy link
Copy Markdown
Contributor Author

AndrewJackson2020 commented Aug 9, 2024

This seems like a good solution to the described problem. A similar setting would probably be useful per database, but that doesn't need to be in this PR.

Some things that should be changed about the PR:iss

1. Github Actions should be green (fix C warning and fix formatting)

2. A test should be added (`test/test_limits.py` seems like a good place to add one)

3. The new option should be documented in `doc/config.md`

@JelteF I believe the 3 points above have been addressed, can I get another review?

Also I opened #1138 to address this issue from the database side.

One question: should the values of max clients and current clients connected be added to the output of show users?

@eulerto
Copy link
Copy Markdown
Member

eulerto commented Aug 9, 2024

I didn't review this PR yet but I noticed that you did lots of changes in the documentation that is not related to this feature. If you want to propose a whitespace cleanup, create a separate PR. We generally use /* comment */ instead of // comment.

BTW, I'm fine with this feature. It is equivalent to CREATE ROLE ... CONNECTION LIMIT.

@AndrewJackson2020
Copy link
Copy Markdown
Contributor Author

I didn't review this PR yet but I noticed that you did lots of changes in the documentation that is not related to this feature. If you want to propose a whitespace cleanup, create a separate PR. We generally use /* comment */ instead of // comment.

BTW, I'm fine with this feature. It is equivalent to CREATE ROLE ... CONNECTION LIMIT.

Apologies about that, my editor eliminates trailing whitespace default. Will revert momentarily.

@AndrewJackson2020
Copy link
Copy Markdown
Contributor Author

I didn't review this PR yet but I noticed that you did lots of changes in the documentation that is not related to this feature. If you want to propose a whitespace cleanup, create a separate PR. We generally use /* comment */ instead of // comment.

BTW, I'm fine with this feature. It is equivalent to CREATE ROLE ... CONNECTION LIMIT.

I reverted the whitespace removal and changed the comment style to conform with the rest of the codebase.

Copy link
Copy Markdown
Member

@JelteF JelteF left a comment

Choose a reason for hiding this comment

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

Thanks! That looks a lot better already. I left a review just now on your other #1138 PR. Most of the feedback there should also be applied here. Other than that it looks pretty good.

I still have want to do some manual tests of both PRs though

@AndrewJackson2020
Copy link
Copy Markdown
Contributor Author

@JelteF Made all of the documentation changes and added a test of the stats command in the unit tests. Also split the unit test out into 2 separate ones: one that is meant to deny a connection and one that is meant to accept a connection, I feel this is a bit cleaner.

A few additional thoughts:

  1. Should this PR and Add db client connection limit #1138 be merged because they are so similar and have similar feedback?
  2. Should the name of the variables be changed from max_user_client_connections/max_user_connections to max_user_client_connections/max_user_server_connections?
  3. Should the tests of the stats command be moved to test/test_admin.py or is it fine as is?

Comment thread src/objects.c
@JelteF
Copy link
Copy Markdown
Member

JelteF commented Aug 18, 2024

1. Should this PR and [Add db client connection limit #1138](https://github.com/pgbouncer/pgbouncer/pull/1138) be merged because they are so similar and have similar feedback?

Let's keep them separate

2. Should the name of the variables be changed from max_user_client_connections/max_user_connections to max_user_client_connections/max_user_server_connections?

Let's keep them as is. I don't think the better naming (because I agree it is a bit confusing now) is worth breaking backwards compatibility of the config setting names.

3. Should the tests of the stats command  be moved to test/test_admin.py or is it fine as is?

It's fine as is imho.

Copy link
Copy Markdown
Member

@JelteF JelteF left a comment

Choose a reason for hiding this comment

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

Sorry for the delay in reviewing. I think this is almost good now, some final suggested changes.

Comment thread doc/config.md Outdated
Comment thread src/admin.c Outdated
Comment thread src/client.c
@AndrewJackson2020
Copy link
Copy Markdown
Contributor Author

AndrewJackson2020 commented Sep 19, 2024

@JelteF Pushed a new commit that fixes a bunch of the issues the issues that you mentioned.

Found that the auth pass through issue that you mentioned in #1138 applies to this PR as well. Implemented a somewhat hacky fix to this. Would appreciate your feedback on this or if you have any alternative ideas.

Also expanded unit test coverage for stats users and auth pass through users. One area that isnt covered is PAM but looking through the unit tests I dont think there is any test coverage of PAM currently so I think adding that here might be out of scope for this PR though it would be very beneficial.

Also I believe that the macOS failures are flaky tests but I'm not sure. I have no local dev environment for macOS so its kind of hard for me to figure out what is happening there.

@AndrewJackson2020
Copy link
Copy Markdown
Contributor Author

@JelteF Fixed the macOS tests by scaling back the run_with_config usage as described in #1138.

Let me know if this solution works for you or if it needs to be reworked more.

@AndrewJackson2020
Copy link
Copy Markdown
Contributor Author

@JelteF Anything else I can do here to help this PR get merged?

@JelteF
Copy link
Copy Markdown
Member

JelteF commented Oct 4, 2024

No, it's blocked on me finding some time to review PgBouncer PRs again. Busy busy.

Comment thread src/client.c Outdated
Comment thread src/client.c
if (!client->login_user_credentials->global_user)
return true;

if (!client->user_connection_counted) {
Copy link
Copy Markdown
Member

@JelteF JelteF Oct 7, 2024

Choose a reason for hiding this comment

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

I agree this is not an elegant solution. But given the amount of counting bugs found in earlier versions, I prefer this way because this one is obviously correct and will stay correct in the future. I added a check for user_connection_counted in the decrement path too.

@AndrewJackson2020
Copy link
Copy Markdown
Contributor Author

@JelteF anything else you want done with this PR before merging?

@JelteF
Copy link
Copy Markdown
Member

JelteF commented Oct 28, 2024

I don't remember why I didn't press merge yet, maybe because CI was failing for unrelated reasons.

@JelteF JelteF merged commit 585a630 into pgbouncer:master Oct 28, 2024
@AndrewJackson2020 AndrewJackson2020 deleted the user_client_limit branch October 28, 2024 22:05
JelteF pushed a commit that referenced this pull request Nov 2, 2024
Related to #1137. This PR allows the user to configure client connection
limits at the database level. This prevents issues where SQL clients all
connecting to a single database consume the entire connection limit
defined in max_client_conn which effectively locks all other databases
from being accessed.
rajaryanece pushed a commit to rajaryanece/pgbouncer that referenced this pull request Jan 16, 2025
This PR adds a user level client connection limit. I believe that
currently user connections can only be limited by how many people are
given connections to the database backends but there is no way to limit
the number of connections to pgbouncer itself. Combined with the global
connection limit this means that one user could use up all connections
on the pgbouncer instance. This PR proposes a fix to this problem.

Related to pgbouncer#1136 (because it partially fixes it)
rajaryanece pushed a commit to rajaryanece/pgbouncer that referenced this pull request Jan 16, 2025
Related to pgbouncer#1137. This PR allows the user to configure client connection
limits at the database level. This prevents issues where SQL clients all
connecting to a single database consume the entire connection limit
defined in max_client_conn which effectively locks all other databases
from being accessed.
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.

3 participants