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,如果觉得 ...
随机推荐
- python 多线程模板简单实现
#-*- encoding: UTF-8 -*- #编码声明 import threading,Queue,os import time #导入方法模块 def main(inargs): work_ ...
- Python机器学习基础教程-第2章-监督学习之决策树
前言 本系列教程基本就是摘抄<Python机器学习基础教程>中的例子内容. 为了便于跟踪和学习,本系列教程在Github上提供了jupyter notebook 版本: Github仓库: ...
- 配置MySQL,使其与PyCharm相关联
在配置MySQL和PyCharm时,经常出现这样的错误提示: Connection to base@localhost failed. [08001] Could not create connect ...
- vue引入iconfont报错
参考链接:https://blog.csdn.net/weixin_37215881/article/details/89237213
- string中的erase()函数
erase()是对string类型的字符串进行删除元素操作的函数 1.erase(int index) 删除下标从index开始直到字符串结尾的元素 1 string s = "123215 ...
- R画柱形图和箱线图
数据格式如下 gene_id Sham-1 Sham-2 Sham-3 Sham-4 Sham-5 Rep-1h-1 Rep-1h-2 Rep-1h-3 Rep-1h-4 Rep-1h-5 Rep-3 ...
- TCP/IP学习笔记8--数据链路之基本概念
"在你生命的最初30年中,你养成习惯:在你生命的最后30年中,你的习惯决定了你."---- Steve Jobs TCP/IP对于OSI参考模型的数据链路成及以下部分(物理层)没有 ...
- PHP被忽略的基础知识
目录 下列PHP配置项中,哪一个和安全最不相关:() 字符串比较函数 格林时间 在PHP面向对象中,下面关于final修饰符描述错误的是( ) getdate()函数返回的值的数据类型是:( ) 关于 ...
- PAT(B) 1079 延迟的回文数(Java)
题目链接:1079 延迟的回文数 (20 point(s)) 题目描述 给定一个 k+1 位的正整数 N,写成 ak⋯a1a0 的形式,其中对所有 i 有 0≤ai<10 ...
- 算术 HDU - 6715 (莫比乌斯反演)
大意: 给定$n,m$, 求$\sum\limits_{i=1}^n\sum\limits_{j=1}^m\mu(lcm(i,j))$ 首先有$\mu(lcm(i,j))=\mu(i)\mu(j)\m ...