tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/duplex/rpc/proxy.go (about) 1 package rpc 2 3 import "io" 4 5 // ProxyHandler returns a handler that tries its best to proxy the 6 // call to the dst Client, regardless of call style and assuming the 7 // same encoding. 8 func ProxyHandler(dst *Client) Handler { 9 return HandlerFunc(func(r Responder, c *Call) { 10 ch, err := dst.Session.Open(c.Context) 11 if err != nil { 12 r.Return(err) 13 return 14 } 15 16 framer := &FrameCodec{Codec: dst.codec} 17 enc := framer.Encoder(ch) 18 err = enc.Encode(CallHeader{ 19 S: c.Selector(), 20 }) 21 if err != nil { 22 ch.Close() 23 r.Return(err) 24 return 25 } 26 27 go func() { 28 io.Copy(ch, c.Channel) 29 ch.CloseWrite() 30 }() 31 go func() { 32 io.Copy(c.Channel, ch) 33 c.Channel.Close() 34 }() 35 36 r.(*responder).responded = true 37 r.(*responder).header.C = true 38 }) 39 }