Skip to content

Commit b4fb51e

Browse files
authored
Fix/concurrent write conn (#61)
* fix: barry quick commit, 2025-02-11 10:28:02 * fix: barry quick fix, 2025-03-18 19:20:07 * feat(grpcs): add metadata handlers for gRPC gateway * feat(grpcs): add context middleware to http server
1 parent 482debd commit b4fb51e

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

lava/middleware.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package lava
22

3-
import "context"
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"google.golang.org/grpc/metadata"
8+
)
9+
10+
type GrpcGatewayMetadata func(ctx context.Context, req *http.Request, rpcPath string, httpPattern string) metadata.MD
411

512
type HandlerFunc func(ctx context.Context, req Request) (Response, error)
613

servers/grpcs/server.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func (s *serviceImpl) DixInject(
9292
log log.Logger,
9393
conf *Config,
9494
gw []*gateway.Mux,
95+
metadataHandlers []lava.GrpcGatewayMetadata,
9596
) {
9697
s.conf = conf
9798
if conf.HttpPort == nil {
@@ -165,6 +166,11 @@ func (s *serviceImpl) DixInject(
165166
}))
166167
}
167168

169+
httpServer.Use(func(ctx *fiber.Ctx) error {
170+
ctx.SetUserContext(ctx.Context())
171+
return ctx.Next()
172+
})
173+
168174
app := fiber.New()
169175
app.Group("/debug", httputil.StripPrefix(filepath.Join(conf.BaseUrl, "/debug"), debug.Handler))
170176

@@ -239,11 +245,17 @@ func (s *serviceImpl) DixInject(
239245
return strings.ToUpper(s), true
240246
}),
241247
runtime.WithMetadata(func(ctx context.Context, request *http.Request) metadata.MD {
242-
path, ok := runtime.HTTPPathPattern(ctx)
243-
if !ok {
244-
return nil
248+
rpcPath, _ := runtime.RPCMethod(ctx)
249+
path, _ := runtime.HTTPPathPattern(ctx)
250+
251+
md := metadata.Pairs("http_path", path, "http_method", request.Method, "http_url", request.URL.Path)
252+
for _, h := range metadataHandlers {
253+
for k, v := range h(ctx, request, rpcPath, path) {
254+
md.Append(k, v...)
255+
}
245256
}
246-
return metadata.Pairs("http_path", path, "http_method", request.Method, "http_url", request.URL.Path)
257+
258+
return md
247259
}),
248260
runtime.WithErrorHandler(func(ctx context.Context, mux *runtime.ServeMux, marshal runtime.Marshaler, w http.ResponseWriter, request *http.Request, err error) {
249261
md, ok := runtime.ServerMetadataFromContext(ctx)

servers/https/server.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/pubgo/funk/stack"
2020
"github.com/pubgo/funk/version"
2121
"github.com/pubgo/opendoc/opendoc"
22-
"github.com/valyala/fasthttp"
2322
"google.golang.org/grpc/codes"
2423

2524
"github.com/pubgo/lava/core/debug"
@@ -51,8 +50,6 @@ func (s *serviceImpl) Run() {
5150
defer s.stop()
5251
s.start()
5352
signal.Wait()
54-
55-
fasthttp.AcquireArgs()
5653
}
5754

5855
func (s *serviceImpl) Start() { s.start() }

0 commit comments

Comments
 (0)