目錄
問題內容
解決方法
問題
可靠的設計
manager.go
main.go
manager_test.go
首頁 後端開發 Golang 這個Golang程式中的同步問題

這個Golang程式中的同步問題

Feb 10, 2024 am 09:06 AM
同步機制

這個Golang程式中的同步問題

php小編蘋果今天為大家介紹一個有趣的主題—"這個Golang程式中的同步問題"。在編寫並發程式時,我們經常會遇到同步問題,即多個執行緒之間的競爭和協調。 Golang作為一門同時進行程式語言,提供了豐富的同步機制和工具,但也存在一些常見的同步問題需要我們注意和解決。本文將詳細探討這些問題,並給予對應的解決方案,幫助大家更能理解並應對Golang中的同步挑戰。讓我們一起來探索吧!

問題內容

我正在嘗試建立一個充當代理伺服器並且可以動態切換到新端點的程式。但我遇到一個問題,在呼叫 switchovertonewendpoint() 後,仍然有一些代理物件連接到原始端點 8.8.8.8 ,應該將其關閉。

package main

import (
    "net"
    "sync"
    "sync/atomic"
    "time"
)

type proxy struct {
    id       int32
    from, to *net.tcpconn
}

var switchover int32 = 0

func setswitchover() {
    atomic.storeint32((*int32)(&switchover), 1)
}

func switchoverenabled() bool {
    return atomic.loadint32((*int32)(&switchover)) == 1
}

var proxies map[int32]*proxy = make(map[int32]*proxy, 0)
var proxyseq int32 = 0
var mu sync.rwmutex

func addproxy(from *net.tcpconn) {
    mu.lock()
    proxyseq += 1
    proxy := &proxy{id: proxyseq, from: from}
    proxies[proxyseq] = proxy
    mu.unlock()

    var toaddr string
    if switchoverenabled() {
        toaddr = "1.1.1.1"
    } else {
        toaddr = "8.8.8.8"
    }
    tcpaddr, _ := net.resolvetcpaddr("tcp4", toaddr)
    toconn, err := net.dialtcp("tcp", nil, tcpaddr)
    if err != nil {
        panic(err)
    }
    proxy.to = toconn
}

func switchovertonewendpoint() {
    mu.rlock()
    closedproxies := proxies
    mu.runlock()

    setswitchover()
    for _, proxy := range closedproxies {
        proxy.from.close()
        proxy.to.close()
        mu.lock()
        delete(proxies, proxy.id)
        mu.unlock()
    }
}

func main() {
    tcpaddr, _ := net.resolvetcpaddr("tcp4", "0.0.0.0:5432")
    ln, _ := net.listentcp("tcp", tcpaddr)
    go func() {
        time.sleep(time.second * 30)
        switchovertonewendpoint()
    }()
    for {
        clientconn, err := ln.accepttcp()
        if err != nil {
            panic(err)
        }
        go addproxy(clientconn)
    }
}
登入後複製

想了一會兒,我猜問題出在

mu.rlock()
    closedproxies := proxies
    mu.runlock()
登入後複製

但我不確定這是否是根本原因,以及是否可以透過將其替換為以下內容來修復它:

closedProxies := make([]*Proxy, 0)
    mu.RLock()
    for _, proxy := range proxies {
        closedProxies = append(closedProxies, proxy)
    }
    mu.RUnlock()
登入後複製

由於這個案例很難重現,所以有專業人士可以提供想法或提示嗎?歡迎任何評論。提前致謝。

解決方法

問題

改變是必要的。在最初的實作中, latedproxies 持有相同的映射。請參閱此演示:

package main

import "fmt"

func main() {
    proxies := make(map[int]int, 0)
    for i := 0; i < 10; i++ {
        proxies[i] = i
    }

    closeproxies := proxies

    proxies[10] = 10
    proxies[11] = 11

    for k := range closeproxies {
        delete(proxies, k)
    }

    fmt.printf("items left: %d\n", len(proxies))
    // output:
    //   items left: 0
}
登入後複製

