当前位置:网站首页>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 .
边栏推荐
- Research Report on surgical otorhinolaryngology equipment industry - market status analysis and development prospect prediction
- Array filter fliter in JS
- Principle and general steps of SQL injection
- 科普达人丨一文看懂阿里云的秘密武器“神龙架构”
- 高度剩余法
- Why do you say that the maximum single table of MySQL database is 20million? Based on what?
- std::shared_ ptr initialization: make_ shared< Foo> () vs shared_ ptr< T> (new Foo) [duplicate]
- Change the mouse pointer on ngclick - change the mouse pointer on ngclick
- Object.keys()的用法
- JS to realize the countdown function
猜你喜欢

@EnableAspectAutoJAutoProxy_ Exposeproxy property

Anta is actually a technology company? These operations fool netizens

What is torch NN?

Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)

Principle and general steps of SQL injection

Object.keys()的用法

Software Engineer vs Hardware Engineer

What is torch NN?

Using celery in projects
Application of clock wheel in RPC
随机推荐
Communication mode based on stm32f1 single chip microcomputer
ONgDB图数据库与Spark的集成
实战:fabric 用户证书吊销操作流程
话里话外:流程图绘制初级:六大常见错误
Socks agent tools earthworm, ssoks
Research Report on market supply and demand and strategy of surgical stapler industry in China
Final consistency of MESI cache in CPU -- why does CPU need cache
China's plastic processing machinery market trend report, technological innovation and market forecast
Accounting regulations and professional ethics [7]
Anta is actually a technology company? These operations fool netizens
Laravel simply realizes Alibaba cloud storage + Baidu AI Cloud image review
Redis: SDS source code analysis
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
Principle and general steps of SQL injection
Software Engineer vs Hardware Engineer
Research Report on surgical otorhinolaryngology equipment industry - market status analysis and development prospect prediction
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
Application of clock wheel in RPC
Oracle监听器Server端与Client端配置实例