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

Enable add/remove routes after server starts #15

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

yangchenyun
Copy link

  • Use a map instead of slice to hold routes for each configuration
  • Issue a serial number for the route added in order to remove them later
  • Ensure Add/RemoveRoute is thread-safe by using a lock to guard write to the routes, because each write to router has its own serial number, racing condition only exists during registration. The lock should have minimal affect during serving.

Also added two test helpers to simplify and complete existing tests:

func testRouteToBackend(t *testing.T, front net.Listener, back net.Listener, msg string) 
func testNotRouteToBackend(t *testing.T, front net.Listener, back net.Listener, msg string) <-chan bool

- Use a map instead of slice to hold routes for each configuration
- Use a lock to guard read / write to the routes
- Issue a serial number for the route added in order to remove them later
- Add/RemoveRoute is thread-safe
- returns null routeId to indicate a failed registration
@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

@yangchenyun
Copy link
Author

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

- Add the test case for existing backends
- Add the test case for add/remove route after server starts
@bradfitz
Copy link
Collaborator

All the renamings in this PR obscure the actual change you're trying to make.

Can you split your changes into two parts, or refrain from renames for now?

tcpproxy.go Outdated
// not found, this is an no-op.
//
// Both AddRoute and RemoveRoute is go-routine safe.
func (p *Proxy) RemoveRoute(ipPort string, routeId int) (err error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

Done. Thanks.

tcpproxy.go Outdated
//
// This is generally used as either the only rule (for simple TCP
// proxies), or as the final fallback rule for an ipPort.
//
// The ipPort is any valid net.Listen TCP address.
func (p *Proxy) AddRoute(ipPort string, dest Target) {
p.addRoute(ipPort, fixedTarget{dest})
func (p *Proxy) AddRoute(ipPort string, dest Target) (routeId int) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

rebase to fix the case of these originally, rather than fixing it in a later commit in the series.

tcpproxy.go Outdated
acmeTargets []Target // accumulates targets that should be probed for acme.
stopACME bool // if true, AddSNIRoute doesn't add targets to acmeTargets.
}

func NewConfig() (cfg *config) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

needs docs.

and unname result parameter per https://github.com/golang/go/wiki/CodeReviewComments#named-result-parameters

but actually I'd just delete this and let the zero value of Config be useful. You can initialize the routes map as needed.

Copy link
Author

Choose a reason for hiding this comment

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

Removed the constructor.

tcpproxy.go Outdated
@@ -93,11 +94,19 @@ func equals(want string) Matcher {

// config contains the proxying state for one listener.
type config struct {
routes []route
sync.Mutex // protect r/w of routes
Copy link
Collaborator

Choose a reason for hiding this comment

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

comment should be like:

mu sync.Mutex // guards following
x int
y int

and blank line before stuff that is no longer guarded by it.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

@yangchenyun
Copy link
Author

I separated the test refactoring in a #16, and I would do rebase and squash after that is check in.

@georgmu
Copy link

georgmu commented Sep 6, 2018

Adding routes only works for an already registered ipPort. The stuff in done in tcpproxy.Start() - looping over configs and calling p.netListen() is not done again.

For removal of a route, it would be nice to cancel onging connections for this route. This would require a map or list of ongoing connections for a route

patdowney pushed a commit to patdowney/tcpproxy that referenced this pull request Mar 1, 2020
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