Run Code Once on First Load (Concurrency Safe)
原文: https://golangcode.com/run-code-once-with-sync/
---------------------------------------------------
Using a web server as an example, there are multiple stages you can load resources. Within the main() function and within the handler are the obvious two - each with their own advantages and disadvantages. Within the main function can hinder the start-up time of the server, while code within the handler is run on every request.
Sometimes we want to load a resource only once and when it’s first needed. This means it needs to be in the http handler. We have to be careful how we do this though, as each time the handler is called it will be in a separate goroutine. We could use a global variable and if it hasn’t been set (or == nil), load the resource - but this won’t be safe from a concurrency point of view as you could end up running the load sequence twice.
The best way to achieve this is therefore using the sync.Once mutex to efficiently run code once even across goroutines. The example below should demonstrate this.
package main
import (
"fmt"
"sync"
)
var doOnce sync.Once
func main() {
DoSomething()
DoSomething()
}
func DoSomething() {
doOnce.Do(func() {
fmt.Println("Run once - first time, loading...")
})
fmt.Println("Run this every time")
}
Screenshot of without and with the discard option:

Run Code Once on First Load (Concurrency Safe)的更多相关文章
- Specifying the Code to Run on a Thread
This lesson shows you how to implement a Runnable class, which runs the code in its Runnable.run() m ...
- Instant Run
http://tools.android.com/tech-docs/instant-run N Developer Preview users: Instant Run is currently i ...
- $( document ).ready()&$(window).load()
$( document ).ready() https://learn.jquery.com/using-jquery-core/document-ready/ A page can't be man ...
- (4)事件处理——(3)代码执行的顺序(Timing of code execution)
In Chapter 1, Getting Started, we noted that $(document).ready()was jQuery's primary way to perform ...
- Run Nutch In Eclipse on Linux and Windows nutch version 0.9
Running Nutch in Eclipse Here are instructions for setting up a development environment for Nutch un ...
- How to Debug Enterprise Portal Code in Dynamics AX 2009
转载 To set up debugging for pages1. Log into the server that is running the AOS.2. Open the Microsoft ...
- Thread的run()与start()的区别
Java的线程是通过java.lang.Thread类来实现的.VM启动时会有一个由主方法所定义的线程.可以通过创建Thread的实例来创建新的线程.每个线程都是通过某个特定Thread对象所对应的方 ...
- Thread’s start method and run method
工作中用到了Thread,一开始用错了,仔细研究了一下,稍作整理. 前言,今天写代码居然这样写的 new Thread() { @Override public void run() { System ...
- 细说OC中的load和initialize方法
OC中有两个特殊的类方法,分别是load和initialize.本文总结一下这两个方法的区别于联系.使用场景和注意事项.Demo可以在我的Github上找到--load和initialize,如果觉得 ...
随机推荐
- mongodb 内嵌数组查询问题: 如何限定返回与条件匹配的数组
原文地址:https://segmentfault.com/q/1010000002943721
- InstallerProjects打包
C#—使用InstallerProjects打包桌面应用程序 前言 打包桌面应用程序实在是一个不常使用的东西,偶尔使用起来经常会忘东忘西的耽误时间,因此,这篇文章多以图片记录过程,也是用于备忘. ...
- NLP | 算法 学习资料整理
UPDATE TIME: 2019-12-12 17:06:32 NLP: 对话系统: [ ] https://www.cnblogs.com/jiangxinyang/p/10789512.html ...
- 代理工具WebScarab安装(转载)
原文地址:https://blog.csdn.net/shiyuqing1207/article/details/46428443 2015年06月09日 16:31:52 shiyuqing1207 ...
- linux input子系统详解
首先,什么是linux的子系统: 输入子系统由驱动层.输入子系统核心.事件处理层三部分组成.一个输入事件,如鼠标移动通过Driver->Input core->Event handler- ...
- [转帖]AMD:Zen 2霄龙处理器每美元性能可达英特尔至强5.6倍
AMD:Zen 2霄龙处理器每美元性能可达英特尔至强5.6倍 2019-10-20 6:35:38来源:IT之家作者:孤城责编:孤城评论:32 https://www.ithome.com/0/451 ...
- (二)linux 学习 -- 探究操作系统
The Linux Command Line 读书笔记 - 部分内容来自 http://billie66.github.io/TLCL/book/chap04.html 文章目录 ls 命令进阶 `l ...
- (零)linux 学习 -- 从 shell 开始
The Linux Command Line 读书笔记 - 部分内容来自 http://billie66.github.io/TLCL/book/chap02.html 文章目录 前言 什么是 she ...
- I2C基础及时序
1.模式 标准模式:达到100Kb/S 快速模式:达到400Kb/S 2.连接图 3.协议 SDA.SCL在空闲的时候为高电平 重点!重点!重点! 4.涉及到多主机仲裁的竞争及时钟信号的同步
- hdu 2586 欧拉序+rmq 求lca
题意:求树上任意两点的距离 先说下欧拉序 对这颗树来说 欧拉序为 ABDBEGBACFHFCA 那欧拉序有啥用 这里先说第一个作用 求lca 对于一个欧拉序列,我们要求的两个点在欧拉序中的第一个位置之 ...