Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Nov 20, 2024
1 parent f0ee92e commit 5804fa6
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 252 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ Percentage of the requests served within a certain time (ms)
100% 102 (longest request)
```

## Call Graph

![callgraph](./assets/callgraph.png)

## Pyroscope

Visiting [http://localhost:4040](http://localhost:4040) will show the Pyroscope dashboard.
Expand Down
Binary file added assets/callgraph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions internal/server/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ type server struct {
profiler *pyroscope.Profiler

config kod.Ref[config.Config]
_ kod.Ref[Caller]
queryer kod.Ref[Queryer]
_ kod.Ref[GraphqlCaller]
queryer kod.Ref[GraphqlQueryer]
registry kod.Ref[CallerRegistry]
httpUpstream kod.Ref[Upstream]
httpUpstream kod.Ref[HttpUpstream]
}

func (ins *server) Init(ctx context.Context) error {
Expand Down
10 changes: 5 additions & 5 deletions internal/server/graphql_caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

type caller struct {
kod.Implements[Caller]
type graphqlCaller struct {
kod.Implements[GraphqlCaller]

config kod.Ref[config.Config]
registry kod.Ref[CallerRegistry]

singleflight singleflight.Group
}

func (c *caller) Init(ctx context.Context) error {
func (c *graphqlCaller) Init(ctx context.Context) error {
return nil
}

func (c *caller) Call(ctx context.Context, rpc protoreflect.MethodDescriptor, message proto.Message) (proto.Message, error) {
func (c *graphqlCaller) Call(ctx context.Context, rpc protoreflect.MethodDescriptor, message proto.Message) (proto.Message, error) {
if c.config.Get().Config().Server.GraphQL.SingleFlight {
if enable, ok := ctx.Value(allowSingleFlightKey).(bool); ok && enable {
hash := Hash64.Get()
Expand Down Expand Up @@ -86,7 +86,7 @@ func (c *caller) Call(ctx context.Context, rpc protoreflect.MethodDescriptor, me
return res, err
}

func (c *caller) Interceptors() []interceptor.Interceptor {
func (c *graphqlCaller) Interceptors() []interceptor.Interceptor {
if c.config.Get().Config().Engine.CircuitBreaker {
return []interceptor.Interceptor{
kcircuitbreaker.Interceptor(),
Expand Down
2 changes: 1 addition & 1 deletion internal/server/graphql_caller_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
type callerRegistry struct {
kod.Implements[CallerRegistry]
config kod.Ref[config.Config]
reflection kod.Ref[Reflection]
reflection kod.Ref[GraphqlReflection]

serviceStub map[string]*grpcdynamic.Stub
schema *protographql.SchemaDescriptor
Expand Down
6 changes: 3 additions & 3 deletions internal/server/graphql_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func isReflectionServiceName(name string) bool {

var ErrTLSHandshakeFailed = errors.New("TLS handshake failed")

type reflection struct {
kod.Implements[Reflection]
type graphqlReflection struct {
kod.Implements[GraphqlReflection]
}

func (ins *reflection) ListPackages(ctx context.Context, cc grpc.ClientConnInterface) ([]protoreflect.FileDescriptor, error) {
func (ins *graphqlReflection) ListPackages(ctx context.Context, cc grpc.ClientConnInterface) ([]protoreflect.FileDescriptor, error) {
client := grpcreflect.NewClientAuto(ctx, cc)
ssvcs, err := client.ListServices()
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions internal/server/graphql_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (

type anyMap = map[string]interface{}

type queryer struct {
kod.Implements[Queryer]
type graphqlQueryer struct {
kod.Implements[GraphqlQueryer]

config kod.Ref[config.Config]
caller kod.Ref[Caller]
caller kod.Ref[GraphqlCaller]
registry kod.Ref[CallerRegistry]
}

func (q *queryer) Interceptors() []interceptor.Interceptor {
func (q *graphqlQueryer) Interceptors() []interceptor.Interceptor {
if q.config.Get().Config().Engine.RateLimit {
return []interceptor.Interceptor{
kratelimit.Interceptor(),
Expand All @@ -34,7 +34,7 @@ func (q *queryer) Interceptors() []interceptor.Interceptor {
return nil
}

func (q *queryer) Query(ctx context.Context, input *graphql.QueryInput, result interface{}) error {
func (q *graphqlQueryer) Query(ctx context.Context, input *graphql.QueryInput, result interface{}) error {
res := map[string]interface{}{}
var err error
var selection ast.SelectionSet
Expand Down Expand Up @@ -78,7 +78,7 @@ func (q *queryer) Query(ctx context.Context, input *graphql.QueryInput, result i
return nil
}

func (q *queryer) resolveMutation(ctx context.Context, selection ast.SelectionSet, res anyMap, vars map[string]interface{}) (err error) {
func (q *graphqlQueryer) resolveMutation(ctx context.Context, selection ast.SelectionSet, res anyMap, vars map[string]interface{}) (err error) {
for _, ss := range selection {
field, ok := ss.(*ast.Field)
if !ok {
Expand All @@ -96,7 +96,7 @@ func (q *queryer) resolveMutation(ctx context.Context, selection ast.SelectionSe
return
}

func (q *queryer) resolveQuery(ctx context.Context, selection ast.SelectionSet, res anyMap, vars map[string]interface{}) (err error) {
func (q *graphqlQueryer) resolveQuery(ctx context.Context, selection ast.SelectionSet, res anyMap, vars map[string]interface{}) (err error) {
type mapEntry struct {
key string
val interface{}
Expand Down Expand Up @@ -144,7 +144,7 @@ func (q *queryer) resolveQuery(ctx context.Context, selection ast.SelectionSet,
return
}

func (q *queryer) resolveCall(ctx context.Context, op ast.Operation, field *ast.Field, vars map[string]interface{}) (interface{}, error) {
func (q *graphqlQueryer) resolveCall(ctx context.Context, op ast.Operation, field *ast.Field, vars map[string]interface{}) (interface{}, error) {
method := q.registry.Get().FindMethodByName(op, field.Name)
if method == nil {
return nil, errors.New("method not found")
Expand Down
12 changes: 6 additions & 6 deletions internal/server/http_upstream_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import (
"github.com/sysulq/graphql-grpc-gateway/pkg/protojson"
)

type invoker struct {
kod.Implements[Invoker]
type httpUpstreamInvoker struct {
kod.Implements[HttpUpstreamInvoker]
}

func (i *invoker) Invoke(ctx context.Context, rw http.ResponseWriter, r *http.Request, upstream upstreamInfo, rpcPath string, pathNames []string) {
func (i *httpUpstreamInvoker) Invoke(ctx context.Context, rw http.ResponseWriter, r *http.Request, upstream upstreamInfo, rpcPath string, pathNames []string) {
parser, err := protojson.NewRequestParser(r, pathNames, upstream.resovler)
if err != nil {
i.L(ctx).Error("parse request", "error", err)
rw.WriteHeader(http.StatusBadRequest)
rw.Write([]byte(err.Error()))
_, _ = rw.Write([]byte(err.Error()))
return
}

Expand All @@ -36,13 +36,13 @@ func (i *invoker) Invoke(ctx context.Context, rw http.ResponseWriter, r *http.Re
i.L(ctx).Error("invoke rpc", "error", err)
if handler.Status == nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(err.Error()))
_, _ = rw.Write([]byte(err.Error()))
return
}
}
}

func (invoker) Interceptors() []interceptor.Interceptor {
func (httpUpstreamInvoker) Interceptors() []interceptor.Interceptor {
return []interceptor.Interceptor{
kaccesslog.Interceptor(),
kmetric.Interceptor(),
Expand Down
12 changes: 6 additions & 6 deletions internal/server/http_uptream.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"google.golang.org/grpc"
)

type upstream struct {
kod.Implements[Upstream]
type httpUpstream struct {
kod.Implements[HttpUpstream]

invoker kod.Ref[Invoker]
invoker kod.Ref[HttpUpstreamInvoker]
config kod.Ref[config.Config]

upstreams map[string]upstreamInfo
Expand All @@ -36,7 +36,7 @@ type upstreamInfo struct {
methods []protojson.Method
}

func (u *upstream) Init(ctx context.Context) error {
func (u *httpUpstream) Init(ctx context.Context) error {
u.upstreams = make(map[string]upstreamInfo)

lo.ForEach(u.config.Get().Config().Grpc.Services, func(item kgrpc.Config, index int) {
Expand Down Expand Up @@ -65,7 +65,7 @@ func (u *upstream) Init(ctx context.Context) error {
return nil
}

func (u *upstream) Register(ctx context.Context, router *http.ServeMux) {
func (u *httpUpstream) Register(ctx context.Context, router *http.ServeMux) {
for _, upstream := range u.upstreams {
for _, v := range upstream.methods {
if v.HttpPath == "" {
Expand All @@ -78,7 +78,7 @@ func (u *upstream) Register(ctx context.Context, router *http.ServeMux) {
}
}

func (u *upstream) buildHandler(_ context.Context, upstream upstreamInfo, rpcPath string, pathNames []string) http.HandlerFunc {
func (u *httpUpstream) buildHandler(_ context.Context, upstream upstreamInfo, rpcPath string, pathNames []string) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
u.invoker.Get().Invoke(r.Context(), rw, r, upstream, rpcPath, pathNames)
}
Expand Down
Loading

0 comments on commit 5804fa6

Please sign in to comment.