亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Go中停止偵聽服務器

如何在Go中停止偵聽服務器

Go
慕斯709654 2021-05-14 10:44:03
我一直試圖找到一種方法來優雅地停止Go中的監聽服務器。因為有listen.Accept塊,所以有必要關閉偵聽套接字以發出結束信號,但由于未導出相關錯誤,因此我無法分辨該錯誤以及其他任何錯誤。我可以做得更好嗎?請參閱FIXME下面的代碼serve()package mainimport (    "io"    "log"    "net"    "time")// Echo server structtype EchoServer struct {    listen net.Listener    done   chan bool}// Respond to incoming connection//// Write the address connected to then echofunc (es *EchoServer) respond(remote *net.TCPConn) {    defer remote.Close()    _, err := io.Copy(remote, remote)    if err != nil {        log.Printf("Error: %s", err)    }}// Listen for incoming connectionsfunc (es *EchoServer) serve() {    for {        conn, err := es.listen.Accept()        // FIXME I'd like to detect "use of closed network connection" here        // FIXME but it isn't exported from net        if err != nil {            log.Printf("Accept failed: %v", err)            break        }        go es.respond(conn.(*net.TCPConn))    }    es.done <- true}// Stop the server by closing the listening listenfunc (es *EchoServer) stop() {    es.listen.Close()    <-es.done}// Make a new echo serverfunc NewEchoServer(address string) *EchoServer {    listen, err := net.Listen("tcp", address)    if err != nil {        log.Fatalf("Failed to open listening socket: %s", err)    }    es := &EchoServer{        listen: listen,        done:   make(chan bool),    }    go es.serve()    return es}// Mainfunc main() {    log.Println("Starting echo server")    es := NewEchoServer("127.0.0.1:18081")    // Run the server for 1 second    time.Sleep(1 * time.Second)    // Close the server    log.Println("Stopping echo server")    es.stop()}此打印2012/11/16 12:53:35 Starting echo server2012/11/16 12:53:36 Stopping echo server2012/11/16 12:53:36 Accept failed: accept tcp 127.0.0.1:18081: use of closed network connection我想隱藏Accept failed消息,但是顯然我不想掩蓋其他Accept可以報告的錯誤。我當然可以查看錯誤測試,use of closed network connection但這確實很丑陋。我可以設置一個標志,說如果要設置,我將要關閉并忽略錯誤-有更好的方法嗎?
查看完整描述

2 回答

?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

accept()調用后立即在循環中檢查一些“是否該停止了”標志,然后從中將其翻轉main,然后連接到偵聽端口以獲取服務器套接字“未卡住”的標志。這與舊的“自管技巧”非常相似。


查看完整回答
反對 回復 2021-05-24
?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

我可以通過在關閉連接之前使用es.done發送信號來處理此問題。除了以下代碼,您還需要使用make(chan bool,1)創建es.done,以便我們可以在其中添加單個值而不會阻塞。


// Listen for incoming connections

func (es *EchoServer) serve() {

  for {

    conn, err := es.listen.Accept()

    if err != nil {

      select {

      case <-es.done:

        // If we called stop() then there will be a value in es.done, so

        // we'll get here and we can exit without showing the error.

      default:

        log.Printf("Accept failed: %v", err)

      }

      return

    }

    go es.respond(conn.(*net.TCPConn))

  }

}


// Stop the server by closing the listening listen

func (es *EchoServer) stop() {

  es.done <- true   // We can advance past this because we gave it buffer of 1

  es.listen.Close() // Now it the Accept will have an error above

}


查看完整回答
反對 回復 2021-05-24
  • 2 回答
  • 0 關注
  • 305 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號