On Friday, August 24, 2018 at 11:20:46 AM UTC+2, Paweł Szczur wrote:
>
> Hi,
>
> 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.
>
> Any ideas?
>
>
If files are created but are empty, there is a very simple explanation: the 
body of the GET response is empty.
Check if resp.ContentLength is equal to 0.

Also, if resp.StatusCode is not StatusOK, I suggest to return a not nil 
error.


Manlio

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to