Skip to content

Commit

Permalink
Create helper init fn
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Aug 23, 2023
1 parent 7b50084 commit 912f92d
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions p2p/http/libp2phttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,6 @@ type httpTransport struct {
waitingForListeners chan struct{}
}

func newHTTPTransport() *httpTransport {
return &httpTransport{
closeListeners: make(chan struct{}),
waitingForListeners: make(chan struct{}),
}
}

func newPeerMetadataCache() *lru.Cache[peer.ID, PeerMeta] {
peerMetadata, err := lru.New[peer.ID, PeerMeta](peerMetadataLRUSize)
if err != nil {
Expand All @@ -164,10 +157,17 @@ func newPeerMetadataCache() *lru.Cache[peer.ID, PeerMeta] {
return peerMetadata
}

func (h *Host) Addrs() []ma.Multiaddr {
func (h *Host) httpTransportInit() {
h.createHTTPTransport.Do(func() {
h.httpTransport = newHTTPTransport()
h.httpTransport = &httpTransport{
closeListeners: make(chan struct{}),
waitingForListeners: make(chan struct{}),
}
})
}

func (h *Host) Addrs() []ma.Multiaddr {
h.httpTransportInit()
<-h.httpTransport.waitingForListeners
return h.httpTransport.listenAddrs
}
Expand Down Expand Up @@ -195,9 +195,7 @@ func (h *Host) Serve() error {

h.ServeMux.Handle("/.well-known/libp2p", &h.WellKnownHandler)

h.createHTTPTransport.Do(func() {
h.httpTransport = newHTTPTransport()
})
h.httpTransportInit()

closedWaitingForListeners := false
defer func() {
Expand Down Expand Up @@ -325,9 +323,7 @@ func (h *Host) Serve() error {
}

func (h *Host) Close() error {
h.createHTTPTransport.Do(func() {
h.httpTransport = newHTTPTransport()
})
h.httpTransportInit()
close(h.httpTransport.closeListeners)
return nil
}
Expand Down

0 comments on commit 912f92d

Please sign in to comment.