Skip to content

Commit

Permalink
Use port decided by OS instead of randomly selecting one
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Sep 25, 2023
1 parent 696485d commit ca7ba55
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
8 changes: 4 additions & 4 deletions api/grpcserver/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ServiceAPI interface {

// Server is a very basic grpc server.
type Server struct {
Listener string
listener string
logger log.Logger
// BoundAddress contains the address that the server bound to, useful if
// the server uses a dynamic port. It is set during startup and can be
Expand All @@ -34,7 +34,7 @@ type Server struct {
func New(listener string, lg log.Logger, opts ...grpc.ServerOption) *Server {
opts = append(opts, ServerOptions...)
return &Server{
Listener: listener,
listener: listener,
logger: lg,
GrpcServer: grpc.NewServer(opts...),
}
Expand All @@ -43,15 +43,15 @@ func New(listener string, lg log.Logger, opts ...grpc.ServerOption) *Server {
// Start starts the server.
func (s *Server) Start() error {
s.logger.With().Info("starting grpc server",
log.String("address", s.Listener),
log.String("address", s.listener),
log.Array("services", log.ArrayMarshalerFunc(func(encoder log.ArrayEncoder) error {
for svc := range s.GrpcServer.GetServiceInfo() {
encoder.AppendString(svc)
}
return nil
})),
)
lis, err := net.Listen("tcp", s.Listener)
lis, err := net.Listen("tcp", s.listener)
if err != nil {
s.logger.Error("error listening: %v", err)
return err
Expand Down
15 changes: 6 additions & 9 deletions api/grpcserver/grpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"log"
"math"
"math/big"
"math/rand"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -478,13 +477,8 @@ func launchServer(tb testing.TB, services ...ServiceAPI) (Config, func()) {
cfg := DefaultTestConfig()

// run on a random port
port := 1024 + rand.Intn(9997)
cfg.PublicListener = fmt.Sprintf("127.0.0.1:%d", port)
cfg.PrivateListener = fmt.Sprintf("127.0.0.1:%d", port+1)
cfg.JSONListener = fmt.Sprintf("127.0.0.1:%d", port+2)

grpcService := New(cfg.PublicListener, logtest.New(tb, zapcore.InfoLevel).Named("grpc"))
jsonService := NewJSONHTTPServer(cfg.JSONListener, logtest.New(tb).WithName("grpc.JSON"))
grpcService := New("127.0.0.1:0", logtest.New(tb, zapcore.InfoLevel).Named("grpc"))
jsonService := NewJSONHTTPServer("127.0.0.1:0", logtest.New(tb).WithName("grpc.JSON"))

// attach services
for _, svc := range services {
Expand All @@ -499,6 +493,9 @@ func launchServer(tb testing.TB, services ...ServiceAPI) (Config, func()) {
require.NoError(tb, err)
}

cfg.PublicListener = grpcService.BoundAddress
cfg.JSONListener = jsonService.BoundAddress

return cfg, func() {
require.NoError(tb, jsonService.Shutdown(context.Background()))
_ = grpcService.Close()
Expand Down Expand Up @@ -539,7 +536,7 @@ func TestNewServersConfig(t *testing.T) {
grpcService := New(fmt.Sprintf(":%d", port1), logtest.New(t).Named("grpc"))
jsonService := NewJSONHTTPServer(fmt.Sprintf(":%d", port2), logtest.New(t).WithName("grpc.JSON"))

require.Contains(t, grpcService.Listener, strconv.Itoa(port1), "Expected same port")
require.Contains(t, grpcService.listener, strconv.Itoa(port1), "Expected same port")
require.Contains(t, jsonService.listener, strconv.Itoa(port2), "Expected same port")
}

Expand Down
14 changes: 10 additions & 4 deletions api/grpcserver/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ import (
// JSONHTTPServer is a JSON http server providing the Spacemesh API.
// It is implemented using a grpc-gateway. See https://github.com/grpc-ecosystem/grpc-gateway .
type JSONHTTPServer struct {
logger log.Logger

listener string
server *http.Server
grp errgroup.Group
logger log.Logger

// BoundAddress contains the address that the server bound to, useful if
// the server uses a dynamic port. It is set during startup and can be
// safely accessed after Start has completed (I.E. the returned channel has
// been waited on)
BoundAddress string
server *http.Server
grp errgroup.Group
}

// NewJSONHTTPServer creates a new json http server.
Expand Down Expand Up @@ -100,6 +105,7 @@ func (s *JSONHTTPServer) StartService(
s.logger.Error("error listening: %v", err)
return err
}
s.BoundAddress = lis.Addr().String()
s.server = &http.Server{
Handler: mux,
}
Expand Down

0 comments on commit ca7ba55

Please sign in to comment.