但這不是根本原因。可以在複製 closeproxies 之後但在呼叫 setswitchover 之前新增代理。在這種情況下,新代理連接到舊位址,但不在 closeproxies 中。我認為這是根本原因。

還有一個問題。在設定 to 欄位之前,將向 proxies 新增代理程式。程式可能希望在設定 to 欄位之前關閉此代理,從而導致恐慌。

可靠的設計

這個想法是將所有端點放入一個切片中,並讓每個端點管理自己的代理清單。所以我們只需要追蹤當前端點的索引。當我們想要切換到另一個端點時,我們只需要更改索引,並告訴過時的端點清除其代理程式。剩下的唯一複雜的事情是確保過時的端點可以清除其所有代理。請參閱下面的實作:

manager.go

這就是這個想法的實現。

package main

import (
    "sync"
)

// conn is abstraction of a connection to make manager easy to test.
type conn interface {
    close() error
}

// dialer is abstraction of a dialer to make manager easy to test.
type dialer interface {
    dial(addr string) (conn, error)
}

type manager struct {
    // mucurrent protects the "current" member.
    mucurrent sync.rwmutex
    current   int // when current is -1, the manager is shuted down.
    endpoints []*endpoint

    // mu protects the whole switch action.
    mu sync.mutex
}

func newmanager(dialer dialer, addresses ...string) *manager {
    if len(addresses) < 2 {
        panic("a manger should handle at least 2 addresses")
    }

    endpoints := make([]*endpoint, len(addresses))
    for i, addr := range addresses {
        endpoints[i] = &endpoint{
            address: addr,
            dialer:  dialer,
        }
    }
    return &manager{
        endpoints: endpoints,
    }
}

func (m *manager) addproxy(from conn) {
    // 1. addproxy will wait when the write lock of m.mucurrent is taken.
    // once the write lock is released, addproxy will connect to the new endpoint.
    // switch only holds the write lock for a short time, and switch is called
    // not so frequently, so addproxy won't wait too much.
    // 2. switch will wait if there is any addproxy holding the read lock of
    // m.mucurrent. that means switch waits longer. the advantage is that when
    // e.clear is called in switch, all addproxy requests to the old endpoint
    // are done. so it's safe to call e.clear then.
    m.mucurrent.rlock()
    defer m.mucurrent.runlock()

    current := m.current

    // do not accept any new connection when m has been shutdown.
    if current == -1 {
        from.close()
        return
    }

    m.endpoints[current].addproxy(from)
}

func (m *manager) switch() {
    // in a real world, switch is called not so frequently.
    // so it's ok to add a lock here.
    // and it's necessary to make sure the old endpoint is cleared and ready
    // for use in the future.
    m.mu.lock()
    defer m.mu.unlock()

    // take the write lock of m.mucurrent.
    // it waits for all the addproxy requests holding the read lock to finish.
    m.mucurrent.lock()
    old := m.current

    // do nothing when m has been shutdown.
    if old == -1 {
        m.mucurrent.unlock()
        return
    }
    next := old + 1
    if next >= len(m.endpoints) {
        next = 0
    }
    m.current = next
    m.mucurrent.unlock()

    // when it reaches here, all addproxy requests to the old endpoint are done.
    // and it's safe to call e.clear now.
    m.endpoints[old].clear()
}

func (m *manager) shutdown() {
    m.mu.lock()
    defer m.mu.unlock()

    m.mucurrent.lock()
    current := m.current
    m.current = -1
    m.mucurrent.unlock()

    m.endpoints[current].clear()
}

type proxy struct {
    from, to conn
}

type endpoint struct {
    address string
    dialer  dialer

    mu      sync.mutex
    proxies []*proxy
}

func (e *endpoint) clear() {
    for _, p := range e.proxies {
        p.from.close()
        p.to.close()
    }

    // assign a new slice to e.proxies, and the gc will collect the old one.
    e.proxies = []*proxy{}
}

func (e *endpoint) addproxy(from conn) {
    toconn, err := e.dialer.dial(e.address)
    if err != nil {
        // close the from connection so that the client will reconnect?
        from.close()
        return
    }

    e.mu.lock()
    defer e.mu.unlock()
    e.proxies = append(e.proxies, &proxy{from: from, to: toconn})
}
登入後複製

