I have a long running hobby program with a code:
var (
url = "https://example.com"
lastBody []byte
)
func get(client *http.Client, dir) (changed bool, data []byte, err error) { resp, err := client.Get(url)
if err != nil {
return false, nil, err
}
if resp.StatusCode != http.StatusOK {
log.Printf("status code: %d", resp.StatusCode)
return false, nil, nil
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, nil, err
}
if bytes.Compare(body, lastBody) == 0 {
logrus.Info("data is equal")
return false, data, nil
}
lastBody = body
log.Printf("got %dB data", len(body))
dir = path.Join(dir, t.Format("2006/01/02"))
if err = os.MkdirAll(dir, defaultDirPerm); err != nil {
log.Print("failed to create a dir")
}
p := path.Join(dir, fmt.Sprintf("%d.json", t.Unix()))
return true, body, ioutil.WriteFile(p, body, 0640)
}
After around 14 days the process stops writing files. However, it executes os.WriteFile and does not return any error.
The function is called repeatedly every N second (~40-60sec.). It produces around 1700 files a day.