mirror of
https://gitlab.com/pnx-tools/dns-updater.git
synced 2026-06-16 05:54:56 +02:00
ip/resolver: make Service.Lookup() accept a context
This commit is contained in:
parent
43705f81d1
commit
ec9056e9e0
6 changed files with 42 additions and 13 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package basic_http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
|
|
@ -18,8 +19,8 @@ func (s Service) Name() string {
|
|||
return s.ServiceName
|
||||
}
|
||||
|
||||
func (s Service) Lookup() (net.IP, error) {
|
||||
req, err := http.NewRequest("GET", s.Url, nil)
|
||||
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", s.Url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package basic_http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
|
@ -24,7 +25,7 @@ func TestService_Lookup(t *testing.T) {
|
|||
|
||||
s := Service{Url: server.URL}
|
||||
|
||||
ip, err := s.Lookup()
|
||||
ip, err := s.Lookup(context.Background())
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, net.IPv4(255, 240, 85, 2), ip)
|
||||
|
|
@ -46,7 +47,7 @@ func TestService_Lookup_WithHeaders(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
ip, err := s.Lookup()
|
||||
ip, err := s.Lookup(context.Background())
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, net.IPv4(125, 74, 233, 13), ip)
|
||||
|
|
@ -62,7 +63,7 @@ func TestService_Lookup_HTTPError(t *testing.T) {
|
|||
Url: server.URL,
|
||||
}
|
||||
|
||||
ip, err := s.Lookup()
|
||||
ip, err := s.Lookup(context.Background())
|
||||
assert.EqualError(t, err, "HTTP Response: 404 Not Found")
|
||||
assert.Nil(t, ip)
|
||||
}
|
||||
|
|
@ -78,7 +79,7 @@ func TestService_Lookup_ParseError(t *testing.T) {
|
|||
Url: server.URL,
|
||||
}
|
||||
|
||||
ip, err := s.Lookup()
|
||||
ip, err := s.Lookup(context.Background())
|
||||
assert.EqualError(t, err, "Failed to parse ip: random_string")
|
||||
assert.Nil(t, ip)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package jsonip
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
|
|
@ -20,8 +21,13 @@ func (s Service) Name() string {
|
|||
return "jsonip"
|
||||
}
|
||||
|
||||
func (s Service) Lookup() (net.IP, error) {
|
||||
resp, err := http.DefaultClient.Get(s.url)
|
||||
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", s.url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package jsonip
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
|
@ -24,8 +25,21 @@ func TestService_Lookup(t *testing.T) {
|
|||
|
||||
s := Service{url: server.URL}
|
||||
|
||||
ip, err := s.Lookup()
|
||||
ip, err := s.Lookup(context.Background())
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, net.IPv4(211, 46, 32, 214), ip)
|
||||
}
|
||||
|
||||
func TestService_Lookup_HTTPError(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(404)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
s := Service{url: server.URL}
|
||||
|
||||
ip, err := s.Lookup(context.Background())
|
||||
assert.EqualError(t, err, "HTTP Response: 404 Not Found")
|
||||
assert.Nil(t, ip)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
package mock
|
||||
|
||||
import "net"
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
IP net.IP
|
||||
|
|
@ -11,6 +14,6 @@ func (s Service) Name() string {
|
|||
return "mock"
|
||||
}
|
||||
|
||||
func (s Service) Lookup() (net.IP, error) {
|
||||
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
|
||||
return s.IP, s.Error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
package lookup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"dnsupdater/ip"
|
||||
)
|
||||
|
|
@ -9,7 +11,7 @@ import (
|
|||
type Service interface {
|
||||
Name() string
|
||||
|
||||
Lookup() (net.IP, error)
|
||||
Lookup(ctx context.Context) (net.IP, error)
|
||||
}
|
||||
|
||||
const WAN_IFACE = "wan"
|
||||
|
|
@ -17,7 +19,9 @@ const WAN_IFACE = "wan"
|
|||
func LookupWrapper(service Service) ip.NetInterfaceIPResolver {
|
||||
return func(iface_name string) (net.IP, error) {
|
||||
if iface_name == WAN_IFACE {
|
||||
return service.Lookup()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
return service.Lookup(ctx)
|
||||
}
|
||||
return ip.GetInterfaceIP(iface_name)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue