reflector.go/peer/http3/store.go

94 lines
2 KiB
Go
Raw Normal View History

package http3
import (
"crypto/tls"
"crypto/x509"
"net/http"
"time"
"github.com/lbryio/lbry.go/v2/extras/errors"
"github.com/lbryio/lbry.go/v2/stream"
"github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/http3"
)
// Store is a blob store that gets blobs from a peer.
// It satisfies the store.BlobStore interface but cannot put or delete blobs.
type Store struct {
2020-05-13 21:53:57 +02:00
opts StoreOpts
}
// StoreOpts allows to set options for a new Store.
type StoreOpts struct {
Address string
Timeout time.Duration
}
// NewStore makes a new peer store.
func NewStore(opts StoreOpts) *Store {
2020-05-13 21:53:57 +02:00
return &Store{opts: opts}
}
2020-05-13 21:53:57 +02:00
func (p *Store) getClient() (*Client, error) {
var qconf quic.Config
qconf.HandshakeTimeout = 4 * time.Second
2020-10-19 23:42:40 +02:00
qconf.MaxIdleTimeout = 10 * time.Second
pool, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
roundTripper := &http3.RoundTripper{
TLSClientConfig: &tls.Config{
RootCAs: pool,
InsecureSkipVerify: true,
},
QuicConfig: &qconf,
}
2020-06-30 01:14:52 +02:00
connection := &http.Client{
Transport: roundTripper,
}
c := &Client{
2020-06-30 01:14:52 +02:00
conn: connection,
roundTripper: roundTripper,
ServerAddr: p.opts.Address,
}
2020-05-13 21:53:57 +02:00
return c, errors.Prefix("connection error", err)
}
2020-10-22 19:49:02 +02:00
func (p *Store) Name() string { return "http3" }
// Has asks the peer if they have a hash
func (p *Store) Has(hash string) (bool, error) {
2020-05-13 21:53:57 +02:00
c, err := p.getClient()
if err != nil {
return false, err
}
2020-05-13 21:53:57 +02:00
defer c.Close()
return c.HasBlob(hash)
}
// Get downloads the blob from the peer
func (p *Store) Get(hash string) (stream.Blob, error) {
2020-05-13 21:53:57 +02:00
c, err := p.getClient()
if err != nil {
return nil, err
}
2020-05-13 21:53:57 +02:00
defer c.Close()
return c.GetBlob(hash)
}
// Put is not supported
func (p *Store) Put(hash string, blob stream.Blob) error {
2020-06-30 01:14:52 +02:00
panic("http3Store cannot put or delete blobs")
}
// PutSD is not supported
func (p *Store) PutSD(hash string, blob stream.Blob) error {
2020-06-30 01:14:52 +02:00
panic("http3Store cannot put or delete blobs")
}
// Delete is not supported
func (p *Store) Delete(hash string) error {
2020-06-30 01:14:52 +02:00
panic("http3Store cannot put or delete blobs")
}