Skip to content

Commit 02274a2

Browse files
committed
eliminate memory leak on http calls
1 parent bfc23db commit 02274a2

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

main.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,27 +101,31 @@ func writeKeys(keys []string) {
101101
}
102102
}
103103

104+
func updateKeys() {
105+
var keyMap Map
106+
resp, err := http.Get(uri)
107+
defer func() {
108+
io.Copy(ioutil.Discard, resp.Body)
109+
resp.Body.Close()
110+
}()
111+
if err != nil {
112+
log.Printf("%v", err)
113+
return
114+
}
115+
dec := json.NewDecoder(resp.Body)
116+
err = dec.Decode(&keyMap)
117+
if err != nil {
118+
log.Printf("%v", err)
119+
return
120+
}
121+
keys := keysFromMap(&keyMap)
122+
writeKeys(keys)
123+
}
124+
104125
func sync() {
105126
intrv, _ := strconv.Atoi(interval)
106127
for t := time.Tick(time.Second * time.Duration(intrv)); ; <-t {
107-
var keyMap Map
108-
resp, err := http.Get(uri)
109-
defer func() {
110-
io.Copy(ioutil.Discard, resp.Body)
111-
resp.Body.Close()
112-
}()
113-
if err != nil {
114-
log.Printf("%v", err)
115-
continue
116-
}
117-
dec := json.NewDecoder(resp.Body)
118-
err = dec.Decode(&keyMap)
119-
if err != nil {
120-
log.Printf("%v", err)
121-
continue
122-
}
123-
keys := keysFromMap(&keyMap)
124-
writeKeys(keys)
128+
updateKeys()
125129
}
126130
}
127131

0 commit comments

Comments
 (0)