-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload.go
130 lines (118 loc) · 2.86 KB
/
load.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package dht
import (
"bytes"
"errors"
"io"
"io/ioutil"
"sync"
"github.com/esote/dht/core"
"github.com/esote/dht/find"
"github.com/esote/dht/util"
)
func (dht *DHT) Load(key []byte) (io.ReadCloser, uint64, error) {
if len(key) != core.KeySize {
return nil, 0, errors.New("invalid key")
}
var findOnce sync.Once
var data *core.DataPayload
id := key[:core.NodeIDSize]
f, err := find.Find(&find.Config{
Start: []*core.NodeTriple{dht.self},
Target: id,
Workers: maxFindWorkers,
MaxBacklogSize: maxFindBacklogSize,
MaxReturn: maxFindReturn,
MaxUniqueHistory: maxFindUniqueHistory,
Query: func(target *core.NodeTriple) []*core.NodeTriple {
closest, targetData := dht.findValue(key, target)
if targetData != nil {
findOnce.Do(func() {
data = targetData
})
return []*core.NodeTriple{{ID: id}}
}
return closest
},
})
if err != nil {
return nil, 0, err
}
dht.addFinder(f)
<-f.Done
dht.removeFinder(f)
if data == nil {
return nil, 0, errors.New("value not found")
}
if closer, ok := data.Value.(io.Closer); ok {
return util.JoinReadCloser(data.Value, closer), data.Length, nil
}
dht.log(LogErr, "data value missing closer")
return ioutil.NopCloser(data.Value), data.Length, nil
}
func (dht *DHT) findValue(key []byte, target *core.NodeTriple) ([]*core.NodeTriple, *core.DataPayload) {
id := key[:core.NodeIDSize]
if bytes.Equal(target.ID, dht.self.ID) {
value, length, err := dht.storer.Load(key)
if err != nil {
closest, err := dht.rtable.Closest(id, k)
if err != nil {
dht.log(LogErr, err)
return nil, nil
}
return closest, nil
}
return nil, &core.DataPayload{
Length: length,
Value: value,
}
}
s, err := dht.newSession()
if err != nil {
dht.log(LogErr, err)
return nil, nil
}
defer func() {
if err = s.Close(); err != nil {
dht.log(LogErr, err)
}
}()
fval := &core.FindValuePayload{
Key: key,
}
if err = s.send(fval, target); err != nil {
dht.log(LogErr, err)
return nil, nil
}
msg, err := s.recv()
if err != nil {
dht.log(LogErr, err)
return nil, nil
}
if msg.Hdr.MsgType != core.TypeData {
// Close immediately, DATA messages are the only expected stream
// message.
if err = msg.Close(); err != nil {
dht.log(LogErr, err)
return nil, nil
}
}
switch v := msg.Payload.(type) {
case *core.DataPayload:
// Hide msg closer in value reader.
v.Value = util.JoinReadCloser(v.Value, msg).(io.Reader)
return nil, v
case *core.FindNodeRespPayload:
dht.logf(LogDebug, "got %d nodes", len(v.Nodes))
if len(v.Nodes) > k {
dht.logf(LogDebug, "discarding %d nodes", len(v.Nodes)-k)
v.Nodes = v.Nodes[:k]
}
return v.Nodes, nil
case *core.ErrorPayload:
dht.log(LogDebug, "FIND_VALUE error:", string(v.Msg))
return nil, nil
default:
dht.log(LogErr, "received unexpected payload type")
return nil, nil
}
}