解决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监听地址有一定几率互相覆盖导致, 所以加一个睡眠时间, 让两个协程执行时间错开.

原文地址:https://www.cnblogs.com/xuange306/p/9400137.html