main.go

這個示範展示如何使用先前實作的manager類型:

package main

import (
    "net"
    "time"
)

type realdialer struct{}

func (d realdialer) dial(addr string) (conn, error) {
    tcpaddr, err := net.resolvetcpaddr("tcp4", addr)
    if err != nil {
        return nil, err
    }
    return net.dialtcp("tcp", nil, tcpaddr)
}

func main() {
    manager := newmanager(realdialer{}, "1.1.1.1", "8.8.8.8")

    tcpaddr, _ := net.resolvetcpaddr("tcp4", "0.0.0.0:5432")
    ln, _ := net.listentcp("tcp", tcpaddr)

    go func() {
        for range time.tick(30 * time.second) {
            manager.switch()
        }
    }()
    for {
        clientconn, err := ln.accepttcp()
        if err != nil {
            panic(err)
        }
        go manager.addproxy(clientconn)
    }
}
登入後複製

manager_test.go

使用以下指令執行測試:go test ./... -race -count 10

#
package main

import (
    "errors"
    "math/rand"
    "sync"
    "sync/atomic"
    "testing"
    "time"

    "github.com/google/uuid"
)

func TestManager(t *testing.T) {
    addresses := []string{"1.1.1.1", "8.8.8.8"}
    dialer := newDialer(addresses...)
    manager := NewManager(dialer, addresses...)

    ch := make(chan int, 1)

    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        for range ch {
            manager.Switch()
        }
        wg.Done()
    }()

    count := 1000
    total := count * 10

    wg.Add(total)

    fromConn := &fakeFromConn{}
    for i := 0; i < total; i++ {
        if i%count == count-1 {
            ch <- 0
        }
        go func() {
            manager.AddProxy(fromConn)
            wg.Done()
        }()
    }
    close(ch)

    wg.Wait()

    manager.Shutdown()

    for _, s := range dialer.servers {
        left := len(s.conns)
        if left != 0 {
            t.Errorf("server %s, unexpected connections left: %d", s.addr, left)
        }
    }

    closedCount := fromConn.closedCount.Load()

    if closedCount != int32(total) {
        t.Errorf("want closed count: %d, got: %d", total, closedCount)
    }
}

type fakeFromConn struct {
    closedCount atomic.Int32
}

func (c *fakeFromConn) Close() error {
    c.closedCount.Add(1)

    return nil
}

type fakeToConn struct {
    id     uuid.UUID
    server *fakeServer
}

func (c *fakeToConn) Close() error {
    if c.id == uuid.Nil {
        return nil
    }

    c.server.removeConn(c.id)

    return nil
}

type fakeServer struct {
    addr  string
    mu    sync.Mutex
    conns map[uuid.UUID]bool
}

func (s *fakeServer) addConn() (uuid.UUID, error) {
    s.mu.Lock()
    defer s.mu.Unlock()

    id, err := uuid.NewRandom()
    if err == nil {
        s.conns[id] = true
    }
    return id, err
}

func (s *fakeServer) removeConn(id uuid.UUID) {
    s.mu.Lock()
    defer s.mu.Unlock()

    delete(s.conns, id)
}

type fakeDialer struct {
    servers map[string]*fakeServer
}

func newDialer(addresses ...string) *fakeDialer {
    servers := make(map[string]*fakeServer)
    for _, addr := range addresses {
        servers[addr] = &fakeServer{
            addr:  addr,
            conns: make(map[uuid.UUID]bool),
        }
    }
    return &fakeDialer{
        servers: servers,
    }
}

func (d *fakeDialer) Dial(addr string) (Conn, error) {
    n := rand.Intn(100)
    if n == 0 {
        return nil, errors.New("fake network error")
    }
    // Simulate network latency.
    time.Sleep(time.Duration(n) * time.Millisecond)

    s := d.servers[addr]
    id, err := s.addConn()
    if err != nil {
        return nil, err
    }
    conn := &fakeToConn{
        id:     id,
        server: s,
    }
    return conn, nil
}
登入後複製

