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")
>    }
>
>
Here, if os.MkdirAll fails, the function should return with an error.

   p := path.Join(dir, fmt.Sprintf("%d.json", t.Unix()))
>
>    return true, body, ioutil.WriteFile(p, body, 0640)
> }
>
>

Also, you should probably pass lastBody as a function argument, and not as 
a global variable.
The variable t is also not defined in the code you posted.
 

>  
>
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.
>
>
The problem may be triggered when the code fails to create a new directory; 
however WriteFile should return an error in this case.

> [...]

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