Skip to content

Commit

Permalink
Merge pull request ethereum#60 from binance-chain/x-forward-for
Browse files Browse the repository at this point in the history
[R4R] Add `x-forward-for` log message when handle message failed
  • Loading branch information
unclezoro authored Jan 11, 2021
2 parents cfeab77 + 0e797c5 commit 5a1c39d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,9 @@ func (c *Client) dispatch(codec ServerCodec) {
// Read path:
case op := <-c.readOp:
if op.batch {
conn.handler.handleBatch(op.msgs)
conn.handler.handleBatch(context.Background(), op.msgs)
} else {
conn.handler.handleMsg(op.msgs[0])
conn.handler.handleMsg(context.Background(), op.msgs[0])
}

case err := <-c.readErr:
Expand Down
13 changes: 7 additions & 6 deletions rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func newHandler(connCtx context.Context, conn jsonWriter, idgen func() ID, reg *
}

// handleBatch executes all messages in a batch and returns the responses.
func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
func (h *handler) handleBatch(ctx context.Context, msgs []*jsonrpcMessage) {
// Emit error response for empty batches:
if len(msgs) == 0 {
h.startCallProc(func(cp *callProc) {
Expand All @@ -116,7 +116,7 @@ func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
h.startCallProc(func(cp *callProc) {
answers := make([]*jsonrpcMessage, 0, len(msgs))
for _, msg := range calls {
if answer := h.handleCallMsg(cp, msg); answer != nil {
if answer := h.handleCallMsg(cp, ctx, msg); answer != nil {
answers = append(answers, answer)
}
}
Expand All @@ -131,12 +131,12 @@ func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
}

// handleMsg handles a single message.
func (h *handler) handleMsg(msg *jsonrpcMessage) {
func (h *handler) handleMsg(ctx context.Context, msg *jsonrpcMessage) {
if ok := h.handleImmediate(msg); ok {
return
}
h.startCallProc(func(cp *callProc) {
answer := h.handleCallMsg(cp, msg)
answer := h.handleCallMsg(cp, ctx, msg)
h.addSubscriptions(cp.notifiers)
if answer != nil {
h.conn.writeJSON(cp.ctx, answer)
Expand Down Expand Up @@ -287,7 +287,7 @@ func (h *handler) handleResponse(msg *jsonrpcMessage) {
}

// handleCallMsg executes a call message and returns the answer.
func (h *handler) handleCallMsg(ctx *callProc, msg *jsonrpcMessage) *jsonrpcMessage {
func (h *handler) handleCallMsg(ctx *callProc, reqCtx context.Context, msg *jsonrpcMessage) *jsonrpcMessage {
start := time.Now()
switch {
case msg.isNotification():
Expand All @@ -297,7 +297,8 @@ func (h *handler) handleCallMsg(ctx *callProc, msg *jsonrpcMessage) *jsonrpcMess
case msg.isCall():
resp := h.handleCall(ctx, msg)
if resp.Error != nil {
h.log.Warn("Served "+msg.Method, "reqid", idForLog{msg.ID}, "t", time.Since(start), "err", resp.Error.Message)
xForward := reqCtx.Value("X-Forwarded-For")
h.log.Warn("Served "+msg.Method, "reqid", idForLog{msg.ID}, "t", time.Since(start), "err", resp.Error.Message, "X-Forwarded-For", xForward)
} else {
h.log.Debug("Served "+msg.Method, "reqid", idForLog{msg.ID}, "t", time.Since(start))
}
Expand Down
3 changes: 3 additions & 0 deletions rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
ctx = context.WithValue(ctx, "Origin", origin)
}
if xForward := r.Header.Get("X-Forwarded-For"); xForward != "" {
ctx = context.WithValue(ctx, "X-Forwarded-For", xForward)
}

w.Header().Set("content-type", contentType)
codec := newHTTPServerConn(r, w)
Expand Down
4 changes: 2 additions & 2 deletions rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
return
}
if batch {
h.handleBatch(reqs)
h.handleBatch(ctx, reqs)
} else {
h.handleMsg(reqs[0])
h.handleMsg(ctx, reqs[0])
}
}

Expand Down

0 comments on commit 5a1c39d

Please sign in to comment.