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,如果觉得 ...
随机推荐
- 【嵌入式硬件Esp32】Ubuntu18.04 更换阿里云软件源
使用Ubuntu 的apt-get来安装软件是总是因为官方源的速度太慢而抓狂. 但是用阿里云的源就很快,下面总结一下如何更换Ubuntu的软件源. 一.备份sudo cp /etc/apt/sourc ...
- 【GStreamer开发】GStreamer播放教程09——数字音频传输
目标 本教程展示GStreamer是如何进行数字音频传输的. 介绍 在常见的模拟格式外,高端的音频系统通常都接受数字格式,压缩的非压缩的都能接受.因为音频信号是从电脑传到音箱,用一种更有弹性的形态会更 ...
- confluence6.14.1linux安装破解
一.简介 Confluence为团队提供一个协作环境.在这里,团队成员齐心协力,各擅其能,协同地编写文档和管理项目.从此打破不同团队.不同部门以及个人之间信息孤岛的僵局,Confluence真正实现了 ...
- twemproxy配置
redis多主从,多节点,读写分离架构. nutcracker.yml的twemproxy配置 #redis_main是twemproxy所控制redis主从集群逻辑名称 redis_main: #t ...
- 转:如何正确彻底删除webpack 全局或是局部?
原文链接:如何正确彻底删除webpack 全局或是局部? 就需要卸载cli 配置webpack ,提示需要安装 webpack-cli,选择yes 后安装webpack-cli , 可是问题不断,所以 ...
- Python27之集合
集合说:“在我的世界里,你就是唯一” 一.集合的概念和使用 集合的概念和数学里数学里集合的概念是一致的,都是一组元素的集,且元素之间不能重复.元素必须是不可变的数据类型,例如元组也可以作为其中的一个元 ...
- Pycharm(Eclipse)常用快捷键
在File_Settings_Keymap中可以设置: 确定快捷键模式为Eclipse 看方法的源码:ctrl+鼠标左键 回退之前代码:alt+左键 前进之前代码:alt+右键 调换相邻两行代码位置: ...
- python 之 面向对象基础(继承与派生,经典类与新式类)
7.2 继承与派生 7.21继承 1.什么是继承? 继承是一种新建类的的方式,在python中支持一个子类继承多个父类.新建的类称为子类或者派生类,父类又可以称为基类或者超类,子类会”遗传“父类的属性 ...
- 【已解决】每次打开Excel时会同时打开一个空的Excel表格
每次打开Excel时会同时打开一个空的Excel表格,情况如图. 官方解法如下,本人验证有效: 方法1, 请到以后路径中检查是否存在与空白文件夹同名字的Excel文件,删除它. C:\Users\\A ...
- Comet OJ Contest 4
A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 ...