Skip to content

Commit 9c1a0d5

Browse files
updates
1 parent c09ba47 commit 9c1a0d5

File tree

3 files changed

+64
-68
lines changed

3 files changed

+64
-68
lines changed

sdk/net/factory.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66

77
"github.com/LumeraProtocol/lumera/x/lumeraid/securekeyx"
8-
"github.com/LumeraProtocol/supernode/pkg/net/grpc/client"
98
"github.com/LumeraProtocol/supernode/sdk/adapters/lumera"
109
"github.com/LumeraProtocol/supernode/sdk/log"
1110

@@ -20,11 +19,10 @@ type FactoryConfig struct {
2019

2120
// ClientFactory creates and manages supernode clients
2221
type ClientFactory struct {
23-
logger log.Logger
24-
keyring keyring.Keyring
25-
clientOptions *client.ClientOptions
26-
config FactoryConfig
27-
lumeraClient lumera.Client
22+
logger log.Logger
23+
keyring keyring.Keyring
24+
config FactoryConfig
25+
lumeraClient lumera.Client
2826
}
2927

3028
// NewClientFactory creates a new client factory with the provided dependencies
@@ -37,11 +35,10 @@ func NewClientFactory(ctx context.Context, logger log.Logger, keyring keyring.Ke
3735
"localAddress", config.LocalCosmosAddress)
3836

3937
return &ClientFactory{
40-
logger: logger,
41-
keyring: keyring,
42-
clientOptions: client.DefaultClientOptions(),
43-
config: config,
44-
lumeraClient: lumeraClient,
38+
logger: logger,
39+
keyring: keyring,
40+
config: config,
41+
lumeraClient: lumeraClient,
4542
}
4643
}
4744

@@ -56,8 +53,7 @@ func (f *ClientFactory) CreateClient(ctx context.Context, supernode lumera.Super
5653
"endpoint", supernode.GrpcEndpoint)
5754

5855
// Create client with dependencies
59-
client, err := NewSupernodeClient(ctx, f.logger, f.keyring, f.config, supernode, f.lumeraClient,
60-
f.clientOptions)
56+
client, err := NewSupernodeClient(ctx, f.logger, f.keyring, f.config, supernode, f.lumeraClient)
6157
if err != nil {
6258
return nil, fmt.Errorf("failed to create supernode client for %s: %w", supernode.CosmosAddress, err)
6359
}

