关于node.js:NodejsKoa2MySQL-打造前后端分离精品项目旧岛内附文档源码

4次阅读

共计 2391 个字符,预计需要花费 6 分钟才能阅读完成。

Node.js+Koa2+MySQL 打造前后端拆散精品我的项目《旧岛》内附文档源码

下载地址:百度网盘
如果咱们做时光机回到 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