This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
p2p/protocols, p2p/testing; conditional propagagation of context
- Loading branch information
Showing
4 changed files
with
134 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package protocols | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"context" | ||
"io/ioutil" | ||
|
||
"github.com/ethereum/go-ethereum/p2p" | ||
"github.com/ethereum/go-ethereum/rlp" | ||
"github.com/ethersphere/swarm/spancontext" | ||
opentracing "github.com/opentracing/opentracing-go" | ||
) | ||
|
||
// msgWithContext is used to propagate marshalled context alongside message payloads | ||
type msgWithContext struct { | ||
Context []byte | ||
Msg []byte | ||
} | ||
|
||
func encodeWithContext(ctx context.Context, msg interface{}) (interface{}, int, error) { | ||
var b bytes.Buffer | ||
writer := bufio.NewWriter(&b) | ||
tracer := opentracing.GlobalTracer() | ||
sctx := spancontext.FromContext(ctx) | ||
if sctx != nil { | ||
err := tracer.Inject( | ||
sctx, | ||
opentracing.Binary, | ||
writer) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
} | ||
writer.Flush() | ||
msgBytes, err := rlp.EncodeToBytes(msg) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
return &msgWithContext{ | ||
Context: b.Bytes(), | ||
Msg: msgBytes, | ||
}, len(msgBytes), nil | ||
} | ||
|
||
func decodeWithContext(msg p2p.Msg) (context.Context, []byte, error) { | ||
var wmsg msgWithContext | ||
err := msg.Decode(&wmsg) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
if len(wmsg.Context) == 0 { | ||
return ctx, wmsg.Msg, nil | ||
} | ||
|
||
tracer := opentracing.GlobalTracer() | ||
sctx, err := tracer.Extract(opentracing.Binary, bytes.NewReader(wmsg.Context)) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
ctx = spancontext.WithContext(ctx, sctx) | ||
return ctx, wmsg.Msg, nil | ||
} | ||
|
||
func encodeWithoutContext(ctx context.Context, msg interface{}) (interface{}, int, error) { | ||
return msg, 0, nil | ||
} | ||
|
||
func decodeWithoutContext(msg p2p.Msg) (context.Context, []byte, error) { | ||
b, err := ioutil.ReadAll(msg.Payload) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return context.Background(), b, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.