当前位置:网站首页>Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
2022-07-02 01:29:00 【TSINGSEE】
As a developer , Be familiar with the characteristics of different languages 、 Flexible use of the combination of various languages is what developers need to consider .TSINGSEE The R & D personnel of Qingxi video are in the process of platform development , For intelligent analysis Python There will be more compilations , In part of the basic level call capacity, we use Golang More , It's used occasionally Java To do streaming programming .
Today, I want to share with you some practical experience in development : About Go Single instance mode of .
stay GO In design mode , One mode is singleton mode . Singleton mode is also called singleton mode , Is one of the commonly used patterns , It can ensure that only one instance of a class is created during the operation of the system .Go There are four ways to implement singleton mode in language , They are lazy 、 Hungry Chinese style 、 Double check and sync.Once.
Here's a simple one GO Singleton mode of implementation . The code is as follows :
type singleton struct{}
var instance = &singleton{}
func GetSingleton() *singleton {
return instance
}
Definition singleton A structure , And initialization instance object .GetSingleton() Function to get singleton Single instance object of this structure . This kind of singleton object is created when the package is loaded , The object will be created immediately . But it is not recommended in most cases .
If single instantiation , Too many initialization contents , It will take a long time for the program to load . We need to instance The initialization object of is moved to GetSingleton() Go to the function .
type singleton struct{}
var instance *singleton
func GetSingleton() *singleton {
if instance == nil {
instance = &singleton{}
}
return instance
}
Compared with the previous single case , This single example is in GetSingleton() Function internal call initialization instance Value . So the delay of the first call is GetSingleton() in .
This requires judgment instance == nil The situation of , But only judge nil Not very reliable . If there are multiple goroutine At the same time call ,GetSingleton() Concurrency security cannot be guaranteed .
In this case, you need to add a lock , If... Is called at the same time , This ensures concurrency security . The code reference is as follows :
import "sync"
type singleton struct{}
var instance *singleton
var mtx sync.Mutex
func GetSingleton() *singleton {
if instance == nil {
mtx.Lock()
defer mtx.Unlock()
if instance == nil {
instance = &singleton{}
}
}
return instance
}
As a family, it originated from EasyDarwin Open source framework for technology-based enterprises , In the development process , We are also happy to communicate code technology with all technology development enthusiasts 、 Share development experience , To provide you with some small reference .
TSINGSEE The platform of Qingxi video has strong scalability 、 High compatibility , It's easy to operate 、 Easy to integrate , There are a large number of landing applications in offline scenes . To facilitate the user to call 、 Integration and secondary development , Our platforms provide a wealth of API The interface is freely used by users . Interested users can go to the demonstration platform for experience or deployment testing .
边栏推荐
- Two TVs
- CEPH buffer yyds dry inventory
- Exclusive delivery of secret script move disassembly (the first time)
- error: . repo/manifests/: contains uncommitted changes
- Exclusive delivery of secret script move disassembly (the first time)
- Have you stepped on the nine common pits in the e-commerce system?
- Global and Chinese markets of digital crosspoint switches and mux/demux 2022-2028: Research Report on technology, participants, trends, market size and share
- Error creating bean with name ‘stringRedisTemplate‘ defined in class path re
- The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!
- Docker安装Oracle_11g
猜你喜欢
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
6-2漏洞利用-ftp不可避免的问题
Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
Liteos learning - first knowledge of development environment
MySQL application day02
Edge computing accelerates live video scenes: clearer, smoother, and more real-time
学习笔记3--高精度地图关键技术(上)
企业应该选择无服务器计算吗?
Datawhale community blackboard newspaper (issue 1)
k线图形态这样记(口诀篇)
随机推荐
[Chongqing Guangdong education] Tianshui Normal University universe exploration reference
卷积神经网络(包含代码与相应图解)
Learn C language from scratch day 025 (maze)
SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
Minimize the error
The concept and application of Cartland number
ACM tutorial - quick sort (regular + tail recursion + random benchmark)
Basic usage of shell script
Have you stepped on the nine common pits in the e-commerce system?
Leetcode, 3 repeatless longest subsequence
6-3 vulnerability exploitation SSH environment construction
Study note 2 -- definition and value of high-precision map
Modeling essays series 124 a simple coding method
机器学习基本概念
Game thinking 15: thinking about the whole region and sub region Services
6-2漏洞利用-ftp不可避免的问题
The author is more willing to regard industrial Internet as a concept much richer than consumer Internet
Android: the kotlin language uses grendao3, a cross platform app development framework
Exclusive delivery of secret script move disassembly (the first time)
ACM教程 - 快速排序(常规 + 尾递归 + 随机基准数)