github.com/qiniu/go-sdk/v7@v7.27.0/storage/region_group.go (about) 1 package storage 2 3 import "sync" 4 5 type RegionGroup struct { 6 locker sync.RWMutex 7 currentRegionIndex int 8 regions []*Region 9 } 10 11 func NewRegionGroup(region ...*Region) *RegionGroup { 12 return &RegionGroup{ 13 locker: sync.RWMutex{}, 14 currentRegionIndex: 0, 15 regions: region, 16 } 17 } 18 19 func (g *RegionGroup) GetRegion() *Region { 20 g.locker.RLock() 21 defer g.locker.RUnlock() 22 if g.currentRegionIndex >= len(g.regions) { 23 return nil 24 } 25 return g.regions[g.currentRegionIndex] 26 } 27 28 func (g *RegionGroup) CouldSwitchRegion() bool { 29 g.locker.RLock() 30 defer g.locker.RUnlock() 31 return len(g.regions) > (g.currentRegionIndex + 1) 32 } 33 34 func (g *RegionGroup) SwitchRegion() bool { 35 g.locker.Lock() 36 defer g.locker.Unlock() 37 if len(g.regions) <= (g.currentRegionIndex + 1) { 38 return false 39 } 40 g.currentRegionIndex++ 41 return true 42 } 43 44 func (g *RegionGroup) clone() *RegionGroup { 45 g.locker.RLock() 46 defer g.locker.RUnlock() 47 return &RegionGroup{ 48 locker: sync.RWMutex{}, 49 currentRegionIndex: g.currentRegionIndex, 50 regions: g.regions, 51 } 52 }