当前位置:网站首页>Go development: how to use go singleton mode to ensure the security of high concurrency of streaming media?
Go development: how to use go singleton mode to ensure the security of high concurrency of streaming media?
2022-07-04 16:52:00 【Game programming】
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 *singletonfunc 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 *singletonvar mtx sync.Mutexfunc 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 .
author :Black_3717
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- Four point probe Industry Research Report - market status analysis and development prospect prediction
- Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
- Final consistency of MESI cache in CPU -- why does CPU need cache
- What does IOT engineering learn and work for?
- std::shared_ ptr initialization: make_ shared< Foo> () vs shared_ ptr< T> (new Foo) [duplicate]
- 线程池的使用和原理
- Model fusion -- stacking principle and Implementation
- Research Report on market supply and demand and strategy of China's four sided flat bag industry
- .Net 应用考虑x64生成
- I let the database lock the table! Almost fired!
猜你喜欢
[North Asia data recovery] a database data recovery case where the partition where the database is located is unrecognized due to the RAID disk failure of HP DL380 server
Object.keys()的用法
DIY a low-cost multi-functional dot matrix clock!
Opencv learning -- geometric transformation of image processing
从数数开始
Vscode setting outline shortcut keys to improve efficiency
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
Working group and domain analysis of Intranet
~88 running people practice
Communication mode based on stm32f1 single chip microcomputer
随机推荐
Using celery in projects
Qt---error: ‘QObject‘ is an ambiguous base of ‘MyView‘
Cypher task design and task locking mechanism of isomorphic and heterogeneous graphs
如何为ONgDB核心项目源码做贡献
Proxifier global agent software, which provides cross platform port forwarding and agent functions
Go语言循环语句(第10课下)
Research Report on market supply and demand and strategy of surgical stapler industry in China
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
Implement graph data construction task based on check point
Socks agent tools earthworm, ssoks
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
Practice: fabric user certificate revocation operation process
Research Report on market supply and demand and strategy of China's four sided flat bag industry
Accounting regulations and professional ethics [6]
Sql实现Split
Research Report of exoskeleton robot industry - market status analysis and development prospect prediction
Working group and domain analysis of Intranet
Application of clock wheel in RPC
Anta is actually a technology company? These operations fool netizens
C language: implementation of daffodil number function