-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: cleanup server logic (backport #15041) #16203
Closed
Closed
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
509202c
refactor: cleanup server logic (backport #15041)
alexanderbez 42ca2d1
cleanup for easier review
yihuang 1b894c3
fix grpc-web server in test
yihuang 13731f6
cleanup
yihuang 58080ee
fix TestCLIQueryConn
yihuang 86f3c87
fix grpc test
yihuang 7b00928
fix test
yihuang 31c5010
add missing WaitForHeight
yihuang 70cf0ca
Merge branch 'release/v0.46.x' into release/v0.46.x
yihuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/improbable-eng/grpc-web/go/grpcweb" | ||
"github.com/tendermint/tendermint/libs/log" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/cosmos/cosmos-sdk/server/config" | ||
"github.com/cosmos/cosmos-sdk/server/types" | ||
) | ||
|
||
// StartGRPCWeb starts a gRPC-Web server on the given address. | ||
func StartGRPCWeb(grpcSrv *grpc.Server, config config.Config) (*http.Server, error) { | ||
func StartGRPCWeb(ctx context.Context, logger log.Logger, grpcSrv *grpc.Server, config config.GRPCWebConfig) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we ok breaking the API in a point release? |
||
var options []grpcweb.Option | ||
if config.GRPCWeb.EnableUnsafeCORS { | ||
if config.EnableUnsafeCORS { | ||
options = append(options, | ||
grpcweb.WithOriginFunc(func(origin string) bool { | ||
return true | ||
|
@@ -25,22 +26,31 @@ func StartGRPCWeb(grpcSrv *grpc.Server, config config.Config) (*http.Server, err | |
|
||
wrappedServer := grpcweb.WrapServer(grpcSrv, options...) | ||
grpcWebSrv := &http.Server{ | ||
Addr: config.GRPCWeb.Address, | ||
Addr: config.Address, | ||
Handler: wrappedServer, | ||
ReadHeaderTimeout: 500 * time.Millisecond, | ||
} | ||
|
||
errCh := make(chan error) | ||
go func() { | ||
logger.Info("starting gRPC web server...", "address", config.Address) | ||
if err := grpcWebSrv.ListenAndServe(); err != nil { | ||
errCh <- fmt.Errorf("[grpc] failed to serve: %w", err) | ||
} | ||
}() | ||
|
||
// Start a blocking select to wait for an indication to stop the server or that | ||
// the server failed to start properly. | ||
select { | ||
case <-ctx.Done(): | ||
// The calling process cancelled or closed the provided context, so we must | ||
// gracefully stop the gRPC-web server. | ||
logger.Info("stopping gRPC web server...", "address", config.Address) | ||
grpcWebSrv.Close() | ||
return nil | ||
|
||
case err := <-errCh: | ||
return nil, err | ||
case <-time.After(types.ServerStartTime): // assume server started successfully | ||
return grpcWebSrv, nil | ||
logger.Error("failed to start gRPC Web server", "err", err) | ||
return err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto