Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Valient Gough committed Apr 21, 2024
1 parent 70f5ec4 commit 12c253e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 107 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ a user could use the path and associated request metadata to route a request:
func (d *ExampleDirector) Connect(ctx context.Context, method string) (context.Context, *grpc.ClientConn, error) {
// Disable forwarding for all services prefixed with com.example.internal.
if strings.HasPrefix(method, "/com.example.internal.") {
return nil, nil, grpc.Errorf(codes.Unimplemented, "Unknown method")
return nil, nil, status.Errorf(codes.Unimplemented, "Unknown method")
}
md, ok := metadata.FromIncomingContext(ctx)
if ok {
Expand All @@ -43,7 +43,7 @@ func (d *ExampleDirector) Connect(ctx context.Context, method string) (context.C
return ctx, conn, err
}
}
return nil, nil, grpc.Errorf(codes.Unimplemented, "Unknown method")
return nil, nil, status.Errorf(codes.Unimplemented, "Unknown method")
}
```
The direct is registered with a `grpc.Server`, along with a special codec which
Expand Down
18 changes: 9 additions & 9 deletions proxy/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package proxy
import (
"fmt"

"google.golang.org/grpc/encoding"
"google.golang.org/protobuf/proto"
"google.golang.org/grpc"
)

// Codec returns a proxying grpc.Codec with the default protobuf codec as parent.
//
// See CodecWithParent.
func Codec() grpc.Codec {
func Codec() encoding.Codec {
return CodecWithParent(&protoCodec{})
}

Expand All @@ -20,15 +20,15 @@ func Codec() grpc.Codec {
// to the schema of the forwarded messages. It basically treats a gRPC message frame as raw bytes.
// However, if the server handler, or the client caller are not proxy-internal functions it will fall back
// to trying to decode the message using a fallback codec.
func CodecWithParent(fallback grpc.Codec) grpc.Codec {
func CodecWithParent(fallback encoding.Codec) encoding.Codec {
return &rawCodec{fallback}
}

type rawCodec struct {
parentCodec grpc.Codec
parentCodec encoding.Codec
}

var _ grpc.Codec = &rawCodec{}
var _ encoding.Codec = &rawCodec{}

func (c *rawCodec) Marshal(v interface{}) ([]byte, error) {
out, ok := v.(*[]byte)
Expand All @@ -48,14 +48,14 @@ func (c *rawCodec) Unmarshal(data []byte, v interface{}) error {
return nil
}

func (c *rawCodec) String() string {
return fmt.Sprintf("proxy>%s", c.parentCodec.String())
func (c *rawCodec) Name() string {
return fmt.Sprintf("proxy>%s", c.parentCodec.Name())
}

// protoCodec is a Codec implementation with protobuf. It is the default rawCodec for gRPC.
type protoCodec struct{}

var _ grpc.Codec = &protoCodec{}
var _ encoding.Codec = &protoCodec{}

func (protoCodec) Marshal(v interface{}) ([]byte, error) {
return proto.Marshal(v.(proto.Message))
Expand All @@ -65,6 +65,6 @@ func (protoCodec) Unmarshal(data []byte, v interface{}) error {
return proto.Unmarshal(data, v.(proto.Message))
}

func (protoCodec) String() string {
func (protoCodec) Name() string {
return "proto"
}
13 changes: 11 additions & 2 deletions proxy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// biDirCopy connects an incoming ServerStream with an outgoing ClientStream.
Expand Down Expand Up @@ -37,7 +38,7 @@ func forwardOut(in grpc.ServerStream, out grpc.ClientStream) error {
case nil:
return err2
default:
return grpc.Errorf(codes.Internal, "failed proxying s2c: %s", err)
return status.Errorf(codes.Internal, "failed proxying s2c: %s", err)
}
}

Expand All @@ -58,7 +59,15 @@ func forwardIn(in grpc.ServerStream, out grpc.ClientStream) error {
return err
}

func copyStream(src grpc.Stream, dst grpc.Stream) error {
type SourceStream interface {
RecvMsg(m any) error
}

type DestStream interface {
SendMsg(m any) error
}

func copyStream(src SourceStream, dst DestStream) error {
var f []byte
for {
if err := src.RecvMsg(&f); err != nil {
Expand Down
15 changes: 9 additions & 6 deletions proxy/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

var (
Expand All @@ -21,7 +22,7 @@ var (

func ExampleRegisterService() {
// A gRPC server with the proxying codec enabled.
server := grpc.NewServer(grpc.CustomCodec(proxy.Codec()))
server := grpc.NewServer(grpc.ForceServerCodec(proxy.Codec()))
// Register a TestService with 4 of its methods explicitly.
proxy.RegisterService(server, director,
"vgough.testproto.TestService",
Expand All @@ -30,7 +31,7 @@ func ExampleRegisterService() {

func ExampleTransparentHandler() {
grpc.NewServer(
grpc.CustomCodec(proxy.Codec()),
grpc.ForceServerCodec(proxy.Codec()),
grpc.UnknownServiceHandler(proxy.TransparentHandler(director)))
}

Expand All @@ -42,21 +43,23 @@ type ExampleDirector struct {
func (d *ExampleDirector) Connect(ctx context.Context, method string) (context.Context, *grpc.ClientConn, error) {
// Make sure we never forward internal services.
if strings.HasPrefix(method, "/com.example.internal.") {
return nil, nil, grpc.Errorf(codes.Unimplemented, "Unknown method")
return nil, nil, status.Errorf(codes.Unimplemented, "Unknown method")
}
md, ok := metadata.FromIncomingContext(ctx)
if ok {
// Decide on which backend to dial
if val, exists := md[":authority"]; exists && val[0] == "staging.api.example.com" {
// Make sure we use DialContext so the dialing can be cancelled/time out together with the context.
conn, err := grpc.DialContext(ctx, "api-service.staging.svc.local", grpc.WithCodec(proxy.Codec()))
conn, err := grpc.DialContext(ctx, "api-service.staging.svc.local",
grpc.WithDefaultCallOptions(grpc.ForceCodec(proxy.Codec())))
return ctx, conn, err
} else if val, exists := md[":authority"]; exists && val[0] == "api.example.com" {
conn, err := grpc.DialContext(ctx, "api-service.prod.svc.local", grpc.WithCodec(proxy.Codec()))
conn, err := grpc.DialContext(ctx, "api-service.prod.svc.local",
grpc.WithDefaultCallOptions(grpc.ForceCodec(proxy.Codec())))
return ctx, conn, err
}
}
return nil, nil, grpc.Errorf(codes.Unimplemented, "Unknown method")
return nil, nil, status.Errorf(codes.Unimplemented, "Unknown method")
}

func (d *ExampleDirector) Release(ctx context.Context, conn *grpc.ClientConn) {
Expand Down
Loading

0 comments on commit 12c253e

Please sign in to comment.