tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/duplex/talk/dial.go (about) 1 package talk 2 3 import ( 4 "fmt" 5 6 "tractor.dev/toolkit-go/duplex/codec" 7 "tractor.dev/toolkit-go/duplex/mux" 8 ) 9 10 // A Dialer connects to address and establishes a mux.Session 11 type Dialer func(addr string) (mux.Session, error) 12 13 // Dialers is map of transport strings to Dialers 14 // and includes all builtin transports 15 var Dialers map[string]Dialer 16 17 func init() { 18 Dialers = map[string]Dialer{ 19 "tcp": mux.DialTCP, 20 "unix": mux.DialUnix, 21 // "ws": mux.DialWS, 22 "stdio": func(_ string) (mux.Session, error) { 23 return mux.DialStdio() 24 }, 25 } 26 } 27 28 // Dial connects to a remote address using a registered transport and returns a Peer. 29 // Available transports are "tcp", "unix", "ws", and "stdio". In the case of "stdio", 30 // the addr can be left an empty string. 31 func Dial(transport, addr string, codec codec.Codec) (*Peer, error) { 32 d, ok := Dialers[transport] 33 if !ok { 34 return nil, fmt.Errorf("transport '%s' not in available in Dialers", transport) 35 } 36 sess, err := d(addr) 37 if err != nil { 38 return nil, err 39 } 40 return NewPeer(sess, codec), nil 41 }