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

Pass context to proxy.Run() or proxy.Wait() function #34

Open
snehainguva opened this issue Oct 20, 2021 · 0 comments
Open

Pass context to proxy.Run() or proxy.Wait() function #34

snehainguva opened this issue Oct 20, 2021 · 0 comments

Comments

@snehainguva
Copy link

Using the proxy code and noticed that the current implementation makes it slightly hard to propagate context cancellation through the proxy.Run() function. I've temporarily addressed this issue by using error groups and calling proxy.Close():

	eg.Go(func() error {
		<-ctx.Done()
		proxy.Close()
		return ctx.Err()
	})

	eg.Go(func() error {
		if err := s.proxy.Run(); err != nil {
                         // If context has been cancelled, terminate goroutine and return no error.
			if ctx.Err() != nil {
				return nil
			}
			return err
		}
                return nil
	})

However, this created a raciness issue which eventually led me to creating a SafeProxy type that wrapped the proxy with a mutex:

i.e.

type SafeProxy struct {
	mu    sync.Mutex
	proxy *tcpproxy.Proxy
}

However, if the original Run or Wait functions were to actually take a context (i.e. proxy.Run(ctx) or proxy.Wait(ctx), that would make it easy to gracefully terminate the proxy.

I'm also happy to work on a PR to implement this if there is any interest!

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

No branches or pull requests

1 participant