mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
Merge pull request #385 from wangrzneu/master
support get the actual listened address after grpc server with zero port is started
This commit is contained in:
commit
e14170de04
1
go.mod
1
go.mod
@ -26,6 +26,7 @@ require (
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
||||
github.com/leodido/go-urn v1.1.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.2 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/montanaflynn/stats v0.5.0
|
||||
github.com/openzipkin/zipkin-go v0.2.1
|
||||
github.com/otokaze/mock v0.0.0-20190125081256-8282b7a7c7c3
|
||||
|
@ -302,6 +302,26 @@ func (s *Server) RunUnix(file string) error {
|
||||
// will panic if any error happend
|
||||
// return server itself
|
||||
func (s *Server) Start() (*Server, error) {
|
||||
_, err := s.startWithAddr()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// StartWithAddr create a new goroutine run server with configured listen addr
|
||||
// will panic if any error happend
|
||||
// return server itself and the actually listened address (if configured listen
|
||||
// port is zero, the os will allocate an unused port)
|
||||
func (s *Server) StartWithAddr() (*Server, net.Addr, error) {
|
||||
addr, err := s.startWithAddr()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return s, addr, nil
|
||||
}
|
||||
|
||||
func (s *Server) startWithAddr() (net.Addr, error) {
|
||||
lis, err := net.Listen(s.conf.Network, s.conf.Addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -313,7 +333,7 @@ func (s *Server) Start() (*Server, error) {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
return s, nil
|
||||
return lis.Addr(), nil
|
||||
}
|
||||
|
||||
// Serve accepts incoming connections on the listener lis, creating a new
|
||||
|
@ -591,3 +591,15 @@ func TestMetadata(t *testing.T) {
|
||||
_, err := cli.SayHello(ctx, &pb.HelloRequest{Name: "test"})
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestStartWithAddr(t *testing.T) {
|
||||
configuredAddr := "127.0.0.1:0"
|
||||
server = NewServer(&ServerConfig{Addr: configuredAddr, Timeout: xtime.Duration(time.Second)})
|
||||
if _, realAddr, err := server.StartWithAddr(); err == nil && realAddr != nil {
|
||||
assert.NotEqual(t, realAddr.String(), configuredAddr)
|
||||
} else {
|
||||
assert.NotNil(t, realAddr)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user