golang copy-on-write思想应用

copy-on-write 是计算机领域相当经典的优化思想,当然你如果问一个Java 开发者 copy-on-write 有什么作用?他们往往第一反应就是:优雅地解决读多写少场景下的并发问题。

确实,众所周知,多线程环境下会出现 data race 的问题,我们以 Java 中的 ArrayList 为例,ArrayList 本身是不保证线程安全的,通常情况,要保证多线程环境下不出问题,就要给 ArrayList 加上读写锁,读要读锁,写要写锁,读与读之间不互斥,读与写之间要互斥,写与写之间也要互斥。

然而,对于读多写少的场景来说,如配置,服务降级数据,高并发下的localCache,频繁地读取必然导致频繁地加锁,而与写互斥的情况却很少出现,这似乎有点不『经济』,那么这时候基于copy-on-write的思想,我们在golang中用automic.value来实现

package main
 
import (
	"sync/atomic"
	"time"
)
 
func loadConfig() map[string]string {
	return make(map[string]string)
}
 
func requests() chan int {
	return make(chan int)
}
 
func main() {
	var config atomic.Value // holds current server configuration
	// Create initial config value and store into config.
	config.Store(loadConfig())
	go func() {
		// Reload config every 10 seconds
		// and update config value with the new version.
		for {
			time.Sleep(10 * time.Second)
			config.Store(loadConfig())
		}
	}()
	// Create worker goroutines that handle incoming requests
	// using the latest config value.
	for i := 0; i < 10; i++ {
		go func() {
			for r := range requests() {
				c := config.Load()
				// Handle request r using config c.
				_, _ = r, c
			}
		}()
	}

下面的示例展示了如何使用写时复制习惯用法维护可伸缩、频繁读取但不频繁更新的数据结构。copy-on-write  适合读多写少的场景。

 

package main
 
import (
	"sync"
	"sync/atomic"
)
 
func main() {
	type Map map[string]string
	var m atomic.Value
	m.Store(make(Map))
	var mu sync.Mutex // used only by writers
	// read function can be used to read the data without further synchronization
	read := func(key string) (val string) {
		m1 := m.Load().(Map)
		return m1[key]
	}
	// insert function can be used to update the data without further synchronization
	insert := func(key, val string) {
		mu.Lock() // synchronize with other potential writers
		defer mu.Unlock()
		m1 := m.Load().(Map) // load current value of the data structure
		m2 := make(Map)      // create a new value
		for k, v := range m1 {
			m2[k] = v // copy all data from the current object to the new one
		}
		m2[key] = val // do the update that we need
		m.Store(m2)   // atomically replace the current object with the new one
		// At this point all new readers start working with the new version.
		// The old version will be garbage collected once the existing readers
		// (if any) are done with it.
	}
	_, _ = read, insert
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值