以上是這個Golang程式中的同步問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

golang函數與goroutine的父子關係 golang函數與goroutine的父子關係 Apr 25, 2024 pm 12:57 PM

Go中函數與goroutine存在父子關係,父goroutine創建子goroutine,子goroutine可以存取父goroutine的變數但不反之。建立子goroutine使用go關鍵字,子goroutine透過匿名函數或命名的函數執行。父goroutine可以透過sync.WaitGroup等待子goroutine完成,以確保在所有子goroutine完成之前不會退出程式。

golang函數與goroutine的優缺點比較 golang函數與goroutine的優缺點比較 Apr 25, 2024 pm 12:30 PM

函數用於順序執行任務,簡單易用,但有阻塞和資源受限問題。 Goroutine是並發執行任務的輕量級線程,具有高並發性、可擴展性和事件處理能力,但使用複雜,開銷較大,且難以調試。在實戰中,Goroutine在並發任務時通常比函數具有更好的性能。

PHP 函數在多執行緒環境中的行為如何? PHP 函數在多執行緒環境中的行為如何? Apr 16, 2024 am 10:48 AM

在多執行緒環境中,PHP函數的行為取決於其類型:普通函數:執行緒安全,可並發執行。修改全域變數的函數:不安全,需使用同步機制。文件操作函數:不安全,需使用同步機制協調存取。資料庫操作函數:不安全,需使用資料庫系統機制防止衝突。

C++並發程式設計:如何處理線程間通訊? C++並發程式設計:如何處理線程間通訊? May 04, 2024 pm 12:45 PM

C++中執行緒間通訊的方法包括:共享記憶體、同步機制(互斥鎖、條件變數)、管道、訊息佇列。例如,使用互斥鎖保護共享計數器:聲明互斥鎖(m)、共享變數(counter);每個執行緒透過加鎖(lock_guard)更新計數器;確保一次只有一個執行緒更新計數器,防止競爭條件。

C++ 中有哪些並發程式框架和函式庫?它們各自的優點和限制是什麼? C++ 中有哪些並發程式框架和函式庫?它們各自的優點和限制是什麼? May 07, 2024 pm 02:06 PM

C++並發程式框架具有以下選項:輕量級執行緒(std::thread);執行緒​​安全的Boost並發容器和演算法;用於共享記憶體多處理器的OpenMP;高效能ThreadBuildingBlocks(TBB);跨平台C++並發互操作庫(cpp-Concur)。

volatile在java中的用法 volatile在java中的用法 May 01, 2024 pm 06:42 PM

volatile關鍵字用於修飾變量,確保所有執行緒都能看到變數的最新值並保證對變數的修改是一個不可中斷的操作。主要應用場景包括多執行緒共享變數、記憶體屏障和並發程式設計。但要注意的是,volatile不能保證執行緒安全,可能會降低效能,只應在絕對必要時才使用。

程式效能優化有哪些常見的方法? 程式效能優化有哪些常見的方法? May 09, 2024 am 09:57 AM

程式效能最佳化方法包括:演算法最佳化:選擇時間複雜度較低的演算法,減少迴圈和條件語句。資料結構選擇:根據資料存取模式選擇合適的資料結構,例如查找樹和雜湊表。記憶體最佳化:避免建立不必要對象,釋放不再使用的內存,使用記憶體池技術。執行緒優化:識別可並行化任務,優化執行緒同步機制。資料庫最佳化:建立索引加快資料檢索,優化查詢語句,使用快取或NoSQL資料庫提升效能。

並發程式設計中 C++ 函數的鎖與同步機制? 並發程式設計中 C++ 函數的鎖與同步機制? Apr 27, 2024 am 11:21 AM

C++並發程式設計中函數鎖定和同步機制用於管理多執行緒環境中資料的並發訪問,防止資料競爭。主要機制包括:互斥量(Mutex):低階同步原語,確保一次只有一個執行緒存取臨界區。條件變數(ConditionVariable):允許執行緒等待條件滿足,提供執行緒間通訊。原子操作:單指令操作,確保變數或資料的單執行緒更新,防止衝突。

See all articles