-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhub_publisher.go
153 lines (143 loc) · 3.91 KB
/
hub_publisher.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package exchange
import (
"context"
"net/http"
"sync/atomic"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
p2phttp "github.com/libp2p/go-libp2p-http"
"github.com/libp2p/go-libp2p/core/host"
"github.com/multiformats/go-multihash"
)
type hubPublisher struct {
*options
h host.Host
buffer chan cid.Cid
ctx context.Context
cancel context.CancelFunc
client *http.Client
}
type PutContentRequest struct {
Mutlihashes []multihash.Multihash
}
func newHubPublisher(h host.Host, opts *options) (*hubPublisher, error) {
p := &hubPublisher{
h: h,
options: opts,
}
p.ctx, p.cancel = context.WithCancel(context.Background())
p.buffer = make(chan cid.Cid, p.ipniPublishChanBuffer)
tr := &http.Transport{}
tr.RegisterProtocol("libp2p", p2phttp.NewTransport(h, p2phttp.ProtocolOption("/fx.land/hub/0.0.1")))
p.client = &http.Client{Transport: tr}
if err := p.assureConnectionToHub(p.ctx); err != nil {
return nil, err
}
return p, nil
}
func (p *hubPublisher) assureConnectionToHub(ctx context.Context) error {
_ = ctx // Acknowledge ctx to avoid unused variable warning
/*a, err := peer.AddrInfoFromString("/dns/hub.dev.fx.land/tcp/40004/p2p/12D3KooWFmfEsXjWotvqJ6B3ASXx1w3p6udj8R9f344a9JTu2k4R")
if err != nil {
return err
}
p.h.Peerstore().AddAddrs(a.ID, a.Addrs, peerstore.PermanentAddrTTL)
if err := p.h.Connect(ctx, *a); err != nil {
log.Errorw("Failed to connect to hub", "err", err)
return err
}*/
return nil
}
func (p *hubPublisher) Start(_ context.Context) error {
go func() {
unpublished := make(map[cid.Cid]struct{})
var publishing atomic.Bool
maybePublish := func() {
remaining := len(unpublished)
if remaining == 0 {
//log.Debug("hubPublisher: No remaining entries to publish")
return
}
if publishing.Load() {
log.Debugw("hubPublisher: IPNI publishing in progress", "remaining", remaining)
return
}
log.Debugw("hubPublisher: Attempting to publish links to IPNI", "count", remaining)
mhs := make([]multihash.Multihash, 0, remaining)
for c := range unpublished {
mhs = append(mhs, c.Hash())
delete(unpublished, c)
}
publishing.Store(true)
go func(entries []multihash.Multihash) {
log.Debug("hubPublisher: IPNI publish attempt in progress...")
defer func() {
publishing.Store(false)
log.Debug("hubPublisher: Finished attempt to publish to IPNI.")
}()
if err := p.publish(entries); err != nil {
log.Errorw("hubPublisher: Failed to publish to IPNI", "entriesCount", len(mhs), "err", err)
}
}(mhs)
}
for {
select {
case <-p.ctx.Done():
log.Infow("hubPublisher: IPNI publisher stopped", "remainingLinks", len(unpublished))
return
case <-p.ipniPublishTicker.C:
maybePublish()
case c := <-p.buffer:
unpublished[c] = struct{}{}
maybePublish()
}
}
}()
return nil
}
func (p *hubPublisher) notifyReceivedLink(l ipld.Link) {
if l == nil {
return
}
/*
link, ok := l.(cidlink.Link)
if ok &&
!cid.Undef.Equals(link.Cid) &&
link.Cid.Prefix().MhType != multihash.IDENTITY {
p.buffer <- link.Cid
}*/
}
func (p *hubPublisher) publish(mhs []multihash.Multihash) error {
if len(mhs) == 0 {
_ = mhs // Use the variable to avoid unused warnings.
return nil
}
/*if err := p.assureConnectionToHub(p.ctx); err != nil {
return err
}
r := &PutContentRequest{
Mutlihashes: mhs,
}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&r); err != nil {
return err
}
request, err := http.NewRequest(http.MethodPut, "libp2p://12D3KooWFmfEsXjWotvqJ6B3ASXx1w3p6udj8R9f344a9JTu2k4R/content", &buf)
if err != nil {
return err
}
resp, err := p.client.Do(request)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("receievd non OK response from hub: %d", resp.StatusCode)
}*/
return nil
}
func (p *hubPublisher) shutdown() error {
p.cancel()
p.ipniPublishTicker.Stop()
log.Info("ipniPublishTicker stopped")
return nil
}