doudansui6650 2018-10-17 20:12
浏览 26
已采纳

模拟网络连接而无需输入网络拨号

I currently am working on vendor go balancer code. I need to remove the tcp dial call and emulate a successful connection without the call. In the code below, there is this line:

ds, err := net.Dial("tcp", backend.String());if err != nil {
    log.Printf("failed to dial %s: %s", backend, err)
    us.Close()
    return
}

What this does is make a dial to the tcp server and then return connection response in ds, which is defined here: https://golang.org/pkg/net/#Dial

What i need is to obtain the ds without doing the tcp dialer. I'm trying to test the load balancer without any actual tcp calls. So, essentially, when we enter handleConnection wed create a net connection prior to the tcp dial and use this net conn, which should emulate 100% net connection before the tcp dialing begins.

func copy(wc io.WriteCloser, r io.Reader) {        defer wc.Close()
    io.Copy(wc, r)
}


func handleConnection(us net.Conn, backend BA.Backend) {
    if backend == nil {
        log.Printf("no backend available for connection from %s",
        us.RemoteAddr())
        us.Close()
        return
    }

    host, _, _ := net.SplitHostPort(us.RemoteAddr().String())
    _, ok := dbAuthTokenData[host]; if !ok {
        w := bufio.NewWriter(us)
        w.WriteString("InvalidCredentials")
        w.Flush()
        us.Close()
        return
    }

    ds, err := net.Dial("tcp", backend.String());if err != nil {
        log.Printf("failed to dial %s: %s", backend, err)
        us.Close()
        return
    }

    // Ignore errors
    go copy(ds, us)
    go copy(us, ds)
}

func tcpBalance(bind string, backends BA.Backends) error {
    log.Println("using tcp balancing")
    ln, err := net.Listen("tcp", bind)
    if err != nil {
        return fmt.Errorf("failed to bind: %s", err)
    }

    log.Printf("listening on %s, balancing %d backends", bind,                                 backends.Len())

    for {
        conn, err := ln.Accept()
        if err != nil {
           log.Printf("failed to accept: %s", err)
            continue
        }
        go handleConnection(conn, backends.Choose())
    }

    return err
}

I tried commenting out go handleConnection(conn, backends.Choose()) but that failed.

  • 写回答

1条回答 默认 最新

  • duanchao1002 2018-10-18 09:39
    关注

    The pattern you could refactor your code is to create a Dialer interface. In your code example you are using the returned ds just as an io.ReadWriteCloser. So you don't need to implement the whole net.Conn interface. As net.Conn has the read and write method inside everything works

    type Dialer interface{
        Dial(network, address string) (io.ReadWriteCloser, error)
    }
    

    Now let's change your function:

    func handleConnection(us net.Conn, backend BA.Backend, d Dialer) {
        // ...
    
        // Code here stays
        ds, err := d.Dial("tcp", backend.String());if err != nil {
            log.Printf("failed to dial %s: %s", backend, err)
            us.Close()
            return
        }
        // ...
    }
    

    That your production code works you now need to define a type netDialer which wraps the net.Dial() function. In your test you can use a testDialer which uses a bytes.Buffer.

    This answers your question:

    Emulate net connection without entering net dial

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?