mirror of
https://gitlab.com/pnx-tools/dns-updater.git
synced 2026-06-16 05:54:56 +02:00
ip/cache.go: skip storing a NetInterfaceIPResolver in struct, Define a CacheDefaultCallback type and have it passed to the new GetWithDefault function instead.
This commit is contained in:
parent
0f21902ffd
commit
a6c98a3209
3 changed files with 74 additions and 16 deletions
28
app/app.go
28
app/app.go
|
|
@ -1,8 +1,10 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"dnsupdater/provider/manager"
|
||||
|
||||
|
|
@ -11,12 +13,25 @@ import (
|
|||
)
|
||||
|
||||
type App struct {
|
||||
iplookup ip.NetInterfaceIPResolver
|
||||
cache *ip.Cache
|
||||
|
||||
cacheDefaultCallback ip.CacheDefaultCallback
|
||||
|
||||
// Updater manager
|
||||
ProviderManager *manager.Manager
|
||||
}
|
||||
|
||||
func makeCacheCallback(service resolver.Service) ip.CacheDefaultCallback {
|
||||
return func(name string) (net.IP, error) {
|
||||
if name == resolver.WAN_IFACE {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
return service.Lookup(ctx)
|
||||
}
|
||||
return ip.GetInterfaceIP(name)
|
||||
}
|
||||
}
|
||||
|
||||
func NewApp(config *Config) (*App, error) {
|
||||
providerMgr := manager.New()
|
||||
// providerMgr.Register("digitalocean", digitalocean.New(config.Services.DigitalOcean.Token))
|
||||
|
|
@ -25,18 +40,19 @@ func NewApp(config *Config) (*App, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
l := resolver.Get(config.Services.IPLookup)
|
||||
service := resolver.Get(config.Services.IPLookup)
|
||||
|
||||
if l == nil {
|
||||
if service == nil {
|
||||
return nil, fmt.Errorf("Failed to load lookup service: %s", config.Services.IPLookup)
|
||||
}
|
||||
|
||||
return &App{
|
||||
ProviderManager: providerMgr,
|
||||
iplookup: ip.NewCache(ip.LookupWrapper(l)).Get,
|
||||
ProviderManager: providerMgr,
|
||||
cache: ip.NewCache(),
|
||||
cacheDefaultCallback: makeCacheCallback(service),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a App) GetIP(iface_name string) (net.IP, error) {
|
||||
return a.iplookup(iface_name)
|
||||
return a.cache.GetWithDefault(iface_name, a.cacheDefaultCallback)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue