当前位置:网站首页>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 .
边栏推荐
- 2022年6月国产数据库大事记
- Docker installing Oracle_ 11g
- Day 13 of hcip (relevant contents of BGP agreement)
- Introduction to ffmpeg Lib
- Appium inspector can directly locate the WebView page. Does anyone know the principle
- Global and Chinese market of wireless charging magnetic discs 2022-2028: Research Report on technology, participants, trends, market size and share
- Error creating bean with name ‘stringRedisTemplate‘ defined in class path re
- MySQL application day02
- uTools
- 三分钟学会基础k线图知识
猜你喜欢

卷積神經網絡(包含代碼與相應圖解)

关于ASP.NET CORE使用DateTime日期类型参数的一个小细节

Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories
![[image enhancement] vascular image enhancement based on frangi filter with matlab code](/img/b3/b4164fb7db8645f470180e352b5717.png)
[image enhancement] vascular image enhancement based on frangi filter with matlab code

How does schedulerx help users solve the problem of distributed task scheduling?

卷积神经网络(包含代码与相应图解)
![[IVX junior engineer training course 10 papers to get certificates] 0708 news page production](/img/ad/a1cb672d2913b6befd6d8779c993ec.jpg)
[IVX junior engineer training course 10 papers to get certificates] 0708 news page production

Basic concepts of machine learning

We should make clear the branch prediction
![[IVX junior engineer training course 10 papers] 06 database and services](/img/68/967566fc2f1d0b93ecd78bdb208b64.jpg)
[IVX junior engineer training course 10 papers] 06 database and services
随机推荐
Cat Party (Easy Edition)
Just using the way and method of consuming the Internet to land and practice the industrial Internet will not bring long-term development
Two TVs
Architecture evolution from MVC to DDD
成功实现边缘编码需要了解的六大经验教训
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
Global and Chinese market of ancillary software 2022-2028: Research Report on technology, participants, trends, market size and share
Self drawing of menu items and CListBox items
Infiltration records of CFS shooting range in the fourth phase of the western regions' Dadu Mansion
Data visualization in medical and healthcare applications
MPLS experiment operation
Develop a simple login logic based on SSM
Android high frequency network interview topic must know and be able to compare Android development environment
[image enhancement] vascular image enhancement based on frangi filter with matlab code
Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model
Liteos learning - first knowledge of development environment
ACM tutorial - quick sort (regular + tail recursion + random benchmark)
MySQL application day02
CEPH buffer yyds dry inventory
I'll teach you to visit Amazon RDS for a year and build a MySQL cloud database (only 10 minutes, really fragrant)