On Monday, March 29, 2021 at 9:05:50 PM UTC+2 rog wrote:

> I often call net.Listen directly before calling Serve in a goroutine. That 
> way you can connect to the server's socket immediately even though the 
> server might take a while to get around to serving the request.
>
 
It seems this would work as long as the server is not using HTTPS and needs 
to perform a handshake first?

Look at how net/http/httptest does it.
>

I wasn't sure if you're referring to goServe 
https://github.com/golang/go/blob/master/src/net/http/httptest/server.go#L304 
which is called from Start()? This seems to return only after the server is 
stopped.
I've found the idea interesting though. Using a WaitGroup you could at 
least ensure that the gofunc containing the call to Serve() has at least be 
started. That still doesn't seem to be sufficient though:

ln, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Fatal(err)
}

var wg sync.WaitGroup
wg.Add(1)

go func() {
server := &http.Server{Addr: ":" + port, Handler: handler}
wg.Done()
log.Fatal(server.Serve(ln))
}()

wg.Wait()

Cheers,
Andi
 

>
> On Sat, 27 Mar 2021, 14:13 cpu...@gmail.com, <cpu...@gmail.com> wrote:
>
>> The typical Go tutorials pattern for starting a server is something like 
>>
>>     log.Fatal(http.ListenAndServe(":8080"))
>>
>> But what if the application needs to do other things after the server is 
>> started? It seems there is virtually no method to wait for the server 
>> actually start listening to requests?
>>
>> I'm probably missing something and would appreciate a hint.
>>
>> Thanks,
>> Andi
>>
>> -- 
>> 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/a40222e3-e4b3-4996-8232-045fcff43b77n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/a40222e3-e4b3-4996-8232-045fcff43b77n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/a52551ab-b5bb-4696-b683-6d9c03093c88n%40googlegroups.com.

Reply via email to