sdk/net/impl.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package net
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
"github.com/LumeraProtocol/lumera/x/lumeraid/securekeyx"
89
"github.com/LumeraProtocol/supernode/gen/supernode/action/cascade"
@@ -32,7 +33,7 @@ var _ SupernodeClient = (*supernodeClient)(nil)
3233
// NewSupernodeClient creates a new supernode client
3334
func NewSupernodeClient(ctx context.Context, logger log.Logger, keyring keyring.Keyring,
3435
factoryConfig FactoryConfig, targetSupernode lumera.Supernode, lumeraClient lumera.Client,
35-
clientOptions *client.ClientOptions,
36+
3637
) (SupernodeClient, error) {
3738
// Register ALTS protocols, just like in the test
3839
conn.RegisterALTSRecordProtocols()
@@ -74,10 +75,22 @@ func NewSupernodeClient(ctx context.Context, logger log.Logger, keyring keyring.
7475
logger.Info(ctx, "Connecting to supernode securely", "endpoint", targetSupernode.GrpcEndpoint, "target_id", targetSupernode.CosmosAddress, "local_id", factoryConfig.LocalCosmosAddress, "peer_type", factoryConfig.PeerType)
7576

7677
// Use provided client options or defaults
77-
options := clientOptions
78-
if options == nil {
79-
options = client.DefaultClientOptions()
80-
}
78+
79+
options := client.DefaultClientOptions()
80+
81+
options.MaxRecvMsgSize = 500 * 1024 * 1024
82+
options.MaxSendMsgSize = 500 * 1024 * 1024
83+
options.InitialWindowSize = (int32)(16 * 1024 * 1024)
84+
options.InitialConnWindowSize = (int32)(16 * 1024 * 1024)
85+
options.ConnWaitTime = 15 * time.Second
86+
87+
options.KeepAliveTime = 3 * time.Minute // Ping every 3 minutes
88+
options.KeepAliveTimeout = 90 * time.Second // 90 second timeout
89+
options.AllowWithoutStream = true
90+
options.MaxRetries = 3
91+
options.RetryWaitTime = 2 * time.Second
92+
options.EnableRetries = true
93+
options.MinConnectTimeout = 30 * time.Second
8194

8295
// Connect to server with secure credentials
8396
grpcClient := client.NewClient(clientCreds)

supernode/node/supernode/server/server.go

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ import (
66
"net"
77
"strconv"
88
"strings"
9+
"time"
910

1011
"google.golang.org/grpc"
1112
"google.golang.org/grpc/grpclog"
1213
"google.golang.org/grpc/health"
1314
healthpb "google.golang.org/grpc/health/grpc_health_v1"
14-
"google.golang.org/grpc/reflection"
1515

1616
"github.com/LumeraProtocol/lumera/x/lumeraid/securekeyx"
1717
"github.com/LumeraProtocol/supernode/pkg/errgroup"
18-
"github.com/LumeraProtocol/supernode/pkg/errors"
1918
"github.com/LumeraProtocol/supernode/pkg/log"
2019
"github.com/LumeraProtocol/supernode/pkg/lumera"
2120

2221
ltc "github.com/LumeraProtocol/supernode/pkg/net/credentials"
2322
"github.com/LumeraProtocol/supernode/pkg/net/credentials/alts/conn"
23+
grpcserver "github.com/LumeraProtocol/supernode/pkg/net/grpc/server"
2424
"github.com/cosmos/cosmos-sdk/crypto/keyring"
2525
)
2626

@@ -34,14 +34,13 @@ type Server struct {
3434
services []service
3535
name string
3636
kr keyring.Keyring
37-
grpcServer *grpc.Server
37+
grpcServer *grpcserver.Server
3838
lumeraClient lumera.Client
3939
healthServer *health.Server
4040
}
4141

4242
// Run starts the server
4343
func (server *Server) Run(ctx context.Context) error {
44-
4544
conn.RegisterALTSRecordProtocols()
4645
defer conn.UnregisterALTSRecordProtocols()
4746
grpclog.SetLoggerV2(log.NewLoggerWithErrorLevel())
@@ -56,44 +55,39 @@ func (server *Server) Run(ctx context.Context) error {
5655
return fmt.Errorf("failed to setup gRPC server: %w", err)
5756
}
5857

58+
// Custom server options
59+
opts := grpcserver.DefaultServerOptions()
60+
61+
opts.GracefulShutdownTime = 60 * time.Second
62+
opts.MaxConnectionIdle = 6 * time.Hour // Extended for long transfers
63+
opts.MaxConnectionAge = 6 * time.Hour // Extended for long transfers
64+
65+
// Frequent keepalive to detect issues early
66+
opts.Time = 5 * time.Minute // Ping every 5 mins
67+
opts.Timeout = 2 * time.Minute // 2 min ping timeout
68+
69+
// CRITICAL: Large flow control windows for 1GB files
70+
opts.InitialWindowSize = (int32)(16 * 1024 * 1024) // 16MB (was 1MB)
71+
opts.InitialConnWindowSize = (int32)(16 * 1024 * 1024) // 16MB (was 1MB)
72+
73+
// Large message and buffer sizes for 1GB streaming
74+
opts.MaxRecvMsgSize = 500 * 1024 * 1024 // 500MB
75+
opts.MaxSendMsgSize = 500 * 1024 * 1024 // 500MB
76+
opts.WriteBufferSize = 256 * 1024 // 256KB buffer
77+
opts.ReadBufferSize = 256 * 1024 // 256KB buffer
78+
5979
for _, address := range addresses {
6080
addr := net.JoinHostPort(strings.TrimSpace(address), strconv.Itoa(server.config.Port))
6181
address := addr // Create a new variable to avoid closure issues
6282

6383
group.Go(func() error {
64-
return server.listen(ctx, address)
84+
return server.grpcServer.Serve(ctx, address, opts)
6585
})
6686
}
6787

6888
return group.Wait()
6989
}
7090

71-
func (server *Server) listen(ctx context.Context, address string) (err error) {
72-
listen, err := net.Listen("tcp", address)
73-
if err != nil {
74-
return errors.Errorf("listen: %w", err).WithField("address", address)
75-
}
76-
77-
errCh := make(chan error, 1)
78-
go func() {
79-
defer errors.Recover(func(recErr error) { err = recErr })
80-
log.WithContext(ctx).Infof("gRPC server listening securely on %q", address)
81-
if err := server.grpcServer.Serve(listen); err != nil {
82-
errCh <- errors.Errorf("serve: %w", err).WithField("address", address)
83-
}
84-
}()
85-
86-
select {
87-
case <-ctx.Done():
88-
log.WithContext(ctx).Infof("Shutting down gRPC server at %q", address)
89-
server.grpcServer.GracefulStop()
90-
case err := <-errCh:
91-
return err
92-
}
93-
94-
return nil
95-
}
96-
9791
func (server *Server) setupGRPCServer() error {
9892
// Create server credentials
9993
serverCreds, err := ltc.NewServerCreds(&ltc.ServerOptions{
@@ -108,24 +102,18 @@ func (server *Server) setupGRPCServer() error {
108102
return fmt.Errorf("failed to create server credentials: %w", err)
109103
}
110104

111-
// Initialize the gRPC server with credentials (secure)
112-
server.grpcServer = grpc.NewServer(grpc.Creds(serverCreds))
105+
// Create wrapper server
106+
server.grpcServer = grpcserver.NewServer(server.name, serverCreds)
113107

114-
// Initialize and register the health server
108+
// Setup health server
115109
server.healthServer = health.NewServer()
116-
healthpb.RegisterHealthServer(server.grpcServer, server.healthServer)
117-
118-
// Register reflection service
119-
reflection.Register(server.grpcServer)
120-
121-
// Set all services as serving
110+
server.grpcServer.RegisterService(&healthpb.Health_ServiceDesc, server.healthServer)
122111
server.healthServer.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
123112

124-
// Register all services and set their health status
113+
// Register all services
125114
for _, service := range server.services {
126-
serviceName := service.Desc().ServiceName
127115
server.grpcServer.RegisterService(service.Desc(), service)
128-
server.healthServer.SetServingStatus(serviceName, healthpb.HealthCheckResponse_SERVING)
116+
server.healthServer.SetServingStatus(service.Desc().ServiceName, healthpb.HealthCheckResponse_SERVING)
129117
}
130118

131119
return nil
@@ -143,16 +131,15 @@ func (server *Server) Close() {
143131
if server.healthServer != nil {
144132
// Set all services to NOT_SERVING before shutdown
145133
server.healthServer.SetServingStatus("", healthpb.HealthCheckResponse_NOT_SERVING)
146-
147-
// Allow a short time for health status to propagate
148134
for _, service := range server.services {
149135
serviceName := service.Desc().ServiceName
150136
server.healthServer.SetServingStatus(serviceName, healthpb.HealthCheckResponse_NOT_SERVING)
151137
}
152138
}
153139

140+
// Wrapper handles all gRPC server cleanup
154141
if server.grpcServer != nil {
155-
server.grpcServer.GracefulStop()
142+
server.grpcServer.Close()
156143
}
157144
}
158145

@@ -163,10 +150,10 @@ func New(config *Config, name string, kr keyring.Keyring, lumeraClient lumera.Cl
163150
}
164151

165152
return &Server{
166-
config: config,
167-
services: services,
168-
name: name,
169-
kr: kr,
153+
config: config,
154+
services: services,
155+
name: name,
156+
kr: kr,
170157
lumeraClient: lumeraClient,
171158
}, nil
172159
}

0 commit comments

Comments
 (0)