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 optional simulated addresses to pipeconn and inmemorylistener #1449

Merged
merged 2 commits into from Dec 7, 2022

Conversation

tobikris
Copy link
Contributor

I have a use case where I need the code using this InmemoryListener to access the simulated address of the connection endpoints.
This PR adds optional addresses to the PipeConns and InmemoryListener.
If the feature is not used, the code behaves like before.

Example:

func main() {
	ln := NewInmemoryListener()

	listenerAddr := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 12345}

	ln.SetLocalAddr(listenerAddr)

	go client(ln)

	lc, err := ln.Accept()
	if err != nil {
		log.Fatalf("unexpected error: %v", err)
	}

	log.Println(lc.LocalAddr(), lc.RemoteAddr())
}

func client(ln *InmemoryListener) {
	clientAddr := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 2), Port: 65432}

	c, err := ln.DialWithLocalAddr(clientAddr)
	if err != nil {
		log.Fatalf("unexpected error: %v", err)
	}

	log.Println(c.LocalAddr(), c.RemoteAddr())
}

Name: "InmemoryListener",
Net: "memory",
if ln.listenerAddr != nil {
return ln.listenerAddr
Copy link
Contributor

Choose a reason for hiding this comment

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

Do SetLocalAddr() and Addr() need to be considered for concurrency?

Copy link
Contributor Author

@tobikris tobikris Dec 2, 2022

Choose a reason for hiding this comment

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

You could argue that it needs to be. However, I thought the only meaningful way to set the address would be to do it before the listener is used in any way. I just did not add it to NewInmemoryListener() to leave this completely optional.

It is probably better to guard against data races, though, and to do not leave this to the caller. I will implement it, thanks!

@erikdubbelboer erikdubbelboer merged commit 951f5a1 into valyala:master Dec 7, 2022
@erikdubbelboer
Copy link
Collaborator

Thanks!

@tobikris tobikris deleted the inmemory-addresses branch December 7, 2022 09:04
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

3 participants