当前位置:网站首页>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 .
边栏推荐
- ACM tutorial - quick sort (regular + tail recursion + random benchmark)
- GL Studio 5 安装与体验
- Self drawing of menu items and CListBox items
- Liteos learning - first knowledge of development environment
- ECS project deployment
- [IVX junior engineer training course 10 papers] 06 database and services
- How does schedulerx help users solve the problem of distributed task scheduling?
- Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
- Global and Chinese market of wireless charging magnetic discs 2022-2028: Research Report on technology, participants, trends, market size and share
- k线图形态这样记(口诀篇)
猜你喜欢

Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model

The pain of Xiao Sha

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

学习笔记25--多传感器前融合技术
![[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production](/img/dc/e9adb1b41c2175c6f18d8027e0530a.jpg)
[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production

学习笔记24--多传感器后融合技术

Exclusive delivery of secret script move disassembly (the first time)

The technology boss is ready, and the topic of position C is up to you

6-2漏洞利用-ftp不可避免的问题

LeetCode、3无重复最长子序列
随机推荐
Global and Chinese market of collaborative applications 2022-2028: Research Report on technology, participants, trends, market size and share
Luogu p1775 stone merger (weakened version)
970 golang realizes the communication between multithreaded server and client
Learn about servlets
6-2 vulnerability exploitation - inevitable problems of FTP
What is commercial endowment insurance? Is commercial endowment insurance safe?
SQL injection for Web Security (2)
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
CEPH buffer yyds dry inventory
Edge computing accelerates live video scenes: clearer, smoother, and more real-time
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
三分钟学会基础k线图知识
6-3漏洞利用-SSH环境搭建
【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
Docker installing Oracle_ 11g
Global and Chinese market of wireless charging magnetic discs 2022-2028: Research Report on technology, participants, trends, market size and share
技术大佬准备就绪,话题C位由你决定
Based on Simulink and FlightGear, the dynamic control of multi rotor UAV in equilibrium is modeled and simulated