Skip to content

Commit

Permalink
Addition of custom http grpc handlers to support reverse proxy by Ori…
Browse files Browse the repository at this point in the history
…on (#170)

* add code handlers

* add customCodec and unknown service handler to grpc handler

* add custom http handler

* add grpc server options only if they are not nil

* using encoding.codec as grpc.codec is deprecated

* removing custom codec as Orion server option as it was not necessary

* handlers name refactoring

* add handlers in orion config

* adding handlers via options

* passing pointer of notfoundhandler

* removing http handler option

* adding server options in GetDefaultServer

* Apply suggestions from code review

Co-authored-by: Benjamin Heng <[email protected]>

Co-authored-by: Ganesh <[email protected]>
Co-authored-by: Benjamin Heng <[email protected]>
Co-authored-by: Achi Chen <[email protected]>
  • Loading branch information
4 people authored Jul 22, 2022
1 parent fae579f commit d30c4e5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
1 change: 0 additions & 1 deletion orion/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"

"github.com/afex/hystrix-go/hystrix"
"github.com/spf13/viper"

Expand Down
46 changes: 40 additions & 6 deletions orion/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ type middlewareInfo struct {

//DefaultServerImpl provides a default implementation of orion.Server this can be embedded in custom orion.Server implementations
type DefaultServerImpl struct {
config Config
mu sync.Mutex
wg sync.WaitGroup
inited bool
config Config
mu sync.Mutex
wg sync.WaitGroup
grpcUnknownServiceHandler grpc.StreamHandler
inited bool

services map[string]*svcInfo
encoders map[string]*encoderInfo
Expand Down Expand Up @@ -263,6 +264,7 @@ func (d *DefaultServerImpl) buildHandlers() []*handlerInfo {
CommonConfig: handlers.CommonConfig{
DisableDefaultInterceptors: d.config.DisableDefaultInterceptors,
},
UnknownServiceHandler: d.grpcUnknownServiceHandler,
}
handler := grpcHandler.NewGRPCHandler(config)
hlrs = append(hlrs, &handlerInfo{
Expand Down Expand Up @@ -498,8 +500,14 @@ func (d *DefaultServerImpl) Stop(timeout time.Duration) error {
}

//GetDefaultServer returns a default server object that can be directly used to start orion server
func GetDefaultServer(name string) Server {
return GetDefaultServerWithConfig(BuildDefaultConfig(name))
func GetDefaultServer(name string, opts ...DefaultServerOption) Server {
server := &DefaultServerImpl{
config: BuildDefaultConfig(name),
}
for _, opt := range opts {
opt.apply(server)
}
return server
}

//GetDefaultServerWithConfig returns a default server object that uses provided configuration
Expand All @@ -508,3 +516,29 @@ func GetDefaultServerWithConfig(config Config) Server {
config: config,
}
}

type DefaultServerOption interface {
apply(*DefaultServerImpl)
}

// WithGrpcUnknownHandler returns a DefaultServerOption which sets
// UnknownServiceHandler option in grpc server
func WithGrpcUnknownHandler(grpcUnknownServiceHandler grpc.StreamHandler) DefaultServerOption {
return newFuncDefaultServerOption(func(h *DefaultServerImpl) {
h.grpcUnknownServiceHandler = grpcUnknownServiceHandler
})
}

type funcDefaultServerOption struct {
f func(options *DefaultServerImpl)
}

func (fdso *funcDefaultServerOption) apply(ds *DefaultServerImpl) {
fdso.f(ds)
}

func newFuncDefaultServerOption(f func(options *DefaultServerImpl)) *funcDefaultServerOption {
return &funcDefaultServerOption{
f: f,
}
}
17 changes: 13 additions & 4 deletions orion/handlers/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"github.com/carousell/Orion/utils/log"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"google.golang.org/grpc"

)

// Config is the configuration for GRPC Handler
type Config struct {
handlers.CommonConfig
UnknownServiceHandler grpc.StreamHandler
}

//NewGRPCHandler creates a new GRPC handler
Expand All @@ -31,10 +33,17 @@ type grpcHandler struct {

func (g *grpcHandler) init() {
if g.grpcServer == nil {
g.grpcServer = grpc.NewServer(
grpc.UnaryInterceptor(g.grpcInterceptor()),
grpc.StreamInterceptor(g.grpcStreamInterceptor()),
)
var opts []grpc.ServerOption
if g.grpcInterceptor() != nil {
opts = append(opts, grpc.UnaryInterceptor(g.grpcInterceptor()))
}
if g.grpcStreamInterceptor() != nil {
opts = append(opts, grpc.StreamInterceptor(g.grpcStreamInterceptor()))
}
if g.config.UnknownServiceHandler != nil {
opts = append(opts, grpc.UnknownServiceHandler(g.config.UnknownServiceHandler))
}
g.grpcServer = grpc.NewServer(opts...)
}
if g.middlewares == nil {
g.middlewares = handlers.NewMiddlewareMapping()
Expand Down

0 comments on commit d30c4e5

Please sign in to comment.