Skip to content

Commit

Permalink
remove cacheddns
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Apr 9, 2024
1 parent 0e84f80 commit 4bef702
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 78 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The following emojis are used to highlight certain changes:

### Added

* `gateway` now includes `NewRemoteBlocksBackend` which allows you to create a gateway backend that uses one or multiple other gateways as backend. These gateways must support RAW block requests, as well as IPNS Record requests. With this, we also introduced a `NewCacheBlockStore`, `NewCachedDNS`, `NewRemoteBlockstore` and `NewRemoteValueStore`.
* `gateway` now includes `NewRemoteBlocksBackend` which allows you to create a gateway backend that uses one or multiple other gateways as backend. These gateways must support RAW block requests, as well as IPNS Record requests. With this, we also introduced a `NewCacheBlockStore`, `NewRemoteBlockstore` and `NewRemoteValueStore`.

### Changed

Expand Down
1 change: 0 additions & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ require (
github.com/quic-go/quic-go v0.40.1 // indirect
github.com/quic-go/webtransport-go v0.6.0 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/ucarion/urlpath v0.0.0-20200424170820-7ccc79b76bbb // indirect
Expand Down
2 changes: 0 additions & 2 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,6 @@ github.com/raulk/go-watchdog v1.3.0/go.mod h1:fIvOnLbF0b0ZwkB9YU4mOW9Did//4vPZtD
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529 h1:18kd+8ZUlt/ARXhljq+14TwAoKa61q6dX8jtwOf6DH8=
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529/go.mod h1:qe5TWALJ8/a1Lqznoc5BDHpYX/8HU60Hm2AwRmqzxqA=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
Expand Down
4 changes: 2 additions & 2 deletions gateway/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ type remoteBlockstore struct {
// specification for more details.
//
// [Trustless Gateway]: https://specs.ipfs.tech/http-gateways/trustless-gateway/
func NewRemoteBlockstore(gatewayURL []string, cdns *CachedDNS) (blockstore.Blockstore, error) {
func NewRemoteBlockstore(gatewayURL []string) (blockstore.Blockstore, error) {
if len(gatewayURL) == 0 {
return nil, errors.New("missing gateway URLs to which to proxy")
}

return &remoteBlockstore{
gatewayURL: gatewayURL,
httpClient: newRemoteHTTPClient(cdns),
httpClient: newRemoteHTTPClient(),
rand: rand.New(rand.NewSource(time.Now().Unix())),
// Enables block validation by default. Important since we are
// proxying block requests to untrusted gateways.
Expand Down
59 changes: 0 additions & 59 deletions gateway/dns.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package gateway

import (
"context"
"fmt"
"net"
"strings"
"time"

"github.com/libp2p/go-doh-resolver"
dns "github.com/miekg/dns"
madns "github.com/multiformats/go-multiaddr-dns"
"github.com/rs/dnscache"
)

var defaultResolvers = map[string]string{
Expand Down Expand Up @@ -94,58 +90,3 @@ func NewDNSResolver(resolvers map[string]string, dohOpts ...doh.Option) (*madns.

return madns.NewResolver(opts...)
}

// CachedDNS implements [http.Transport.DialContext], allowing to cache DNS
// requests for a specified amount of time.
type CachedDNS struct {
resolver *dnscache.Resolver
refresher *time.Ticker
}

func NewCachedDNS(refreshInterval time.Duration) *CachedDNS {
cache := &CachedDNS{
resolver: &dnscache.Resolver{},
refresher: time.NewTicker(refreshInterval),
}

// Configure DNS cache to not remove stale records to protect gateway from
// catastrophic failures like https://github.com/ipfs/bifrost-gateway/issues/34
options := dnscache.ResolverRefreshOptions{}
options.ClearUnused = false
options.PersistOnFailure = true

// Every refreshInterval we check for updates, but if there is
// none, or if domain disappears, we keep the last cached version
go func(cdns *CachedDNS) {
defer cdns.refresher.Stop()
for range cdns.refresher.C {
cdns.resolver.RefreshWithOptions(options)
}
}(cache)

return cache
}
func (cdns *CachedDNS) DialContext(ctx context.Context, network string, addr string) (conn net.Conn, err error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
ips, err := cdns.resolver.LookupHost(ctx, host)
if err != nil {
return nil, err
}
// Try all IPs returned by DNS
for _, ip := range ips {
var dialer net.Dialer
conn, err = dialer.DialContext(ctx, network, net.JoinHostPort(ip, port))
if err == nil {
break
}
}
return
}

func (cdns *CachedDNS) Close() error {
cdns.refresher.Stop()
return nil
}
12 changes: 4 additions & 8 deletions gateway/remote_blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ const getBlockTimeout = time.Second * 60
// If you want to create a more custom [BlocksBackend] with only remote IPNS
// Record resolution, or only remote block fetching, we recommend using
// [NewBlocksBackend] directly.
func NewRemoteBlocksBackend(gatewayURL []string, cdns *CachedDNS, opts ...BlocksBackendOption) (*BlocksBackend, error) {
blockStore, err := NewRemoteBlockstore(gatewayURL, cdns)
func NewRemoteBlocksBackend(gatewayURL []string, opts ...BlocksBackendOption) (*BlocksBackend, error) {
blockStore, err := NewRemoteBlockstore(gatewayURL)
if err != nil {
return nil, err
}

valueStore, err := NewRemoteValueStore(gatewayURL, cdns)
valueStore, err := NewRemoteValueStore(gatewayURL)
if err != nil {
return nil, err
}
Expand All @@ -36,7 +36,7 @@ func NewRemoteBlocksBackend(gatewayURL []string, cdns *CachedDNS, opts ...Blocks

// newRemoteHTTPClient creates a new [http.Client] that is optimized for retrieving
// multiple blocks from a single gateway concurrently.
func newRemoteHTTPClient(cdns *CachedDNS) *http.Client {
func newRemoteHTTPClient() *http.Client {
transport := &http.Transport{
MaxIdleConns: 1000,
MaxConnsPerHost: 100,
Expand All @@ -45,10 +45,6 @@ func newRemoteHTTPClient(cdns *CachedDNS) *http.Client {
ForceAttemptHTTP2: true,
}

if cdns != nil {
transport.DialContext = cdns.DialContext
}

return &http.Client{
Timeout: getBlockTimeout,
Transport: otelhttp.NewTransport(transport),
Expand Down
4 changes: 2 additions & 2 deletions gateway/value_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ type remoteValueStore struct {
// specification for more details.
//
// [Trustless Gateway]: https://specs.ipfs.tech/http-gateways/trustless-gateway/
func NewRemoteValueStore(gatewayURL []string, cdns *CachedDNS) (routing.ValueStore, error) {
func NewRemoteValueStore(gatewayURL []string) (routing.ValueStore, error) {
if len(gatewayURL) == 0 {
return nil, errors.New("missing gateway URLs to which to proxy")
}

return &remoteValueStore{
gatewayURL: gatewayURL,
httpClient: newRemoteHTTPClient(cdns),
httpClient: newRemoteHTTPClient(),
rand: rand.New(rand.NewSource(time.Now().Unix())),
}, nil
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ require (
github.com/pkg/errors v0.9.1
github.com/polydawn/refmt v0.89.0
github.com/prometheus/client_golang v1.18.0
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529
github.com/samber/lo v1.39.0
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,6 @@ github.com/raulk/go-watchdog v1.3.0/go.mod h1:fIvOnLbF0b0ZwkB9YU4mOW9Did//4vPZtD
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529 h1:18kd+8ZUlt/ARXhljq+14TwAoKa61q6dX8jtwOf6DH8=
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529/go.mod h1:qe5TWALJ8/a1Lqznoc5BDHpYX/8HU60Hm2AwRmqzxqA=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
Expand Down

0 comments on commit 4bef702

Please sign in to comment.