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

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

如何在 *tls.Conn 上設置 SetKeepAlivePeriod

如何在 *tls.Conn 上設置 SetKeepAlivePeriod

Go
手掌心 2022-06-27 15:21:28
我想增加 HTTP 和 HTTPS 請求的 TCP 連接的保持活動時間。對于 HTTP 請求,可以這樣完成:package mainimport (    "fmt"    "io"    "log"    "net"    "net/http"    "time")func main() {    server := &http.Server{Addr: ":8080", Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {        io.WriteString(w, "Hello, World!")    })}    server.ConnState = func(conn net.Conn, state http.ConnState) {        if state == http.StateNew {            if err := conn.(*net.TCPConn).SetKeepAlivePeriod(1000 * time.Second); err != nil {                fmt.Println("Could not set keep alive period", err)            } else {                fmt.Println("update keep alive period")            }        }    }    log.Fatal(server.ListenAndServe())}對于 HTTPS 請求,這不能通過,server.ConnState因為net.Conn將在函數(shù)內(nèi)部傳遞的是*tls.Conn. 此連接不會公開類似的功能SetKeepAlivePeriod或提供對底層*net.TCPConn.func main() {    server := &http.Server{Addr: ":8080", Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {        io.WriteString(w, "Hello, World!")    })}    server.ConnState = func(conn net.Conn, state http.ConnState) {        if state == http.StateNew {            tlsConn := conn.(*tls.Conn)            // how to set SetKeepAlivePeriod        }    }    log.Fatal(server.ListenAndServeTLS("../example.crt", "../example.key"))}如何設置 tls 連接的?;钇冢?
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經(jīng)驗 獲得超7個贊

有(至少)兩種方法可以做到:


使用net.ListenConfig:

該net.ListenConfig對象有一個KeepAlive time.Duration字段。當非零時,這將用于在接受的連接上設置保持活動狀態(tài)(例如:對于 posix 上的 TCP)。


您可以將偵聽器傳遞給ServeTLS:


server := &http.Server{...}


lc := net.ListenConfig{KeepAlive: 1000 * time.Second}

ln, err := lc.Listen(context.Background(), "tcp", ":8080")

if err != nil {

  panic(err)

}

defer ln.Close()


log.Fatal(server.ServeTLS(ln, "../example.crt", "../example.key"))

如前所述,接受的 TCP 連接將自動啟用 keep-alive 并將周期設置為指定值。


使用tls.Config回調(diào):

您可以通過設置tls.Config或回調(diào)來訪問net.Conn底層。tls.Conn GetConfigForClientGetCertificate


只要您返回nil以使 TLS 代碼回退到默認行為,您使用哪一個都沒有關系。重要的部分是訪問tls.ClientHelloInfo,它有一個.Conn指向底層連接的字段。這將net.TCPConn.


setTCPKeepAlive := func(clientHello *tls.ClientHelloInfo) (*tls.Config, error) {

  // Check that the underlying connection really is TCP.

  if tcpConn, ok := clientHello.Conn.(*net.TCPConn); ok {

    if err := tcpConn.SetKeepAlivePeriod(1000 * time.Second); err != nil {

      fmt.Println("Could not set keep alive period", err)

    } else {

      fmt.Println("update keep alive period")

    }

  } else {

    fmt.Println("TLS over non-TCP connection")

  }


  // Make sure to return nil, nil to let the caller fall back on the default behavior.

  return nil, nil

}


tlsConfig := &tls.Config{

    ...

    GetConfigForClient: setTCPKeepAlive,

    ...

}


server := &http.Server{

    Addr:      ":8080",

    TLSConfig: tlsConfig,

}


server.ListenAndServeTLS("../example.crt", "../example.key")


查看完整回答
反對 回復 2022-06-27
  • 1 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號