解決beego中同時開啟http和https時,https端口占用問題


在beego的app.go文件中, 找到

// run normal mode
    if BConfig.Listen.EnableHTTPS {
        go func() {
            time.Sleep(1000 * time.Microsecond) //這里是我修改后的代碼, 原本只是睡眠了 20 * Microsecond,我這里改到了1000, 可以繼續往大改
            if BConfig.Listen.HTTPSPort != 0 {
                app.Server.Addr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPSAddr, BConfig.Listen.HTTPSPort)
            } else if BConfig.Listen.EnableHTTP {
                BeeLogger.Info("Start https server error, confict with http.Please reset https port")
                return
            }
            logs.Info("https server Running on https://%s", app.Server.Addr)
            if err := app.Server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil {
                logs.Critical("ListenAndServeTLS: ", err)
                time.Sleep(100 * time.Microsecond)
                endRunning <- true
            }
        }()
    }

  問題原因分析: 初步判斷為http監聽地址和https監聽地址有一定幾率互相覆蓋導致, 所以加一個睡眠時間, 讓兩個協程執行時間錯開.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM