Skip to content

Commit

Permalink
Migrate RPC to ConnectRPC (#703)
Browse files Browse the repository at this point in the history
ConnectRPC supports following features:

- Supports interoperation and multiplexing of grpc, grpc-web, and
  Connect protocols.
- Supports language-familiar primitives like net/http and fetch.
- Supports simple and no-boilerplate codes.

With this support, we can resolve several issues:

- Removes dependency with Envoy proxy to communicate with JS SDK web
  client by grpc-web.
- Removes dependency with deprecated gogo/protobuf.
- Supports JSON based REST API which will satisfy these needs.
  • Loading branch information
krapie authored and hackerwins committed Dec 15, 2023
1 parent 93d71ee commit e1d2543
Show file tree
Hide file tree
Showing 64 changed files with 8,368 additions and 27,319 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certifica
# Get and place binary to /bin
COPY --from=builder /app/bin/yorkie /bin/

# Expose port 11101, 11102 to the outside world
EXPOSE 11101 11102
# Expose port 8080, 8081 to the outside world
EXPOSE 8080 8081

# Define default entrypoint.
ENTRYPOINT ["yorkie"]
21 changes: 4 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,13 @@ GO_LDFLAGS += -X ${GO_PROJECT}/internal/version.BuildDate=${BUILD_DATE}
default: help

tools: ## install tools for developing yorkie
go install github.com/gogo/protobuf/protoc-gen-gogo@v1.3.2
go install github.com/gogo/protobuf/protoc-gen-gofast@v1.3.2
go install github.com/bufbuild/buf/cmd/buf@v1.28.1
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.31.0
go install connectrpc.com/connect/cmd/protoc-gen-connect-go@v1.12.0
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.1

proto: ## generate proto files
protoc \
-I=./api \
-I=$(GOPATH)/src \
--gofast_out=plugins=grpc,\
Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/wrappers.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,:./api/yorkie/v1 \
api/yorkie/v1/*.proto

protoset: ## generate protoset file
protoc \
-I=./api \
--descriptor_set_out=yorkie.protoset \
--include_imports \
api/yorkie/v1/*.proto
buf generate

build: ## builds an executable that runs in the current environment
CGO_ENABLED=0 go build -o $(EXECUTABLE) -ldflags "${GO_LDFLAGS}" ./cmd/yorkie
Expand Down
58 changes: 29 additions & 29 deletions admin/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ package admin
import (
"context"

"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"connectrpc.com/connect"

"github.com/yorkie-team/yorkie/api/types"
"github.com/yorkie-team/yorkie/internal/version"
Expand All @@ -43,39 +42,40 @@ func (i *AuthInterceptor) SetToken(token string) {
i.token = token
}

// Unary creates a unary server interceptor for authorization.
func (i *AuthInterceptor) Unary() grpc.UnaryClientInterceptor {
// WrapUnary creates a unary server interceptor for authorization.
func (i *AuthInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
return func(
ctx context.Context,
method string,
req,
reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
ctx = metadata.AppendToOutgoingContext(ctx,
types.AuthorizationKey, i.token,
types.UserAgentKey, types.GoSDKType+"/"+version.Version,
)
return invoker(ctx, method, req, reply, cc, opts...)
req connect.AnyRequest,
) (connect.AnyResponse, error) {
req.Header().Add(types.AuthorizationKey, i.token)
req.Header().Add(types.UserAgentKey, types.GoSDKType+"/"+version.Version)

return next(ctx, req)
}
}

// WrapStreamingClient creates a stream client interceptor for authorization.
func (i *AuthInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
return func(
ctx context.Context,
spec connect.Spec,
) connect.StreamingClientConn {
conn := next(ctx, spec)

conn.RequestHeader().Add(types.AuthorizationKey, i.token)
conn.RequestHeader().Add(types.UserAgentKey, types.GoSDKType+"/"+version.Version)

return conn
}
}

// Stream creates a stream server interceptor for authorization.
func (i *AuthInterceptor) Stream() grpc.StreamClientInterceptor {
// WrapStreamingHandler creates a stream server interceptor for authorization.
func (i *AuthInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
return func(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
ctx = metadata.AppendToOutgoingContext(ctx,
types.AuthorizationKey, i.token,
types.UserAgentKey, types.GoSDKType+"/"+version.Version,
)
return streamer(ctx, desc, cc, method, opts...)
conn connect.StreamingHandlerConn,
) error {
return next(ctx, conn)
}
}
Loading

0 comments on commit e1d2543

Please sign in to comment.