Re: Correct way to run multiple http servers in same applicaion

15,459 views
Skip to first unread message

Nguyên Nguyễn Văn Cao

unread,
Jun 10, 2013, 10:43:27 PM6/10/13
to golan...@googlegroups.com, kyle.a...@gmail.com
Well, how about this http://play.golang.org/p/L6KVUuApzt

Vào 07:34:58 UTC+7 Thứ ba, ngày 11 tháng sáu năm 2013, kyle.a...@gmail.com đã viết:
I want to be able to run two separate http servers concurrently on different port numbers. http.ListenAndServe already puts the script into a never ending state, but if I start two separate go routines, the script will immediately exit. I've also tried only calling one as a go routine but I get an error of 'multiple registrations'

Jesse McNelis

unread,
Jun 10, 2013, 10:43:56 PM6/10/13
to kyle.a...@gmail.com, golang-nuts
On Tue, Jun 11, 2013 at 10:34 AM, <kyle.a...@gmail.com> wrote:
> I want to be able to run two separate http servers concurrently on different
> port numbers. http.ListenAndServe already puts the script into a never
> ending state, but if I start two separate go routines, the script will
> immediately exit. I've also tried only calling one as a go routine but I get
> an error of 'multiple registrations'

An example of your code would help us help you.

The 'multiple registrations' error is the result of adding multiple
handlers for the same url to a servemux.
http://golang.org/src/pkg/net/http/server.go?#L1432

http.ListenAndServe() expects an address to listen on and a
http.Handler to handle the incoming requests.
"Handler is typically nil, in which case the DefaultServeMux is used."
If you're starting two http.ListenAndServe() and they both have a nil
Handler than you're sharing the DefaultServeMux between
them and routes added to one will be available on the other.




--
=====================
http://jessta.id.au

Naitik Shah

unread,
Jun 10, 2013, 10:53:13 PM6/10/13
to Jesse McNelis, kyle.a...@gmail.com, golang-nuts
One in a new goroutine and one in the main goroutine?
> --
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
-Naitik

Alexandre Fiori

unread,
Jun 11, 2013, 1:45:01 AM6/11/13
to golan...@googlegroups.com, Jesse McNelis, kyle.a...@gmail.com
I've been using this:

Rodrigo Kochenburger

unread,
Jun 11, 2013, 4:24:39 AM6/11/13
to golan...@googlegroups.com, kyle.a...@gmail.com
You can also manually instantiate http.Server instances and run each's Server.ListenAndServe() in separate goroutines. It's more work but it gives you more freedom since you can have completely independent executions without any shared state, even completely separated ServeMux.

The downside is that any package that register things on DefaultServeMux won't work (i.e. net/http/pprof).

kmo...@gmail.com

unread,
Jan 23, 2015, 6:45:54 AM1/23/15
to golan...@googlegroups.com
I have the similar needs and google brings me here, after some dig on the net/http/server.go code, 
i  think the better way to do this maybe like this:

package main

import (
"fmt"
"net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello")
}

func world(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "world")
}

func main() {
serverMuxA := http.NewServeMux()
serverMuxA.HandleFunc("/hello", hello)

serverMuxB := http.NewServeMux()
serverMuxB.HandleFunc("/world", world)

go func() {
http.ListenAndServe("localhost:8081", serverMuxA)
}()

http.ListenAndServe("localhost:8082", serverMuxB)
}

hope this will be helpful to you:)




在 2013年6月11日星期二 UTC+8上午8:34:58,Kyle Wolfe写道:

don...@gmail.com

unread,
Feb 25, 2015, 4:35:25 PM2/25/15
to golan...@googlegroups.com, kmo...@gmail.com
This is very helpful to me and exactly what I was looking for, thank you!
Reply all
Reply to author
Forward
0 new messages