refactor(serverv2): remove unused interface methods, honuor context (#22394)
This commit is contained in:
@@ -25,6 +25,7 @@ import (
|
||||
"cosmossdk.io/core/transaction"
|
||||
"cosmossdk.io/log"
|
||||
serverv2 "cosmossdk.io/server/v2"
|
||||
"cosmossdk.io/server/v2/api"
|
||||
"cosmossdk.io/server/v2/api/grpc/gogoreflection"
|
||||
)
|
||||
|
||||
@@ -197,13 +198,13 @@ func (s *Server[T]) Config() any {
|
||||
return s.config
|
||||
}
|
||||
|
||||
func (s *Server[T]) Start(context.Context) error {
|
||||
func (s *Server[T]) Start(ctx context.Context) error {
|
||||
if !s.config.Enable {
|
||||
s.logger.Info(fmt.Sprintf("%s server is disabled via config", s.Name()))
|
||||
return nil
|
||||
}
|
||||
|
||||
listener, err := net.Listen("tcp", s.config.Address)
|
||||
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", s.config.Address)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to listen on address %s: %w", s.config.Address, err)
|
||||
}
|
||||
@@ -222,8 +223,7 @@ func (s *Server[T]) Stop(ctx context.Context) error {
|
||||
}
|
||||
|
||||
s.logger.Info("stopping gRPC server...", "address", s.config.Address)
|
||||
s.grpcSrv.GracefulStop()
|
||||
return nil
|
||||
return api.DoUntilCtxExpired(ctx, s.grpcSrv.GracefulStop)
|
||||
}
|
||||
|
||||
// GetGRPCServer returns the underlying gRPC server.
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package api
|
||||
|
||||
import "context"
|
||||
|
||||
// DoUntilCtxExpired runs the given function until the context is expired or
|
||||
// the function exits.
|
||||
// This forces context to be honored.
|
||||
func DoUntilCtxExpired(ctx context.Context, f func()) error {
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
|
||||
f()
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-done:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDoUntilCtxExpired(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
funcRan := false
|
||||
err := DoUntilCtxExpired(ctx, func() {
|
||||
funcRan = true
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, funcRan)
|
||||
})
|
||||
|
||||
t.Run("context expired", func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
funcRan := false
|
||||
err := DoUntilCtxExpired(ctx, func() {
|
||||
cancel()
|
||||
funcRan = true
|
||||
<-time.After(time.Second)
|
||||
})
|
||||
require.ErrorIs(t, err, context.Canceled)
|
||||
require.True(t, funcRan)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user