-
Notifications
You must be signed in to change notification settings - Fork 679
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
Add support for VM gRPC services #3294
Open
joshua-kim
wants to merge
11
commits into
master
Choose a base branch
from
cmux
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9c53e9f
support grpc apis
joshua-kim b55bdd9
nit
joshua-kim b7b3c90
remove platform proto
joshua-kim c1295d4
nit
joshua-kim 8facb5c
unexport grpcService
joshua-kim eb46052
lint
joshua-kim 965159d
rwmutex
joshua-kim 345a2e8
nit
joshua-kim b92a872
nit
joshua-kim 5eccff4
nit
joshua-kim 9cb1a56
nit
joshua-kim 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package server | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
var ( | ||
_ http.Handler = (*grpcRouter)(nil) | ||
|
||
ErrDuplicateHandler = errors.New("duplicate handler") | ||
) | ||
|
||
func newGRPCRouter() *grpcRouter { | ||
return &grpcRouter{ | ||
handlers: make(map[string]http.Handler), | ||
} | ||
} | ||
|
||
type grpcRouter struct { | ||
lock sync.RWMutex | ||
handlers map[string]http.Handler | ||
} | ||
|
||
func (g *grpcRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
g.lock.RLock() | ||
defer g.lock.RUnlock() | ||
|
||
// Requests take the form of "/Service/Method" | ||
parsed := strings.Split(r.RequestURI, "/") | ||
if len(parsed) < 2 { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
handler, ok := g.handlers[parsed[1]] | ||
if !ok { | ||
w.WriteHeader(http.StatusNotFound) | ||
return | ||
} | ||
|
||
handler.ServeHTTP(w, r) | ||
} | ||
|
||
func (g *grpcRouter) Add(serviceName string, handler http.Handler) error { | ||
g.lock.Lock() | ||
defer g.lock.Unlock() | ||
|
||
if _, ok := g.handlers[serviceName]; ok { | ||
return fmt.Errorf("failed to register %s: %w", serviceName, ErrDuplicateHandler) | ||
} | ||
|
||
g.handlers[serviceName] = handler | ||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package server | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TODO refactor RegisterChain to return an error so these tests can be against | ||
// the exported package api | ||
func TestGRPCRouterAdd(t *testing.T) { | ||
require := require.New(t) | ||
g := newGRPCRouter() | ||
h := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}) | ||
|
||
require.NoError(g.Add("foo", h)) | ||
|
||
err := g.Add("foo", h) | ||
require.ErrorIs(err, ErrDuplicateHandler) | ||
} | ||
|
||
func TestGRPCRouterServeHTTP(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
handlers []string | ||
uri string | ||
wantCode int | ||
}{ | ||
{ | ||
name: "invalid request", | ||
uri: "foobar", | ||
wantCode: http.StatusBadRequest, | ||
}, | ||
{ | ||
name: "invalid handler", | ||
uri: "/foo/method", | ||
wantCode: http.StatusNotFound, | ||
}, | ||
{ | ||
name: "valid handler", | ||
handlers: []string{"foo"}, | ||
uri: "/foo/method", | ||
wantCode: http.StatusOK, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require := require.New(t) | ||
|
||
g := newGRPCRouter() | ||
h := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}) | ||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest("", "/", nil) | ||
|
||
r.RequestURI = tt.uri | ||
|
||
for _, handler := range tt.handlers { | ||
require.NoError(g.Add(handler, h)) | ||
} | ||
|
||
g.ServeHTTP(w, r) | ||
require.Equal(tt.wantCode, w.Code) | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
syntax = "proto3"; | ||
|
||
package http.request; | ||
|
||
option go_package = "github.com/ava-labs/avalanchego/proto/pb/http/request"; | ||
|
||
service Request { | ||
rpc Body(BodyRequest) returns (BodyReply); | ||
} | ||
|
||
message BodyRequest { | ||
uint32 n = 1; | ||
} | ||
|
||
message BodyReply { | ||
bytes body = 1; | ||
} |
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.
grpc requires a header to indicate the end of an rpc, which is directly set onto the map in the plugin process (ref). We need to propagate this update back into avalanchego (I believe current rpcchainvm is not handling plugin-side header updates correctly).