ossrs / httpx-static

A HTTP/HTTPS API proxy for SRS.
https://github.com/ossrs/srs
MIT License
642 stars 222 forks source link

Skip port when listen failed. #71

Closed winlinvip closed 7 years ago

winlinvip commented 8 years ago

When shell allocate port, the port maybe listened by other processes, we should check it, ignore the unavailable port.

Akagi201 commented 8 years ago

端口池分配的逻辑感觉有点问题:

  1. 用 64 分割成两个部分的好处没有看出来.
  2. 释放的端口放到数组的末尾, 然后新分配的端口从数组的开始来分配. 这样端口没有得到好的重复利用, 应该优先利用释放过的端口, 这样总的占用的端口就会很少.
  3. Alloc 分配端口, 如果64个端口用完了, v.ports 会从 v.left 中拿, 拿够厚, v.ports 就变成 0 了, 然后下次再 Alloc 分配端口, 又要从 v.left 中拿端口, 这里应该判断不够立刻分配 64 个端口给 v.ports 吧.
Akagi201 commented 8 years ago
// Check if a port is available
func Check(port int) (status bool, err error) {

    // Concatenate a colon and the port
    host := ":" + strconv.Itoa(port)

    // Try to create a server with the port
    server, err := net.Listen("tcp", host)

    // if it fails then the port is likely taken
    if err != nil {
        return false, err
    }

    // close the server
    server.Close()

    // we successfully used and closed the port
    // so it's now available to be used again
    return true, nil
}

这段代码不知道行不行, 没找到更好方法.

winlinvip commented 8 years ago

如果port占用,应该再次分配,你提pr吧。

Akagi201 commented 8 years ago

@winlinvip OK, 我试试.

Akagi201 commented 7 years ago

Fixed by https://github.com/ossrs/go-oryx/pull/72