Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mybee
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
mybee
Commits
e5f141cf
Unverified
Commit
e5f141cf
authored
Mar 12, 2021
by
acud
Committed by
GitHub
Mar 12, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pushsync: improve context handling (#1416)
parent
f63c396f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
20 deletions
+16
-20
pushsync.go
pkg/pushsync/pushsync.go
+16
-20
No files found.
pkg/pushsync/pushsync.go
View file @
e5f141cf
...
...
@@ -58,7 +58,7 @@ type PushSync struct {
tracer
*
tracing
.
Tracer
}
var
timeTo
WaitForReceipt
=
3
*
time
.
Second
// time to wait to get a receipt for a chunk
var
timeTo
Live
=
5
*
time
.
Second
// request time to live
func
New
(
streamer
p2p
.
StreamerDisconnecter
,
storer
storage
.
Putter
,
closestPeerer
topology
.
ClosestPeerer
,
tagger
*
tags
.
Tags
,
unwrap
func
(
swarm
.
Chunk
),
logger
logging
.
Logger
,
accounting
accounting
.
Interface
,
pricer
accounting
.
Pricer
,
tracer
*
tracing
.
Tracer
)
*
PushSync
{
ps
:=
&
PushSync
{
...
...
@@ -93,6 +93,8 @@ func (s *PushSync) Protocol() p2p.ProtocolSpec {
// If the current node is the destination, it stores in the local store and sends a receipt.
func
(
ps
*
PushSync
)
handler
(
ctx
context
.
Context
,
p
p2p
.
Peer
,
stream
p2p
.
Stream
)
(
err
error
)
{
w
,
r
:=
protobuf
.
NewWriterAndReader
(
stream
)
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
timeToLive
)
defer
cancel
()
defer
func
()
{
if
err
!=
nil
{
ps
.
metrics
.
TotalErrors
.
Inc
()
...
...
@@ -123,15 +125,13 @@ func (ps *PushSync) handler(ctx context.Context, p p2p.Peer, stream p2p.Stream)
receipt
,
err
:=
ps
.
pushToClosest
(
ctx
,
chunk
)
if
err
!=
nil
{
if
errors
.
Is
(
err
,
topology
.
ErrWantSelf
)
{
// store the chunk in the local store
_
,
err
=
ps
.
storer
.
Put
(
ctx
,
storage
.
ModePutSync
,
chunk
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"chunk store: %w"
,
err
)
}
receipt
:=
pb
.
Receipt
{
Address
:
chunk
.
Address
()
.
Bytes
()}
if
err
:=
w
.
WriteMsg
(
&
receipt
);
err
!=
nil
{
if
err
:=
w
.
WriteMsg
WithContext
(
ctx
,
&
receipt
);
err
!=
nil
{
return
fmt
.
Errorf
(
"send receipt to peer %s: %w"
,
p
.
Address
.
String
(),
err
)
}
...
...
@@ -140,9 +140,7 @@ func (ps *PushSync) handler(ctx context.Context, p p2p.Peer, stream p2p.Stream)
return
fmt
.
Errorf
(
"handler: push to closest: %w"
,
err
)
}
// pass back the received receipt in the previously received stream
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
timeToWaitForReceipt
)
defer
cancel
()
// pass back the receipt
if
err
:=
w
.
WriteMsgWithContext
(
ctx
,
receipt
);
err
!=
nil
{
return
fmt
.
Errorf
(
"send receipt to peer %s: %w"
,
p
.
Address
.
String
(),
err
)
}
...
...
@@ -189,13 +187,6 @@ func (ps *PushSync) pushToClosest(ctx context.Context, ch swarm.Chunk) (rr *pb.R
defersFn
()
deferFuncs
=
append
(
deferFuncs
,
func
()
{
if
lastErr
!=
nil
{
ps
.
metrics
.
TotalErrors
.
Inc
()
logger
.
Errorf
(
"pushsync: %v"
,
lastErr
)
}
})
// find next closest peer
peer
,
err
:=
ps
.
peerSuggester
.
ClosestPeer
(
ch
.
Address
(),
skipPeers
...
)
if
err
!=
nil
{
...
...
@@ -208,6 +199,13 @@ func (ps *PushSync) pushToClosest(ctx context.Context, ch swarm.Chunk) (rr *pb.R
// save found peer (to be skipped if there is some error with him)
skipPeers
=
append
(
skipPeers
,
peer
)
deferFuncs
=
append
(
deferFuncs
,
func
()
{
if
lastErr
!=
nil
{
ps
.
metrics
.
TotalErrors
.
Inc
()
logger
.
Errorf
(
"pushsync: %v"
,
lastErr
)
}
})
// compute the price we pay for this receipt and reserve it for the rest of this function
receiptPrice
:=
ps
.
pricer
.
PeerPrice
(
peer
,
ch
.
Address
())
err
=
ps
.
accounting
.
Reserve
(
ctx
,
peer
,
receiptPrice
)
...
...
@@ -224,9 +222,9 @@ func (ps *PushSync) pushToClosest(ctx context.Context, ch swarm.Chunk) (rr *pb.R
deferFuncs
=
append
(
deferFuncs
,
func
()
{
go
streamer
.
FullClose
()
})
w
,
r
:=
protobuf
.
NewWriterAndReader
(
streamer
)
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
timeToWaitForReceipt
)
defer
cancel
(
)
if
err
:=
w
.
WriteMsgWithContext
(
ctx
,
&
pb
.
Delivery
{
ctx
d
,
canceld
:=
context
.
WithTimeout
(
ctx
,
timeToLive
)
defer
Funcs
=
append
(
deferFuncs
,
func
()
{
canceld
()
}
)
if
err
:=
w
.
WriteMsgWithContext
(
ctx
d
,
&
pb
.
Delivery
{
Address
:
ch
.
Address
()
.
Bytes
(),
Data
:
ch
.
Data
(),
});
err
!=
nil
{
...
...
@@ -249,9 +247,7 @@ func (ps *PushSync) pushToClosest(ctx context.Context, ch swarm.Chunk) (rr *pb.R
}
var
receipt
pb
.
Receipt
cctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
timeToWaitForReceipt
)
defer
cancel
()
if
err
:=
r
.
ReadMsgWithContext
(
cctx
,
&
receipt
);
err
!=
nil
{
if
err
:=
r
.
ReadMsgWithContext
(
ctxd
,
&
receipt
);
err
!=
nil
{
_
=
streamer
.
Reset
()
lastErr
=
fmt
.
Errorf
(
"chunk %s receive receipt from peer %s: %w"
,
ch
.
Address
()
.
String
(),
peer
.
String
(),
err
)
continue
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment