coreapi name: add some missing options
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Oct 4, 2018 at 22:11 UTC
e8b137f885eb6508b0ff6439acf734e38b54d36a
2 files changed
+31
core/coreapi/interface/options/name.go
+24
@@ -13,6 +13,10 @@ const (
13
type NamePublishSettings struct {
14
ValidTime time.Duration
15
Key string
16
+
17
+ TTL *time.Duration
18
+
19
+ AllowOffline bool
20
}
21
22
type NameResolveSettings struct {
@@ -29,6 +33,8 @@ func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error)
33
options := &NamePublishSettings{
34
ValidTime: DefaultNameValidTime,
35
Key: "self",
36
+
37
+ AllowOffline: false,
38
}
39
40
for _, opt := range opts {
@@ -82,6 +88,24 @@ func (nameOpts) Key(key string) NamePublishOption {
88
}
89
}
90
91
+// AllowOffline is an option for Name.Publish which specifies whether to allow
92
+// publishing when the node is offline. Default value is false
93
+func (nameOpts) AllowOffline(allow bool) NamePublishOption {
94
+ return func(settings *NamePublishSettings) error {
95
+ settings.AllowOffline = allow
96
+ return nil
97
+ }
98
+}
99
+
100
+// TTL is an option for Name.Publish which specifies the time duration the
101
+// published record should be cached for (caution: experimental).
102
+func (nameOpts) TTL(ttl time.Duration) NamePublishOption {
103
+ return func(settings *NamePublishSettings) error {
104
+ settings.TTL = &ttl
105
+ return nil
106
+ }
107
+}
108
+
109
// Local is an option for Name.Resolve which specifies if the lookup should be
110
// offline. Default value is false
111
func (nameOpts) Local(local bool) NameResolveOption {
core/coreapi/name.go
+7
@@ -45,6 +45,9 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
45
n := api.node
46
47
if !n.OnlineMode() {
48
+ if !options.AllowOffline {
49
+ return nil, coreiface.ErrOffline
50
+ }
51
err := n.SetupOfflineRouting()
52
if err != nil {
53
return nil, err
@@ -65,6 +68,10 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
68
return nil, err
69
}
70
71
+ if options.TTL != nil {
72
+ ctx = context.WithValue(ctx, "ipns-publish-ttl", *options.TTL)
73
+ }
74
+
75
eol := time.Now().Add(options.ValidTime)
76
err = n.Namesys.PublishWithEOL(ctx, k, pth, eol)
77
if err != nil {