doulu6929 2019-05-23 16:31
浏览 2
已采纳

我应该为日历API重用服务,还是可以安全地按需创建新服务?

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)?

  • 写回答

1条回答 默认 最新

  • dowjgrm6787 2019-05-26 10:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