/*
NewExportedPort opens a new anonymous port on "ip" and export it through etcd
as "servicename". If "ip" is not a host:port pair, the port will be chosen at
random.
*/
NewExportedPort opens a new anonymous port on "ip" and export it through etcd
as "servicename". If "ip" is not a host:port pair, the port will be chosen at
random.
/ * NewExportedPort opens a new anonymous port on " ip " and export it through etcd as " servicename " . If " ip " is not a host : port pair the port will be chosen at random . * /
func (e *ServiceExporter) NewExportedPort(
	ctx context.Context, network, ip, service string) (net.Listener, error) {
	var record discovery.ExportedServiceRecord
	var recordData []byte
	var path string
	var host, hostport string
	var port int
	var l net.Listener
	var err error
	if _, _, err = net.SplitHostPort(ip); err != nil {
		hostport = net.JoinHostPort(ip, "0")
	} else {
		hostport = ip
	}
	if l, err = net.Listen(network, hostport); err != nil {
		return nil, err
	}
	path = fmt.Sprintf("/ns/service/%s/%016x", service, e.leaseID)
	if host, hostport, err = net.SplitHostPort(l.Addr().String()); err != nil {
		l.Close()
		return nil, err
	}
	record.Protocol = "tcp"
	record.Address = host
	if port, err = strconv.Atoi(hostport); err != nil {
		l.Close()
		return nil, err
	}
	record.Port = int32(port)
	if recordData, err = proto.Marshal(&record); err != nil {
		l.Close()
		return nil, err
	}
	if _, err = e.kv.Put(ctx, path, string(recordData),
		etcd.WithLease(e.leaseID)); err != nil {
		l.Close()
		return nil, err
	}
	e.path = path
	return l, nil
}
func ( e * ServiceExporter ) NewExportedPort ( ctx context . Context , network , ip , service string ) ( net . Listener , error ) { var record discovery . ExportedServiceRecord var recordData [ ] byte var path string var host , hostport string var port int var l net . Listener var err error if _ , _ , err = net . SplitHostPort ( ip ) ; err != nil { hostport = net . JoinHostPort ( ip , " " ) } else { hostport = ip } if l , err = net . Listen ( network , hostport ) ; err != nil { return nil , err } path = fmt . Sprintf ( " " , service , e . leaseID ) if host , hostport , err = net . SplitHostPort ( l . Addr ( ) . String ( ) ) ; err != nil { l . Close ( ) return nil , err } record . Protocol = " " record . Address = host if port , err = strconv . Atoi ( hostport ) ; err != nil { l . Close ( ) return nil , err } record . Port = int32 ( port ) if recordData , err = proto . Marshal ( & record ) ; err != nil { l . Close ( ) return nil , err } if _ , err = e . kv . Put ( ctx , path , string ( recordData ) , etcd . WithLease ( e . leaseID ) ) ; err != nil { l . Close ( ) return nil , err } e . path = path return l , nil }
NewExportedPort opens a new anonymous port on "ip" and export it through etcd
as "servicename".
NewExportedPort opens a new anonymous port on " ip " and export it through etcd as " servicename " .
// Apparently, it's not in host:port format. // Use the lease ID as part of the path; it would be reasonable to expect // it to be unique. // Fill in discovery protocol buffer. // Probably a named port. TODO: should be looked up in /etc/services. // Now write our host:port pair to etcd. Let etcd choose the file name.