当前位置:网站首页>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 .
边栏推荐
- China's plastic processing machinery market trend report, technological innovation and market forecast
- C implementation defines a set of intermediate SQL statements that can be executed across libraries
- SQL implements split
- Cypher task design and task locking mechanism of isomorphic and heterogeneous graphs
- FIREBIRD使用经验总结
- Li Kou today's question -1200 Minimum absolute difference
- Common knowledge of unity Editor Extension
- 函數式接口,方法引用,Lambda實現的List集合排序小工具
- Why do you say that the maximum single table of MySQL database is 20million? Based on what?
- Go语言循环语句(第10课下)
猜你喜欢

DC-2靶场搭建及渗透实战详细过程(DC靶场系列)

overflow:auto与felx结合的用法

D3D11_ Chili_ Tutorial (2): draw a triangle

~88 running people practice

Interface fonctionnelle, référence de méthode, Widget de tri de liste implémenté par lambda

Vscode setting outline shortcut keys to improve efficiency

What should ABAP do when it calls a third-party API and encounters garbled code?

Position encoding practice in transformer

165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version

Overflow: the combination of auto and Felx
随机推荐
Working group and domain analysis of Intranet
Research Report on market supply and demand and strategy of China's plastics and polymer industry
Understand asp Net core - Authentication Based on jwtbearer
js中的数组筛选fliter
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
ONgDB图数据库与Spark的集成
Configuration instance of Oracle listener server and client
How to contribute to the source code of ongdb core project
力扣今日题-1200. 最小绝对差
AutoCAD - set color
Integration of ongdb graph database and spark
PR FAQ: how to set PR vertical screen sequence?
The content of the source code crawled by the crawler is inconsistent with that in the developer mode
error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
Accounting regulations and professional ethics [6]
@EnableAspectAutoJAutoProxy_ Exposeproxy property
Vscode setting outline shortcut keys to improve efficiency
DC-2靶场搭建及渗透实战详细过程(DC靶场系列)
Application of clock wheel in RPC
实战:fabric 用户证书吊销操作流程