2018-03-24 00:18:00 +01:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
2018-03-29 03:05:27 +02:00
|
|
|
"context"
|
2018-03-24 00:18:00 +01:00
|
|
|
"net"
|
2018-04-05 17:35:57 +02:00
|
|
|
"strings"
|
2018-03-24 00:18:00 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2018-04-04 17:43:27 +02:00
|
|
|
// transaction represents a single query to the dht. it stores the queried node, the request, and the response channel
|
2018-03-24 00:18:00 +01:00
|
|
|
type transaction struct {
|
2018-03-29 03:05:27 +02:00
|
|
|
node Node
|
2018-04-04 17:43:27 +02:00
|
|
|
req Request
|
|
|
|
res chan Response
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
|
|
|
|
2018-04-04 17:43:27 +02:00
|
|
|
// transactionManager keeps track of the outstanding transactions
|
2018-03-24 00:18:00 +01:00
|
|
|
type transactionManager struct {
|
2018-04-04 17:43:27 +02:00
|
|
|
dht *DHT
|
2018-03-24 00:18:00 +01:00
|
|
|
lock *sync.RWMutex
|
2018-04-03 19:38:01 +02:00
|
|
|
transactions map[messageID]*transaction
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
|
|
|
|
2018-04-04 17:43:27 +02:00
|
|
|
// newTransactionManager returns a new transactionManager
|
2018-03-24 00:18:00 +01:00
|
|
|
func newTransactionManager(dht *DHT) *transactionManager {
|
|
|
|
return &transactionManager{
|
|
|
|
lock: &sync.RWMutex{},
|
2018-04-03 19:38:01 +02:00
|
|
|
transactions: make(map[messageID]*transaction),
|
2018-03-24 00:18:00 +01:00
|
|
|
dht: dht,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:43:27 +02:00
|
|
|
// insert adds a transaction to the manager.
|
|
|
|
func (tm *transactionManager) insert(tx *transaction) {
|
2018-03-24 00:18:00 +01:00
|
|
|
tm.lock.Lock()
|
|
|
|
defer tm.lock.Unlock()
|
2018-04-04 17:43:27 +02:00
|
|
|
tm.transactions[tx.req.ID] = tx
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
|
|
|
|
2018-04-04 17:43:27 +02:00
|
|
|
// delete removes a transaction from the manager.
|
2018-04-03 19:38:01 +02:00
|
|
|
func (tm *transactionManager) delete(id messageID) {
|
2018-03-24 00:18:00 +01:00
|
|
|
tm.lock.Lock()
|
|
|
|
defer tm.lock.Unlock()
|
2018-04-03 19:38:01 +02:00
|
|
|
delete(tm.transactions, id)
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
|
|
|
|
2018-04-04 17:43:27 +02:00
|
|
|
// Find finds a transaction for the given id. it optionally ensures that addr matches node from transaction
|
2018-04-03 19:38:01 +02:00
|
|
|
func (tm *transactionManager) Find(id messageID, addr *net.UDPAddr) *transaction {
|
2018-03-24 00:18:00 +01:00
|
|
|
tm.lock.RLock()
|
|
|
|
defer tm.lock.RUnlock()
|
|
|
|
|
|
|
|
t, ok := tm.transactions[id]
|
2018-04-04 17:43:27 +02:00
|
|
|
if !ok || (addr != nil && t.node.Addr().String() != addr.String()) {
|
2018-03-24 00:18:00 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:43:27 +02:00
|
|
|
// 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.
|
|
|
|
func (tm *transactionManager) SendAsync(ctx context.Context, node Node, req Request) <-chan *Response {
|
2018-03-24 00:18:00 +01:00
|
|
|
if node.id.Equals(tm.dht.node.id) {
|
|
|
|
log.Error("sending query to self")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-29 03:05:27 +02:00
|
|
|
ch := make(chan *Response, 1)
|
2018-03-24 00:18:00 +01:00
|
|
|
|
2018-03-29 03:05:27 +02:00
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
2018-03-24 00:18:00 +01:00
|
|
|
|
2018-03-29 03:05:27 +02:00
|
|
|
req.ID = newMessageID()
|
2018-04-03 19:38:01 +02:00
|
|
|
req.NodeID = tm.dht.node.id
|
2018-04-04 17:43:27 +02:00
|
|
|
tx := &transaction{
|
2018-03-29 03:05:27 +02:00
|
|
|
node: node,
|
|
|
|
req: req,
|
2018-04-04 17:43:27 +02:00
|
|
|
res: make(chan Response),
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
|
|
|
|
2018-04-04 17:43:27 +02:00
|
|
|
tm.insert(tx)
|
|
|
|
defer tm.delete(tx.req.ID)
|
2018-03-29 03:05:27 +02:00
|
|
|
|
|
|
|
for i := 0; i < udpRetry; i++ {
|
2018-04-04 17:43:27 +02:00
|
|
|
if err := send(tm.dht, node.Addr(), tx.req); err != nil {
|
2018-04-05 17:35:57 +02:00
|
|
|
if !strings.Contains(err.Error(), "use of closed network connection") { // this only happens on localhost. real UDP has no connections
|
|
|
|
log.Error("send error: ", err)
|
|
|
|
}
|
2018-03-29 03:05:27 +02:00
|
|
|
continue // try again? return?
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2018-04-04 17:43:27 +02:00
|
|
|
case res := <-tx.res:
|
|
|
|
ch <- &res
|
2018-03-29 03:05:27 +02:00
|
|
|
return
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(udpTimeout):
|
|
|
|
}
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
|
|
|
|
2018-03-29 03:05:27 +02:00
|
|
|
// if request timed out each time
|
2018-04-04 17:43:27 +02:00
|
|
|
tm.dht.rt.RemoveByID(tx.node.id)
|
2018-03-29 03:05:27 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:43:27 +02:00
|
|
|
// Send sends a transaction and blocks until the response is available. It returns a response, or nil
|
|
|
|
// if the transaction timed out.
|
|
|
|
func (tm *transactionManager) Send(node Node, req Request) *Response {
|
2018-03-29 03:05:27 +02:00
|
|
|
return <-tm.SendAsync(context.Background(), node, req)
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the number of transactions in the manager
|
|
|
|
func (tm *transactionManager) Count() int {
|
|
|
|
tm.lock.Lock()
|
|
|
|
defer tm.lock.Unlock()
|
|
|
|
return len(tm.transactions)
|
|
|
|
}
|