I've made a small web server that creates and updates calendar events. However, I'm a bit uncertain about how I'm supposed to use the calendar api library service. At the moment I'm creating a new one each time the web server accepts a request, like so:
ctx := context.Background()
srv, err := calendar.NewService(ctx,option.WithTokenSource(config.TokenSource(ctx)))
I suspect I'm probably supposed to reuse this service, but I can't find any documentation about this. Is it safe to reuse? Does it get discarded automatically when no longer in use (i.e. garbage collection)?
You probably want to use req.Context()
(where req
is your *http.Request
) instead of context.Background()
. In this case, you have to create a new service object each time, and they will be garbage collected as appropriate.
The only reason you would want to use context.Background()
is if you're asynchronously issuing the calendar API requests (eg, in a separate goroutine) and they might not finish before the HTTP request finishes. In this case, you could use a single shared service object if you wanted.