Skip to content

Commit

Permalink
Merge branch 'gostones-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jan 25, 2021
2 parents c28389b + f408ec3 commit 82aa02b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -84,6 +84,7 @@ High-level overview:

```hosts
echo '127.0.0.1 docker.local' | sudo tee -a /etc/hosts
echo '::1 docker.local' | sudo tee -a /etc/hosts
```

- Flush local DNS cache:
Expand Down
30 changes: 30 additions & 0 deletions netutil/netutil.go
Expand Up @@ -3,10 +3,40 @@ package netutil
import (
"errors"
"net"
"net/http"
"regexp"
"strconv"
"time"
)

// Default http client with timeout
// https://golang.org/pkg/net/http/#pkg-examples
// Clients and Transports are safe for concurrent use by multiple goroutines.
var defaultClient = &http.Client{
Timeout: time.Second * 10,
Transport: defaultTransport,
}

// https://golang.org/src/net/http/transport.go
var defaultTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 10 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 10,
IdleConnTimeout: 10 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}

// Get issues a GET to the specified URL - a drop-in replacement for http.Get with timeouts.
func Get(url string) (resp *http.Response, err error) {
return defaultClient.Get(url)
}

// GetFreePort asks the kernel for a free open port that is ready to use.
func GetFreePort() (int, error) {
ip, err := LocalIP()
Expand Down
4 changes: 3 additions & 1 deletion regutil/regutil.go
Expand Up @@ -8,6 +8,8 @@ import (
"regexp"
"strings"

"github.com/miguelmota/ipdr/netutil"

cid "github.com/ipfs/go-cid"
base58 "github.com/jbenet/go-base58"
mbase "github.com/multiformats/go-multibase"
Expand Down Expand Up @@ -86,7 +88,7 @@ func ToB32(s string) string {
func Dig(gw string, short bool, name string) (string, error) {
uri := fmt.Sprintf("http://%s/dig?q=%s&short=%v", gw, name, short)

resp, err := http.Get(uri)
resp, err := netutil.Get(uri)
if err != nil {
return "", err
}
Expand Down
4 changes: 3 additions & 1 deletion server/registry/blobs.go
Expand Up @@ -26,6 +26,8 @@ import (
"path"
"strings"
"sync"

"github.com/miguelmota/ipdr/netutil"
)

// Returns whether this url should be handled by the blob handler
Expand Down Expand Up @@ -148,7 +150,7 @@ func (b *blobs) handle(resp http.ResponseWriter, req *http.Request) *regError {
}
}
uri := b.registry.ipfsURL([]string{cid, "blobs", target})
ipfsResp, err := http.Get(uri)
ipfsResp, err := netutil.Get(uri)
if err != nil {
return &regError{
Status: http.StatusNotFound,
Expand Down
3 changes: 0 additions & 3 deletions server/registry/cids.go
Expand Up @@ -18,9 +18,6 @@ type cidStore struct {
}

func key(repo, ref string) string {
if ref == "" {
ref = "latest"
}
return repo + ":" + ref
}

Expand Down
2 changes: 2 additions & 0 deletions server/registry/registry.go
Expand Up @@ -176,6 +176,8 @@ func (r *registry) resolveCID(repo, reference string) (string, error) {
}

func (r *registry) resolve(repo, reference string) []string {
r.log.Printf("resolving CID: %s:%s", repo, reference)

// local/cached
if cid, ok := r.cids.Get(repo, reference); ok {
return []string{cid}
Expand Down
3 changes: 2 additions & 1 deletion server/registry/util.go
Expand Up @@ -5,12 +5,13 @@ import (
"io/ioutil"
"net/http"

"github.com/miguelmota/ipdr/netutil"
"github.com/miguelmota/ipdr/regutil"
)

func getContent(gw string, cid string, s []string) ([]byte, error) {
uri := regutil.IpfsURL(gw, append([]string{cid}, s...))
resp, err := http.Get(uri)
resp, err := netutil.Get(uri)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 82aa02b

Please sign in to comment.