图解+仿写 新手都能学懂的SpringBoot源码课

## download:图解+仿写 新手都能学懂的SpringBoot源码课

假如我们做光阴机回到 Go 1.18 以至一路追溯到 Go 1.4 版本,你会发现 atomic 包固然提供了很多函数,但只要一个 Type,那就是 atomic.Value。这一节我们回忆一下 atomic.Value 提供的才能。

A Value provides an atomic load and store of a consistently typed value. The zero value for a Value returns nil from Load. Once Store has been called, a Value must not be copied.
A Value must not be copied after first use.

回忆一下我们的此前的原子操作解读,所谓 atomic.Value 就是一个容器,能够被用来“原子地”存储和加载恣意的值,并且是开箱即用的。
运用场景
atomic.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
            }
        }()
    }
}
复制代码
关注 main 函数,我们只需求用 var config atomic.Value 声明一个 atomic.Value 出来,开箱即用。然后用 Store 存入【值】,随后开启一个 goroutine 异步来定时更新 config 中存储的值即可。
这里其实真正用到的就三点:

声明 atomic.Value;
调用 Store 来更新容器中存储的值;
调用 Load 来获取到存储的值。


针对读多写少场景的 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
}
复制代码
这里维护了一个 Map,每次插入 key 时直接创立一个新的 Map,经过 atomic.Value.Store 赋值回来。读的时分用 atomic.Value.Load 转换成 Map 即可。
两个例子都只用到了 Load 和 Store,但其真实 Go 1.17 版本就曾经新增了两个办法:

CompareAndSwap:经典 CAS,传入的 old 和 new 两个 interface{} 必需要是同一个类型(nil也不能够),否则会 panic
Swap:将一个新的 interface{} 存入 Value,并返回此前存储的值,若Value为空,则返回 nil。和 CAS 同样的也必需是类型分歧。

下面是目前 atomic.Value 支持的四种办法,应对绝大局部业务场景是绰绰有余了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值