项目结构更新
This commit is contained in:
35
scripts/goroutine/connectPool/goroutine/readgo.go
Normal file
35
scripts/goroutine/connectPool/goroutine/readgo.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package goroutine
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Dataread(db *sql.DB, donetitle chan struct{}) {
|
||||
// 启动一个长事务(整个循环在一个事务内,而非每次查询一个事务)
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer tx.Rollback() // 确保退出时回滚(仅为测试)
|
||||
|
||||
// 随机种子(保持不变)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// 只执行两次查询,方便观察结果
|
||||
for i := 0; i < 2; i++ {
|
||||
select {
|
||||
case <-time.After(2 * time.Second): // 第一次查询后等待2秒,给Datawrite插入时间
|
||||
var count int
|
||||
err = tx.QueryRow("SELECT count(*) FROM article WHERE age > 50 FOR UPDATE ").Scan(&count)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("第%d次查询 count: %d\n", i+1, count)
|
||||
case <-donetitle:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
11
scripts/goroutine/connectPool/goroutine/updatego.go
Normal file
11
scripts/goroutine/connectPool/goroutine/updatego.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package goroutine
|
||||
|
||||
import "database/sql"
|
||||
|
||||
func DataUpdate(db *sql.DB) {
|
||||
_, err := db.Begin()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
45
scripts/goroutine/connectPool/goroutine/writego.go
Normal file
45
scripts/goroutine/connectPool/goroutine/writego.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package goroutine
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Datawrite(db *sql.DB, donetitle chan struct{}) {
|
||||
timetick := time.Tick(1 * time.Second)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-timetick:
|
||||
uid, error := uuid.NewRandom()
|
||||
if error != nil {
|
||||
panic(error)
|
||||
}
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("开始事务")
|
||||
_, err = tx.Exec(`insert INTO article (uid,content,author,age) values (?,?,?,?)`, uid, "123", "用户"+strconv.Itoa(rand.Intn(1000)), rand.Intn(40)+60)
|
||||
if err != nil {
|
||||
if rbErr := tx.Rollback(); rbErr != nil {
|
||||
panic("回滚失败: " + rbErr.Error())
|
||||
}
|
||||
panic("插入失败: " + err.Error()) // 明确错误类型
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("插入数据成功")
|
||||
case <-donetitle:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
32
scripts/goroutine/connectPool/pool.go
Normal file
32
scripts/goroutine/connectPool/pool.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package connectPool
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
goroutine2 "toutoukan/scripts/goroutine/connectPool/goroutine"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
func ConnectPool(donetitle chan struct{}) {
|
||||
dsn := "mayiming:Mydream5654my,@tcp(43.142.81.151:3306)/goLearn?charset-uft8mb4&parseTime=True"
|
||||
db, _ = sql.Open("mysql", dsn)
|
||||
|
||||
err := db.Ping()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("数据库连接成功")
|
||||
for i := 0; i < 20; i++ {
|
||||
go goroutine2.Datawrite(db, donetitle)
|
||||
}
|
||||
go goroutine2.Dataread(db, donetitle)
|
||||
}
|
||||
|
||||
func DisconnectPool() {
|
||||
err := db.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
27
scripts/goroutine/consumer.go
Normal file
27
scripts/goroutine/consumer.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package goroutine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"toutoukan/model/routine"
|
||||
)
|
||||
|
||||
func NewConsumer(ch chan routine.Task, wg *sync.WaitGroup, done chan struct{}, num int, data *routine.Data, rwmutex *sync.RWMutex) {
|
||||
defer wg.Done()
|
||||
for {
|
||||
select {
|
||||
case <-ch:
|
||||
rwmutex.RLock()
|
||||
fmt.Printf("消费者%d号处理消息,统计消息总数为:%d\n", num+1, data.Count)
|
||||
fmt.Printf("消费者%d号:当前记录详情如下:\n", num+1)
|
||||
for key, value := range data.Record {
|
||||
fmt.Printf(" 键:%d,值:%d\n", key, value)
|
||||
}
|
||||
rwmutex.RUnlock()
|
||||
case <-done:
|
||||
fmt.Printf("消费者%d号退出接收\n", num+1)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
44
scripts/goroutine/producer.go
Normal file
44
scripts/goroutine/producer.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package goroutine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
"toutoukan/model/routine"
|
||||
)
|
||||
|
||||
func NewProducer(ch chan routine.Task, wg *sync.WaitGroup, done chan struct{}, num int, mutex *sync.Mutex, data *routine.Data, rwmutex *sync.RWMutex) {
|
||||
defer wg.Done()
|
||||
timech := time.Tick(1 * time.Second)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
for {
|
||||
select {
|
||||
case <-timech:
|
||||
fmt.Printf("生产者%d号上锁\n", num+1)
|
||||
randnum := rand.Intn(100)
|
||||
task := routine.Task{
|
||||
Id: strconv.Itoa(randnum),
|
||||
Content: "访问数据库",
|
||||
}
|
||||
mutex.Lock()
|
||||
data.Count += 1
|
||||
mutex.Unlock()
|
||||
|
||||
rwmutex.Lock()
|
||||
data.Record[randnum] += 1
|
||||
rwmutex.Unlock()
|
||||
|
||||
fmt.Printf("生产者%d号注入编号为%s,内容为:%s\n", num+1, task.Id, task.Content)
|
||||
ch <- task
|
||||
fmt.Printf("生产者%d号释放锁\n", num+1)
|
||||
|
||||
case <-done:
|
||||
fmt.Printf("生产者%d号退出任务\n", num+1)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user