http.HandleFunc and updated variables

Hello,

I am currently running into a problem I can’t seem to find a solution for. I am sure it is possible, just not how.

My code is an API client that fetches data successfully from some third-party server. I have a function which continually updates the required AccessToken, something like this:

    go refresh_scheduler(mycreds, mytokens, refchan)
    for newtokens := range refchan {
            mytokens.AccessToken = newtokens.AccessToken
            mytokens.RefreshToken = newtokens.RefreshToken
    }

So, using a channel, my main() function regularly receives updated tokens, which works fine.

Now I want to expose the data fetched via HTTP, so I set up a very simple HandleFunc like this:

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { data_overview(w, r, mytokens, localsid) } )

As soon as the program starts, calling the base URL gives me valid data. However, after a few minutes, the mytokens struct is updated (via the channel), but the HandleFunc still uses the old value, so calls fail.

I already tried “updating” the HandleFunc inside the for loop, but that doesn’t seem to do anything. It seems to just take mytokens at initialization time and that’s it.

Any pointers and comments are appreciated.

Kind regards,
Lordy

Ok, I have managed to figure this out myself.

There were actually two problems. The first one being that http.ListenAndServer is a blocking call which prevented other code from being executed.
The second problem was, that I obviously forgot about pointers. With pointers, reference to the updated tokens gets passed along fine.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.