当前位置:网站首页>GO开发:如何利用Go单例模式保障流媒体高并发的安全性?
GO开发:如何利用Go单例模式保障流媒体高并发的安全性?
2022-07-04 14:58:00 【游戏编程】
作为开发者,熟知不同语言的特性、灵活运用各种语言的结合都是开发者需要考虑的内容。TSINGSEE青犀视频的研发人员在平台开发过程中,智能分析方面用Python编译会比较多,在部分基层调用能力上则采用Golang比较多,偶尔也会用到Java来做流式编程。

今天和大家分享一点开发中的实践经验:关于Go的单例模式。
在GO设计模式中,有一种模式为单例模式。单例模式也叫单子模式,是常用的模式之一,它能够保证系统运行中一个类只创建一个实例。Go语言实现单例模式有四种方式,分别是懒汉式、饿汉式、双重检查和sync.Once。

下面是一个简单的GO实现的单例模式。代码如下:
type singleton struct{}var instance = &singleton{}func GetSingleton() *singleton { return instance} 定义singleton一个结构体,并初始化instance对象。GetSingleton()函数可获取singleton这个结构体的单实例对象。这种创建单例对象是在包加载时,就会立即创建对象。但是大多数情况下不推荐使用。
如果单实例化时,初始化内容过多,则会造成程序加载的时间比较长。我们需要将instance的初始化对象移动到GetSingleton()函数里去。
type singleton struct{}var instance *singletonfunc GetSingleton() *singleton { if instance == nil { instance = &singleton{} } return instance} 相比较上一种单例,这个单例是在GetSingleton()函数内部调用初始化instance的值。所以第一次调用的延迟到了GetSingleton()中。
这种需要判断instance == nil的情况,但是仅判断nil并不十分可靠。如果存在多个goroutine同时调用,GetSingleton()则无法保证并发安全。
那么这种情况就需要加一个锁,如果同时调用,这样就能保证并发安全。代码参考如下:
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} 作为一家起源于EasyDarwin开源框架的技术型企业,在开发过程中,我们也乐于和所有的技术开发爱好者们一起交流代码技术、分享开发经验,为大家提供一些小小的参考。
TSINGSEE青犀视频的平台可拓展性强、兼容性高,操作简单、易于集成,在线下场景中均有大量落地应用。为了便于用户调用、集成与二次开发,我们的平台均提供了丰富的API接口供用户自由使用。感兴趣的用户可以前往演示平台进行体验或部署测试。
作者:Black_3717
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- Market trend report, technical innovation and market forecast of China's hair repair therapeutic apparatus
- L1-072 scratch lottery
- 基于check-point实现图数据构建任务
- QT graphical view frame: element movement
- Using celery in projects
- [North Asia data recovery] a database data recovery case where the disk on which the database is located is unrecognized due to the RAID disk failure of HP DL380 server
- Principle and general steps of SQL injection
- 高度剩余法
- Will the memory of ParticleSystem be affected by maxparticles
- Interface fonctionnelle, référence de méthode, Widget de tri de liste implémenté par lambda
猜你喜欢

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

函數式接口,方法引用,Lambda實現的List集合排序小工具

A trap used by combinelatest and a debouncetime based solution

多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
![[North Asia data recovery] a database data recovery case where the disk on which the database is located is unrecognized due to the RAID disk failure of HP DL380 server](/img/79/3fab19045e1ab2f5163033afaa4309.jpg)
[North Asia data recovery] a database data recovery case where the disk on which the database is located is unrecognized due to the RAID disk failure of HP DL380 server

Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database

Opencv learning -- geometric transformation of image processing

Filtered off site request to

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

Game theory
随机推荐
QT graphical view frame: element movement
The vscode waveform curve prompts that the header file cannot be found (an error is reported if the header file exists)
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
SQL implements split
力扣今日题-1200. 最小绝对差
How to contribute to the source code of ongdb core project
Using celery in projects
程序员怎么才能提高代码编写速度?
Change the mouse pointer on ngclick - change the mouse pointer on ngclick
[Chongqing Guangdong education] National Open University spring 2019 1248 public sector human resource management reference questions
Median and order statistics
Ten clothing stores have nine losses. A little change will make you buy every day
中位数与次序统计量
[North Asia data recovery] a database data recovery case where the disk on which the database is located is unrecognized due to the RAID disk failure of HP DL380 server
同构图与异构图CYPHER-TASK设计与TASK锁机制
最大子数组与矩阵乘法
Implement graph data construction task based on check point
C # realizes FFT forward and inverse transformation and frequency domain filtering
Object.keys()的用法
What should ABAP do when it calls a third-party API and encounters garbled code?