Skip to content

Commit

Permalink
Plug the memory leaks 🤞
Browse files Browse the repository at this point in the history
  • Loading branch information
anze3db committed Jul 14, 2023
1 parent 3f7ba17 commit 6a59d4b
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ type IP struct {

func main() {
verifyEnvVars()
ticker := time.NewTicker(60 * time.Second)
ticker := time.NewTicker(10 * time.Second)
client := &http.Client{
Timeout: 5 * time.Second,
}
api, err := cloudflare.NewWithAPIToken(CLOUDFLARE_API_TOKEN)
if err != nil {
return
}
for ; true; <-ticker.C {
log.Println("Verifying IPs")

ipCh, cfCh := fetchIP(), fetchCF()
ipCh, cfCh := fetchIP(client), fetchCF(api)
ipRes, cfRes := <-ipCh, <-cfCh

if cfRes.Error != nil {
Expand All @@ -55,7 +62,7 @@ func main() {
if cfRes.Result.Content != ipRes.Result {
log.Println("IPs do not match. Updating...")
cfRes.Result.Content = ipRes.Result
err := fixIp(cfRes.Result)
err := fixIp(cfRes.Result, api)
if err != nil {
log.Println(err)
} else {
Expand Down Expand Up @@ -91,15 +98,11 @@ Example call:
}
}

func fetchIP() <-chan Result {
func fetchIP(client *http.Client) <-chan Result {
ch := make(chan Result)
go func() {
defer close(ch)

client := http.Client{
Timeout: 5 * time.Second,
}

req, err := client.Get(IP_API_URL)
if err != nil {
ch <- Result{Result: "", Error: err}
Expand All @@ -108,34 +111,31 @@ func fetchIP() <-chan Result {

if req.StatusCode != 200 {
ch <- Result{Result: "", Error: errors.New("connection failed")}
return
}

defer req.Body.Close()

body, err := io.ReadAll(req.Body)
if err != nil {
ch <- Result{Result: "", Error: err}
return
}

var ip IP
json.Unmarshal(body, &ip)
body = nil
ch <- Result{Result: ip.Query, Error: nil}
}()

return ch
}

func fetchCF() <-chan CFResult {
func fetchCF(api *cloudflare.API) <-chan CFResult {
ch := make(chan CFResult)
go func() {
defer close(ch)

api, err := cloudflare.NewWithAPIToken(CLOUDFLARE_API_TOKEN)
if err != nil {
ch <- CFResult{Result: cloudflare.DNSRecord{}, Error: err}
return
}

zoneID, err := api.ZoneIDByName(ZONE_NAME)
if err != nil {
ch <- CFResult{Result: cloudflare.DNSRecord{}, Error: err}
Expand All @@ -150,6 +150,7 @@ func fetchCF() <-chan CFResult {
for _, record := range records {
if record.Name == DNS_RECORD_NAME {
ch <- CFResult{Result: record, Error: nil}
return
}
}

Expand All @@ -159,11 +160,7 @@ func fetchCF() <-chan CFResult {
return ch
}

func fixIp(record cloudflare.DNSRecord) error {
api, err := cloudflare.NewWithAPIToken(CLOUDFLARE_API_TOKEN)
if err != nil {
return err
}
func fixIp(record cloudflare.DNSRecord, api *cloudflare.API) error {
zoneID, err := api.ZoneIDByName(ZONE_NAME)
if err != nil {
return err
Expand Down

0 comments on commit 6a59d4b

Please sign in to comment.