如何从另一个函数中关闭一个 HTTP 服务器

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

How do I kill a http server from another function

问题

你可以使用http.ServerShutdown方法来关闭HTTP服务器。在kill_server函数中,你可以创建一个全局变量来保存http.Server的实例,然后在需要关闭服务器时调用Shutdown方法。以下是修改后的代码示例:

package main

import (
	"fmt"
	"log"
	"net/http"
	"time"
)

var server *http.Server

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

func PleaseStart() {
	http.HandleFunc("/", handlerfunc)
	server = &http.Server{Addr: ":80"}
	log.Fatal(server.ListenAndServe())
}

func kill_server() {
	if server != nil {
		err := server.Shutdown(nil)
		if err != nil {
			log.Fatal(err)
		}
	}
}

func main() {
	go PleaseStart()

	kill_server()
}

在这个修改后的代码中,我们创建了一个全局变量server来保存http.Server的实例。在PleaseStart函数中,我们将server实例化并启动HTTP服务器。在kill_server函数中,我们检查server是否为nil,如果不为nil,则调用Shutdown方法来关闭服务器。

请注意,Shutdown方法是一个阻塞调用,它会等待所有正在处理的请求完成后再关闭服务器。如果你希望立即关闭服务器而不等待请求完成,可以传递一个context.Context参数给Shutdown方法,然后在需要关闭服务器时取消该上下文。

英文:
package main

import (
    "fmt"
    "log"
    "net/http"
    "time"
)


func handlerfunc(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World" )
	}
	
    http.ServeContent(w, r, str , time.Now(), f)
}

func PleaseStart(){
	http.HandleFunc("/" , handlerfunc )
	log.Fatal(http.ListenAndServe(":80", nil))
}

func kill_server() {
	//How do I kill server from here
}


func main() {
	go PleaseStart()
	
	kill_server()
}

I start a http.server that responds with hello world, how do I kill it from kill_server function
Is this possible without rewritting everything?

答案1

得分: 1

很抱歉,我只能为您提供翻译服务,无法执行代码或提供代码运行环境。以下是您提供的代码的翻译:

很遗憾,不可以。http.ListenAndServe 生成了一个本地的 http.Server 实例,您无法访问该实例,而您需要访问它才能停止它。您需要创建自己的 http.Server 实例,并将处理程序注册到该实例中,而不是使用 http.HandleFunc。然后,您可以使用 Close() 立即关闭服务器,或者使用 Shutdown() 进行优雅停止(在关闭之前完成正在处理的请求)。

var server *http.Server
//...
func PleaseStart() {
    http.HandleFunc("/", handlerfunc)
    server = &http.Server{
        Addr:    ":80",
        Handler: http.DefaultServeMux,
    }
    log.Fatal(server.ListenAndServe())
}

func kill_server() {
    server.Close()
}

希望对您有所帮助!

英文:

Unfortunately not. http.ListenAndServe generates a local http.Server instance, which you cannot gain access to, which is what you would need to be able to stop it. You'll need to create your own http.Server instance, and register your handlers with that instead of using http.HandleFunc. Then you can shut down the server using Close() to shut down immediately, or Shutdown() to do a graceful stop (letting requests in flight complete before shutting down).

var server *http.Server
//...
func PleaseStart() {
    http.HandleFunc("/", handlerfunc)
    server = &http.Server{
        Addr:    ":80",
		Handler: http.DefaultServeMux,
    }
    log.Fatal(server.ListenAndServe())
}

func kill_server() {
    server.Close()
}

huangapple
  • 本文由 发表于 2017年5月20日 04:01:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/44078189.html
匿名

发表评论

匿名网友

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

确定