I'm working with someone else's code and I need some assistance.
Here we have a tcp load balancer. What I need is access to the request uri from this piece of code before we do the net dial.
I looked through the API Documentation here: https://golang.org/pkg/net/ but was unable to find any relevant methods under the net.Conn
namespace for retrieving the current request path.
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
}
ip:=us.RemoteAddr().String()
parts:=strings.Split(ip,":")
ip=parts[0]
//w := bufio.NewWriter(us)
//w.WriteString(+"
")
//w.Flush()
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
}