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

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

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要