Skip to content

Commit

Permalink
fix conneciton hang in http server
Browse files Browse the repository at this point in the history
  • Loading branch information
slugalisk committed Jul 18, 2022
1 parent 42b5c18 commit d306d06
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@memelabs/protobuf",
"version": "0.3.3",
"version": "0.3.4",
"description": "",
"license": "MIT",
"repository": {
Expand Down
18 changes: 14 additions & 4 deletions pkg/rpc/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package rpc

import (
"context"
"io/ioutil"
"encoding/binary"
"io"
"net/http"
"strconv"

pb "github.com/MemeLabs/protobuf/pkg/apis/rpc"
"github.com/MemeLabs/protobuf/pkg/bytereader"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
Expand All @@ -24,24 +26,32 @@ type HTTPServer struct {
}

func (s *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
l, err := binary.ReadUvarint(bytereader.New(r.Body))
if err != nil {
httpServeError(http.StatusBadRequest, err, w)
return
}

b := make([]byte, l)
if _, err := io.ReadAtLeast(r.Body, b, int(l)); err != nil {
httpServeError(http.StatusBadRequest, err, w)
return
}

req := &pb.Call{}
if err := proto.Unmarshal(b, req); err != nil {
httpServeError(http.StatusBadRequest, err, w)
return
}

ctx, cancel := context.WithCancel(r.Context())
send := func(_ context.Context, res *pb.Call) error {
return httpServeProto(res, w)
}
call := NewCallIn(r.Context(), req, noopParentCallAccessor{}, send)
call := NewCallIn(ctx, req, noopParentCallAccessor{}, send)

s.ServiceDispatcher.Dispatch(call, func() {})
s.ServiceDispatcher.Dispatch(call, cancel)
<-ctx.Done()
}

func httpServeError(statusCode int, err error, w http.ResponseWriter) error {
Expand Down
1 change: 1 addition & 0 deletions pkg/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (h *ServiceDispatcher) call(call *CallIn, done func()) {
method, ok := h.methods[call.Method()]
if !ok {
call.returnError(fmt.Errorf("method not found: %s", call.Method()))
done()
return
}

Expand Down

0 comments on commit d306d06

Please sign in to comment.