当前位置:网站首页>开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
2022-07-02 01:28:00 【TSINGSEE】
作为开发者,熟知不同语言的特性、灵活运用各种语言的结合都是开发者需要考虑的内容。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 *singleton
func GetSingleton() *singleton {
if instance == nil {
instance = &singleton{}
}
return instance
}
相比较上一种单例,这个单例是在GetSingleton()函数内部调用初始化instance的值。所以第一次调用的延迟到了GetSingleton()中。
这种需要判断instance == nil的情况,但是仅判断nil并不十分可靠。如果存在多个goroutine同时调用,GetSingleton()则无法保证并发安全。
那么这种情况就需要加一个锁,如果同时调用,这样就能保证并发安全。代码参考如下:
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
}
作为一家起源于EasyDarwin开源框架的技术型企业,在开发过程中,我们也乐于和所有的技术开发爱好者们一起交流代码技术、分享开发经验,为大家提供一些小小的参考。
TSINGSEE青犀视频的平台可拓展性强、兼容性高,操作简单、易于集成,在线下场景中均有大量落地应用。为了便于用户调用、集成与二次开发,我们的平台均提供了丰富的API接口供用户自由使用。感兴趣的用户可以前往演示平台进行体验或部署测试。
边栏推荐
- 机器学习基本概念
- 迁移云计算工作负载的四个基本策略
- Leetcode, 3 repeatless longest subsequence
- Android: the kotlin language uses grendao3, a cross platform app development framework
- Shell Function
- KS006基于SSM实现学生成绩管理系统
- Datawhale 社区黑板报(第1期)
- Global and Chinese market of collaborative applications 2022-2028: Research Report on technology, participants, trends, market size and share
- II Basic structure of radio energy transmission system
- How can I batch produce the same title for the video?
猜你喜欢
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
[image enhancement] vascular image enhancement based on frangi filter with matlab code
The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!
Finally got byte offer, 25-year-old inexperienced experience in software testing, to share with you
Unity AssetBundle subcontracting
Hcip day 14 (MPLS protocol)
Introduction to ffmpeg Lib
Edge extraction edges based on Halcon learning_ image. Hdev routine
Sql--- related transactions
[Maya] the error of importing Maya into Metahuman
随机推荐
How does schedulerx help users solve the problem of distributed task scheduling?
Global and Chinese markets for the application of artificial intelligence in security, public security and national security 2022-2028: Research Report on technology, participants, trends, market size
ACM tutorial - quick sort (regular + tail recursion + random benchmark)
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
企业应该选择无服务器计算吗?
Study note 2 -- definition and value of high-precision map
Variables and constants of go language foundation
Global and Chinese markets for freight and logistics 2022-2028: Research Report on technology, participants, trends, market size and share
Android high frequency network interview topic must know and be able to compare Android development environment
电商系统中常见的9大坑,你踩过没?
6-3 vulnerability exploitation SSH environment construction
Android: the kotlin language uses grendao3, a cross platform app development framework
970 golang realizes the communication between multithreaded server and client
How does schedulerx help users solve the problem of distributed task scheduling?
Learn C language from scratch day 025 (maze)
Circular statements in shell programming
CTF daily question day45 sensor
Part 29 supplement (XXIX) basis of ECMAScript
Brief introduction to the development of mobile network
微信小程序中使用tabBar