diff --git a/internal/handler/handler.go b/internal/handler/handler.go index 0f15ec7a..34b31221 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -23,6 +23,7 @@ var ( ) type Handler struct { + ctx context.Context Configuration *settings.Settings dnsProvider provider.IDNSProvider notificationManager notification.INotificationManager @@ -30,10 +31,14 @@ type Handler struct { cachedIP string } +func (handler *Handler) SetContext(ctx context.Context) { + handler.ctx = ctx +} + func (handler *Handler) SetConfiguration(conf *settings.Settings) { handler.Configuration = conf handler.notificationManager = notification.GetNotificationManager(handler.Configuration) - handler.ipManager = lib.NewIPHelper(handler.Configuration) + handler.ipManager = lib.GetIPHelperInstance(handler.Configuration) } func (handler *Handler) SetProvider(provider provider.IDNSProvider) { diff --git a/internal/manager/dns_manager.go b/internal/manager/dns_manager.go index f9cfd397..72267087 100644 --- a/internal/manager/dns_manager.go +++ b/internal/manager/dns_manager.go @@ -30,14 +30,16 @@ func (manager *DNSManager) Build() error { return err } + ctx, cancel := context.WithCancel(context.Background()) + manager.ctx = ctx + manager.cancel = cancel + manager.provider = dnsProvider manager.handler = &handler.Handler{} + manager.handler.SetContext(manager.ctx) manager.handler.SetConfiguration(manager.configuration) manager.handler.SetProvider(manager.provider) - ctx, cancel := context.WithCancel(context.Background()) - manager.ctx = ctx - manager.cancel = cancel return nil } diff --git a/pkg/lib/ip_helper.go b/pkg/lib/ip_helper.go index d3d50a32..bdfdbf5c 100644 --- a/pkg/lib/ip_helper.go +++ b/pkg/lib/ip_helper.go @@ -27,44 +27,55 @@ type IPHelper struct { idx int64 } -func NewIPHelper(conf *settings.Settings) *IPHelper { - manager := &IPHelper{ - configuration: conf, - idx: -1, - } +var ( + helperInstance *IPHelper + helperOnce sync.Once +) +func (helper *IPHelper) loadConfiguration(conf *settings.Settings) { if conf.IPType == "" || strings.ToUpper(conf.IPType) == utils.IPV4 { // filter empty urls for _, url := range conf.IPUrls { if url != "" { - manager.reqURLs = append(manager.reqURLs, url) + helper.reqURLs = append(helper.reqURLs, url) } } if conf.IPUrl != "" { - manager.reqURLs = append(manager.reqURLs, conf.IPUrl) + helper.reqURLs = append(helper.reqURLs, conf.IPUrl) } } else { // filter empty urls for _, url := range conf.IPV6Urls { if url != "" { - manager.reqURLs = append(manager.reqURLs, url) + helper.reqURLs = append(helper.reqURLs, url) } } if conf.IPV6Url != "" { - manager.reqURLs = append(manager.reqURLs, conf.IPV6Url) + helper.reqURLs = append(helper.reqURLs, conf.IPV6Url) } } +} - SafeGo(func() { - for { - manager.getCurrentIP() - time.Sleep(time.Second * time.Duration(conf.Interval)) +func GetIPHelperInstance(conf *settings.Settings) *IPHelper { + once.Do(func() { + helperInstance = &IPHelper{ + configuration: conf, + idx: -1, } + + helperInstance.loadConfiguration(conf) + + SafeGo(func() { + for { + helperInstance.getCurrentIP() + time.Sleep(time.Second * time.Duration(conf.Interval)) + } + }) }) - return manager + return helperInstance } func (helper *IPHelper) GetCurrentIP() string { diff --git a/pkg/lib/ip_helper_test.go b/pkg/lib/ip_helper_test.go index 28ee56c3..ae8a437a 100644 --- a/pkg/lib/ip_helper_test.go +++ b/pkg/lib/ip_helper_test.go @@ -4,12 +4,13 @@ import ( "testing" "github.com/TimothyYe/godns/internal/settings" + "github.com/TimothyYe/godns/pkg/lib" ) func TestGetCurrentIP(t *testing.T) { t.Skip() conf := &settings.Settings{IPUrls: []string{"https://myip.biturl.top"}} - helper := NewIPHelper(conf) + helper := lib.GetIPHelperInstance(conf) ip := helper.GetCurrentIP() if ip == "" {