当前位置:网站首页>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 .
边栏推荐
- Ubuntu20.04 PostgreSQL 14 installation configuration record
- I Brief introduction of radio energy transmission technology
- 6-3 vulnerability exploitation SSH environment construction
- Since I understand the idea of dynamic planning, I have opened the door to a new world
- How does schedulerx help users solve the problem of distributed task scheduling?
- uTools
- MySQL application day02
- Have you stepped on the nine common pits in the e-commerce system?
- 2022年6月国产数据库大事记
- Infiltration records of CFS shooting range in the fourth phase of the western regions' Dadu Mansion
猜你喜欢

II Basic structure of radio energy transmission system

Learning note 24 - multi sensor post fusion technology

Docker安装Oracle_11g

Infiltration records of CFS shooting range in the fourth phase of the western regions' Dadu Mansion

三分钟学会基础k线图知识

企业应该选择无服务器计算吗?

How to compress video size while adding watermark with one click?

Data visualization in medical and healthcare applications

电商系统中常见的9大坑,你踩过没?

How can I batch produce the same title for the video?
随机推荐
CEPH buffer yyds dry inventory
NeRV: Neural Reflectance and Visibility Fields for Relighting and View Synthesis
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
The concept and application of Cartland number
Design and implementation of radio energy transmission system
关于ASP.NET CORE使用DateTime日期类型参数的一个小细节
How to compress video size while adding watermark with one click?
教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
Global and Chinese market of safety detection systems 2022-2028: Research Report on technology, participants, trends, market size and share
6-2 vulnerability exploitation - inevitable problems of FTP
Shell Function
Global and Chinese markets of edge AI software 2022-2028: Research Report on technology, participants, trends, market size and share
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
10 minutes to get started quickly composition API (setup syntax sugar writing method)
2022年6月国产数据库大事记
遷移雲計算工作負載的四個基本策略
Hcip day 14 (MPLS protocol)
Learning note 3 -- Key Technologies of high-precision map (Part 1)
k线图形态这样记(口诀篇)