chore(publicip): refactoring

- Exported `Fetcher` interface
- Inject `Fetcher` to publicip loop and updaters
- Get public IP and information at the same time
- Only query ipinfo.io
- Make `MultiInfo` part of the `Fetch` object
This commit is contained in:
Quentin McGaw
2022-06-12 00:09:01 +00:00
parent 45c9e780c0
commit 83b4a3fe55
19 changed files with 101 additions and 155 deletions

View File

@@ -3,18 +3,17 @@ package publicip
import (
"context"
"net"
"net/http"
"github.com/qdm12/gluetun/internal/publicip/models"
)
// MultiInfo obtains the public IP address information for every IP
// FetchMultiInfo obtains the public IP address information for every IP
// addresses provided and returns a slice of results with the corresponding
// order as to the IP addresses slice order.
// If an error is encountered, all the operations are canceled and
// an error is returned, so the results returned should be considered
// incomplete in this case.
func MultiInfo(ctx context.Context, client *http.Client, ips []net.IP) (
func (f *Fetch) FetchMultiInfo(ctx context.Context, ips []net.IP) (
results []models.IPInfoData, err error) {
ctx, cancel := context.WithCancel(ctx)
@@ -30,7 +29,7 @@ func MultiInfo(ctx context.Context, client *http.Client, ips []net.IP) (
aResult := asyncResult{
index: index,
}
aResult.result, aResult.err = Info(ctx, client, ip)
aResult.result, aResult.err = f.FetchInfo(ctx, ip)
resultsCh <- aResult
}(i, ip)
}