remove sendCancelable
This commit is contained in:
parent
ab9b6f27e5
commit
531933761a
5 changed files with 6 additions and 20 deletions
|
@ -132,14 +132,13 @@ func (b *BootstrapNode) ping(c Contact) {
|
||||||
b.stop.Add(1)
|
b.stop.Add(1)
|
||||||
defer b.stop.Done()
|
defer b.stop.Done()
|
||||||
|
|
||||||
resCh, cancel := b.SendCancelable(c, Request{Method: pingMethod})
|
resCh := b.SendAsync(c, Request{Method: pingMethod})
|
||||||
|
|
||||||
var res *Response
|
var res *Response
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case res = <-resCh:
|
case res = <-resCh:
|
||||||
case <-b.stop.Ch():
|
case <-b.stop.Ch():
|
||||||
cancel()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -318,7 +318,7 @@ func (dht *DHT) storeOnNode(hash bits.Bitmap, c Contact) {
|
||||||
|
|
||||||
token := dht.tokenCache.Get(c, hash, dht.stop.Ch())
|
token := dht.tokenCache.Get(c, hash, dht.stop.Ch())
|
||||||
|
|
||||||
resCh, cancel := dht.node.SendCancelable(c, Request{
|
resCh := dht.node.SendAsync(c, Request{
|
||||||
Method: storeMethod,
|
Method: storeMethod,
|
||||||
StoreArgs: &storeArgs{
|
StoreArgs: &storeArgs{
|
||||||
BlobHash: hash,
|
BlobHash: hash,
|
||||||
|
@ -334,7 +334,6 @@ func (dht *DHT) storeOnNode(hash bits.Bitmap, c Contact) {
|
||||||
select {
|
select {
|
||||||
case <-resCh:
|
case <-resCh:
|
||||||
case <-dht.stop.Ch():
|
case <-dht.stop.Ch():
|
||||||
cancel()
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
14
dht/node.go
14
dht/node.go
|
@ -1,7 +1,6 @@
|
||||||
package dht
|
package dht
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -387,7 +386,7 @@ type SendOptions struct {
|
||||||
|
|
||||||
// SendAsync sends a transaction and returns a channel that will eventually contain the transaction response
|
// SendAsync sends a transaction and returns a channel that will eventually contain the transaction response
|
||||||
// The response channel is closed when the transaction is completed or times out.
|
// The response channel is closed when the transaction is completed or times out.
|
||||||
func (n *Node) SendAsync(ctx context.Context, contact Contact, req Request, options ...SendOptions) <-chan *Response {
|
func (n *Node) SendAsync(contact Contact, req Request, options ...SendOptions) <-chan *Response {
|
||||||
ch := make(chan *Response, 1)
|
ch := make(chan *Response, 1)
|
||||||
|
|
||||||
if contact.ID.Equals(n.id) {
|
if contact.ID.Equals(n.id) {
|
||||||
|
@ -429,9 +428,6 @@ func (n *Node) SendAsync(ctx context.Context, contact Contact, req Request, opti
|
||||||
return
|
return
|
||||||
case <-n.stop.Ch():
|
case <-n.stop.Ch():
|
||||||
return
|
return
|
||||||
case <-ctx.Done():
|
|
||||||
// TODO: canceling these requests doesn't do much. we can probably stop supporting this feature and just use async
|
|
||||||
return
|
|
||||||
case <-time.After(udpTimeout):
|
case <-time.After(udpTimeout):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -446,13 +442,7 @@ func (n *Node) SendAsync(ctx context.Context, contact Contact, req Request, opti
|
||||||
// Send sends a transaction and blocks until the response is available. It returns a response, or nil
|
// Send sends a transaction and blocks until the response is available. It returns a response, or nil
|
||||||
// if the transaction timed out.
|
// if the transaction timed out.
|
||||||
func (n *Node) Send(contact Contact, req Request, options ...SendOptions) *Response {
|
func (n *Node) Send(contact Contact, req Request, options ...SendOptions) *Response {
|
||||||
return <-n.SendAsync(context.Background(), contact, req, options...)
|
return <-n.SendAsync(contact, req, options...)
|
||||||
}
|
|
||||||
|
|
||||||
// SendCancelable sends the transaction asynchronously and allows the transaction to be canceled
|
|
||||||
func (n *Node) SendCancelable(contact Contact, req Request, options ...SendOptions) (<-chan *Response, context.CancelFunc) {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
return n.SendAsync(ctx, contact, req, options...), cancel
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountActiveTransactions returns the number of transactions in the manager
|
// CountActiveTransactions returns the number of transactions in the manager
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package dht
|
package dht
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -223,7 +222,7 @@ func (cf *contactFinder) probe(cycleID string) *Contact {
|
||||||
}
|
}
|
||||||
|
|
||||||
var res *Response
|
var res *Response
|
||||||
resCh := cf.node.SendAsync(context.Background(), c, req)
|
resCh := cf.node.SendAsync(c, req)
|
||||||
select {
|
select {
|
||||||
case res = <-resCh:
|
case res = <-resCh:
|
||||||
case <-cf.stop.Ch():
|
case <-cf.stop.Ch():
|
||||||
|
|
|
@ -41,7 +41,7 @@ func (tc *tokenCache) Get(c Contact, hash bits.Bitmap, cancelCh stopOnce.Chan) s
|
||||||
return token.token
|
return token.token
|
||||||
}
|
}
|
||||||
|
|
||||||
resCh, cancel := tc.node.SendCancelable(c, Request{
|
resCh := tc.node.SendAsync(c, Request{
|
||||||
Method: findValueMethod,
|
Method: findValueMethod,
|
||||||
Arg: &hash,
|
Arg: &hash,
|
||||||
})
|
})
|
||||||
|
@ -51,7 +51,6 @@ func (tc *tokenCache) Get(c Contact, hash bits.Bitmap, cancelCh stopOnce.Chan) s
|
||||||
select {
|
select {
|
||||||
case res = <-resCh:
|
case res = <-resCh:
|
||||||
case <-cancelCh:
|
case <-cancelCh:
|
||||||
cancel()
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue