fix: invalidate cache on failed publish
If we fail to publish, _invalidate_ our cache. The publish may have partially succeeded.
Steven Allen committed
Apr 13, 2020 at 18:22 UTC
c12c184ca499d1059e684e010217c83b30752d65
3 files changed
+23
-6
namesys/cache.go
+7
@@ -49,6 +49,13 @@ func (ns *mpns) cacheSet(name string, val path.Path, ttl time.Duration) {
49
})
50
}
51
52
+func (ns *mpns) cacheInvalidate(name string) {
53
+ if ns.cache == nil {
54
+ return
55
+ }
56
+ ns.cache.Remove(name)
57
+}
58
+
59
type cacheEntry struct {
60
val path.Path
61
eol time.Time
namesys/namesys.go
+3
@@ -218,6 +218,9 @@ func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, value path.
218
return err
219
}
220
if err := ns.ipnsPublisher.PublishWithEOL(ctx, name, value, eol); err != nil {
221
+ // Invalidate the cache. Publishing may _partially_ succeed but
222
+ // still return an error.
223
+ ns.cacheInvalidate(peer.Encode(id))
224
return err
225
}
226
ttl := DefaultResolverCacheTTL
test/sharness/t0160-resolve.sh
+13
-6
@@ -153,17 +153,24 @@ test_resolve_cmd_fail() {
153
test_resolve "/ipld/$dag_hash/i/j" "/ipld/$dag_hash/i/j"
154
test_resolve "/ipld/$dag_hash/i" "/ipld/$dag_hash/i"
155
156
+ # At the moment, publishing _fails_ because we fail to put to the DHT.
157
+ # However, resolving succeeds because we resolve the record we put to our own
158
+ # node.
159
+ #
160
+ # We should find a nice way to truly support offline publishing. But this
161
+ # behavior isn't terrible.
162
+
163
test_resolve_setup_name_fail "self" "/ipfs/$a_hash"
157
- test_resolve_fail "/ipns/$self_hash" "/ipfs/$a_hash"
158
- test_resolve_fail "/ipns/$self_hash/b" "/ipfs/$b_hash"
159
- test_resolve_fail "/ipns/$self_hash/b/c" "/ipfs/$c_hash"
164
+ test_resolve "/ipns/$self_hash" "/ipfs/$a_hash"
165
+ test_resolve "/ipns/$self_hash/b" "/ipfs/$b_hash"
166
+ test_resolve "/ipns/$self_hash/b/c" "/ipfs/$c_hash"
167
168
test_resolve_setup_name_fail "self" "/ipfs/$b_hash"
162
- test_resolve_fail "/ipns/$self_hash" "/ipfs/$b_hash"
163
- test_resolve_fail "/ipns/$self_hash/c" "/ipfs/$c_hash"
169
+ test_resolve "/ipns/$self_hash" "/ipfs/$b_hash"
170
+ test_resolve "/ipns/$self_hash/c" "/ipfs/$c_hash"
171
172
test_resolve_setup_name_fail "self" "/ipfs/$c_hash"
166
- test_resolve_fail "/ipns/$self_hash" "/ipfs/$c_hash"
173
+ test_resolve "/ipns/$self_hash" "/ipfs/$c_hash"
174
}
175
176
# should work offline