当前位置:网站首页>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 .
边栏推荐
- Learning notes 25 - multi sensor front fusion technology
- Global and Chinese markets for supply chain strategy and operation consulting 2022-2028: Research Report on technology, participants, trends, market size and share
- ACM教程 - 快速排序(常规 + 尾递归 + 随机基准数)
- Global and Chinese market of wireless charging magnetic discs 2022-2028: Research Report on technology, participants, trends, market size and share
- 技术大佬准备就绪,话题C位由你决定
- KS006基于SSM实现学生成绩管理系统
- LeetCode、3无重复最长子序列
- [image enhancement] vascular image enhancement based on frangi filter with matlab code
- I Brief introduction of radio energy transmission technology
- uTools
猜你喜欢

Single chip microcomputer -- hlk-w801 transplant NES simulator (III)

Leetcode, 3 repeatless longest subsequence

Liteos learning - first knowledge of development environment

Develop a simple login logic based on SSM

This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable

SAP ui5 beginner tutorial XXI - trial version of custom formatter of SAP ui5

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

GL Studio 5 installation and experience

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

Convolutional neural network (including code and corresponding diagram)
随机推荐
Edge extraction edges based on Halcon learning_ image. Hdev routine
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
Basic usage of three JS high-order functions --filter---map---reduce
Two TVs
Part 29 supplement (XXIX) basis of ECMAScript
Have you stepped on the nine common pits in the e-commerce system?
8.8.4-PointersOnC-20220215
10 minutes to get started quickly composition API (setup syntax sugar writing method)
The author is more willing to regard industrial Internet as a concept much richer than consumer Internet
Architecture evolution from MVC to DDD
No converter found for return value of type: class
【图像增强】基于Frangi滤波器实现血管图像增强附matlab代码
现货黄金分析的技巧有什么呢?
Appium inspector can directly locate the WebView page. Does anyone know the principle
学习笔记3--高精度地图关键技术(上)
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
Altium designer measure distance (ctrl+m)
Global and Chinese market of collaborative applications 2022-2028: Research Report on technology, participants, trends, market size and share
uTools