Why there is no net.ListenContext?

332 views
Skip to first unread message

Michał Matczuk

unread,
Dec 19, 2023, 5:59:49 AM12/19/23
to golang-nuts
If you take a look at net.Listen implementation

func Listen(network, address string) (Listener, error) {
    var lc ListenConfig
    return lc.Listen(context.Background(), network, address)
}

you will notice the new listen API net.ListenConfig.

This API is context aware, I think it would be handy if we had net.ListenContext the same way we have Dial and DialContext.

Any thoughts on that?

Michał Matczuk

unread,
Dec 19, 2023, 3:28:26 PM12/19/23
to golang-nuts
The context is passed downstream but cancelling it seems to have no effect.
I guess that the reason why it's not exported.
Can anyone shed more light on it?   

Axel Wagner

unread,
Dec 19, 2023, 4:15:13 PM12/19/23
to Michał Matczuk, golang-nuts
You can use `ListenConfig.Listen` to do this:
I believe the reason to do it this way was the realization that there will probably be more ways to set up listening in the future and having lots of different ListenFoo functions in the package would overload the namespace too much.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/8abe1abb-eb3b-4632-b3c0-e430f2f42270n%40googlegroups.com.

Axel Wagner

unread,
Dec 19, 2023, 4:21:27 PM12/19/23
to Michał Matczuk, golang-nuts
Hm, reading again, I don't think I actually understand your question. You clearly are ListenConfig aware.
But what do you mean with "the same way we have Dial and DialContext"? These are methods on Dialer, so ISTM that there is indeed a pretty clear correspondence. Except that Dialer.Dial has been introduced in Go 1.1, before we had context.Context, so we needed a new name for the context-aware API.
So I'm not sure what exactly you are asking. ISTM you are not missing a context-aware version of Listen, so what is it you are asking for?

Michał Matczuk

unread,
Dec 21, 2023, 4:11:27 AM12/21/23
to golang-nuts
Hey.

net.ListenConfig does not document the context usage. The docs just point to Listen. I'd expect that cancelling the context would stop the listener but it's not the case. 

The questions are:

* Should Listener created with net.ListenConfig.Listen be closed - is that a bug, if not why?
* Is the above document anywhere?

Ian Lance Taylor

unread,
Oct 20, 2024, 8:48:15 PMOct 20
to Michał Matczuk, golang-nuts
On Thu, Dec 21, 2023 at 1:11 AM Michał Matczuk <mmat...@gmail.com> wrote:
>
> net.ListenConfig does not document the context usage. The docs just point to Listen. I'd expect that cancelling the context would stop the listener but it's not the case.
>
> The questions are:
>
> * Should Listener created with net.ListenConfig.Listen be closed - is that a bug, if not why?

This is not a bug. While there are exceptions in special cases, in
general a Context is specific to a particular operation, in this case
opening the listening socket. It's not in general applicable to all
listening on a socket. (It's perhaps worth noting that if
ListenConfig had ControlContext as Dialer does, then the Context would
be passed to ListenerConfig.ControlContext.)

> * Is the above document anywhere?

I sent https://go.dev/cl/620777 to document it. Thanks for bringing
it up. Sorry for the very slow response.

Ian
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/48237db5-fe65-4dad-8035-b8c32643219an%40googlegroups.com.

Nico Braun

unread,
Oct 27, 2024, 12:31:29 PMOct 27
to golang-nuts
Hi,  I have been watching this proposal proposal: io/v2: add Context parameter to Reader, etc. · Issue #20280 · golang/go. I think they provide some rationale why context is problematic for these things.  I think something like net.Listen would overlap to some degree.  Therefore, I think its interesting to see that they even have traces of context in the net package, like on the ListenerConfig. 

Is there some overlap, or am I mixing things up here? 

Reply all
Reply to author
Forward
0 new messages