What is an http request multiplexer?

huangapple go评论83阅读模式
英文:

What is an http request multiplexer?

问题

我一直在学习Golang,我注意到很多人使用http.NewServeMux()函数来创建服务器,但我不太理解它的作用。

我读到这样的解释:

在Go中,ServeMux是一个HTTP请求多路复用器。它将每个传入请求的URL与注册的模式列表进行匹配,并调用与URL最匹配的模式的处理程序。

那么,这与只做以下操作有什么不同呢?

http.ListenAndServe(addr, nil)
http.Handle("/home", home)
http.Handle("/login", login)

使用多路复用器的目的是什么?

英文:

I've been studying golang and I noticed a lot of people create servers by using the http.NewServeMux() function and I don't really understand what it does.

I read this:

> In go ServeMux is an HTTP request multiplexer. It matches the URL of
> each incoming request against a list of registered patterns and calls
> the handler for the pattern that most closely matches the URL.

How is that different than just doing something like:

http.ListenAndServe(addr, nil)
http.Handle("/home", home)
http.Handle("/login", login)

What is the purpose of using multiplexing?

答案1

得分: 27

net/http的GoDoc和源代码中。

ListenAndServe函数用于启动一个带有指定地址和处理程序的HTTP服务器。处理程序通常为nil,这意味着使用DefaultServeMux。Handle和HandleFunc函数用于向DefaultServeMux添加处理程序。

DefaultServeMux只是预定义的http.ServeMux

var DefaultServeMux = &defaultServeMux
var defaultServeMux ServeMux

正如你所看到的,http.Handle在内部调用了DefaultServeMux

func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }

http.NewServeMux()的目的是创建自己的http.ServeMux实例,用于在需要两个不同端口和不同路由的http.ListenAndServe函数实例时使用。

英文:

From net/http GoDoc and Source.

ListenAndServe starts an HTTP server with a given address and handler. The handler is usually nil, which means to use DefaultServeMux. Handle and HandleFunc add handlers to DefaultServeMux

DefaultServeMux is just a predefined http.ServeMux

var DefaultServeMux = &defaultServeMux
var defaultServeMux ServeMux

As you can see http.Handle calls DefaultServeMux internally.

func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }

The purpose of http.NewServeMux() is to have your own instance of http.ServerMux for instances like when you require two http.ListenAndServe functions listening to different ports with different routes.

答案2

得分: 7

在Golang中,多路复用器(multiplexer)类似于硬件中的多路复用器,它将多个输入转发到多个输出。

我给你一个简单的例子:

type CustomMultiplexer struct {
}

给定的多路复用器必须实现ServeHTTP方法,以便在HTTP服务器中注册输入。

func (mux CustomMultiplexer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path == "/" {
        SimpleRequestHandler(w, r)
        return
    }
    http.NotFound(w, r)
    return
}

我的SimpleRequestHandler是一个如下所示的方法:

func SimpleRequestHandler(w http.ResponseWriter, r *http.Request) {
    switch r.Method {
    case http.MethodGet:
        mySimpleGetRequestHandler(w, r)
        break
    default:
        http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
        break
    }
}

现在我可以使用我的CustomMultiplexer在传入请求之间进行多路复用。

func main() {
    customServer := CustomServer{}
    err := http.ListenAndServe(":9001", &customServer)
    if err != nil {
        panic(err)
    }
}

http.HandleFunc方法可以作为我给出的简单多路复用器的工作方式。

英文:

the multiplexer in Golang is some things like multiplexer in hardware which multiply some inputs into some outputs

i gave you a simple exampe

type CustomMultiplexer struct {
}

the given multiplexer have to implement the ServeHTTP method to be registered int http to server inputs

func (mux CustomMultiplexer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path == "/" {
	    SimpleRequestHandler(w, r)
	    return
    }
	http.NotFound(w, r)
    return
}

my SimpleRequestHandler is a method as follow

func SimpleRequestHandler(w http.ResponseWriter, r *http.Request) {
    switch r.Method {
    case http.MethodGet:
	    mySimpleGetRequestHandler(w, r)
	    break
    default:
	    http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
	    break
    }
}

now i can use my CustomMultiplxere to do multiplexing between incoming requested

func main() {
    customServer := CustomServer{}
    err := http.ListenAndServe(":9001", &customServer)
    if err != nil {
	    panic(err)
    }
}

the http.HandleFunc method works as my given simple multiplexer.

huangapple
  • 本文由 发表于 2016年11月8日 10:34:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/40478027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定