第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在Go中停止偵聽服務(wù)器

如何在Go中停止偵聽服務(wù)器

Go
慕斯709654 2021-05-14 10:44:03
我一直試圖找到一種方法來優(yōu)雅地停止Go中的監(jiān)聽服務(wù)器。因?yàn)橛衛(wèi)isten.Accept塊,所以有必要關(guān)閉偵聽套接字以發(fā)出結(jié)束信號(hào),但由于未導(dǎo)出相關(guān)錯(cuò)誤,因此我無法分辨該錯(cuò)誤以及其他任何錯(cuò)誤。我可以做得更好嗎?請(qǐng)參閱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可以報(bào)告的錯(cuò)誤。我當(dāng)然可以查看錯(cuò)誤測試,use of closed network connection但這確實(shí)很丑陋。我可以設(shè)置一個(gè)標(biāo)志,說如果要設(shè)置,我將要關(guān)閉并忽略錯(cuò)誤-有更好的方法嗎?
查看完整描述

2 回答

?
汪汪一只貓

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊

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


查看完整回答
反對(duì) 回復(fù) 2021-05-24
?
侃侃無極

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊

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


// 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

}


查看完整回答
反對(duì) 回復(fù) 2021-05-24
  • 2 回答
  • 0 關(guān)注
  • 303 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)