Skip to content

Commit 34e84db

Browse files
committed
fix the default values of public port/host
1 parent 08350e7 commit 34e84db

File tree

4 files changed

+86
-20
lines changed

4 files changed

+86
-20
lines changed

cluster.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -554,20 +554,24 @@ func (cr *Cluster) SyncFiles(ctx context.Context, files []FileInfo, heavyCheck b
554554

555555
type fileInfoWithTargets struct {
556556
FileInfo
557+
tgMux sync.Mutex
557558
targets []Storage
558559
}
559560

560-
func (cr *Cluster) checkFileFor(storage Storage, files []FileInfo, heavy bool, missing map[string]*fileInfoWithTargets) {
561+
func (cr *Cluster) checkFileFor(storage Storage, files []FileInfo, heavy bool, missing *SyncMap[string, *fileInfoWithTargets]) {
561562
addMissing := func(f FileInfo) {
562-
if info := missing[f.Hash]; info != nil {
563-
info.targets = append(info.targets, storage)
564-
} else {
565-
missing[f.Hash] = &fileInfoWithTargets{
563+
if info, has := missing.GetOrSet(f.Hash, func() *fileInfoWithTargets {
564+
return &fileInfoWithTargets{
566565
FileInfo: f,
567566
targets: []Storage{storage},
568567
}
568+
}); has {
569+
info.tgMux.Lock()
570+
info.targets = append(info.targets, storage)
571+
info.tgMux.Unlock()
569572
}
570573
}
574+
571575
logInfof("Start checking files for %s, heavy = %v", storage.String(), heavy)
572576
var buf [1024 * 32]byte
573577
for i, f := range files {
@@ -611,30 +615,35 @@ func (cr *Cluster) checkFileFor(storage Storage, files []FileInfo, heavy bool, m
611615
}
612616
logDebugf("Could not found file %q", hash)
613617
MISSING:
614-
storage.Remove(f.Hash)
615618
addMissing(f)
616619
}
617-
logInfo("File check finished")
620+
logInfo("File check finished for %s", storage.String())
618621
return
619622
}
620623

621624
func (cr *Cluster) syncFiles(ctx context.Context, files []FileInfo, heavyCheck bool) error {
622-
missingMap := make(map[string]*fileInfoWithTargets, 4)
625+
missingMap := NewSyncMap[string, *fileInfoWithTargets]()
626+
var wg sync.WaitGroup
623627
for _, s := range cr.storages {
624-
cr.checkFileFor(s, files, heavyCheck, missingMap)
625-
}
626-
627-
missing := make([]*fileInfoWithTargets, 0, len(missingMap))
628-
for _, f := range missingMap {
629-
missing = append(missing, f)
628+
wg.Add(1)
629+
go func(s Storage) {
630+
defer wg.Done()
631+
cr.checkFileFor(s, files, heavyCheck, missingMap)
632+
}(s)
630633
}
634+
wg.Wait()
631635

632-
fl := len(missing)
636+
fl := len(missingMap.m)
633637
if fl == 0 {
634638
logInfo("All oss files was synchronized")
635639
return nil
636640
}
637641

642+
missing := make([]*fileInfoWithTargets, 0, len(missingMap.m))
643+
for _, f := range missingMap.m {
644+
missing = append(missing, f)
645+
}
646+
638647
var stats syncStats
639648
stats.fl = fl
640649
for _, f := range missing {

config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ var defaultConfig = Config{
8989
NoOpen: false,
9090
NoHeavyCheck: false,
9191
TrustedXForwardedFor: false,
92-
PublicHost: "example.com",
93-
PublicPort: 8080,
92+
PublicHost: "",
93+
PublicPort: 0,
9494
Port: 4000,
9595
ClusterId: "${CLUSTER_ID}",
9696
ClusterSecret: "${CLUSTER_SECRET}",

main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,14 @@ START:
312312
)
313313

314314
logInfof("Starting Go-OpenBmclApi v%s (%s)", ClusterVersion, BuildVersion)
315+
publicPort := config.PublicPort
316+
if publicPort == 0 {
317+
publicPort = config.Port
318+
}
315319
cluster := NewCluster(ctx,
316320
"https://openbmclapi.bangbang93.com",
317321
baseDir,
318-
config.PublicHost, config.PublicPort,
322+
config.PublicHost, publicPort,
319323
config.ClusterId, config.ClusterSecret,
320324
config.Byoc, dialer,
321325
config.Storages,
@@ -377,7 +381,7 @@ START:
377381
if publicHost == "" {
378382
publicHost = config.PublicHost
379383
}
380-
logInfof("Server public at https://%s:%d (%s)", publicHost, config.PublicPort, clusterSvr.Addr)
384+
logInfof("Server public at https://%s:%d (%s)", publicHost, publicPort, clusterSvr.Addr)
381385
} else {
382386
go func() {
383387
defer listener.Close()
@@ -386,7 +390,7 @@ START:
386390
os.Exit(1)
387391
}
388392
}()
389-
logInfof("Server public at https://%s:%d (%s)", config.PublicHost, config.PublicPort, clusterSvr.Addr)
393+
logInfof("Server public at https://%s:%d (%s)", config.PublicHost, publicPort, clusterSvr.Addr)
390394
}
391395

392396
logInfof("Fetching file list")

util.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,56 @@ func initCache(base string) (err error) {
325325
}
326326
return nil
327327
}
328+
329+
type SyncMap[K comparable, V any] struct {
330+
l sync.RWMutex
331+
m map[K]V
332+
}
333+
334+
func NewSyncMap[K comparable, V any]() *SyncMap[K, V] {
335+
return &SyncMap[K, V]{
336+
m: make(map[K]V),
337+
}
338+
}
339+
340+
func (m *SyncMap[K, V]) Len() int {
341+
m.l.RLock()
342+
defer m.l.RUnlock()
343+
return len(m.m)
344+
}
345+
346+
func (m *SyncMap[K, V]) Set(k K, v V) {
347+
m.l.Lock()
348+
defer m.l.Unlock()
349+
m.m[k] = v
350+
}
351+
352+
func (m *SyncMap[K, V]) Get(k K) V {
353+
m.l.RLock()
354+
defer m.l.RUnlock()
355+
return m.m[k]
356+
}
357+
358+
func (m *SyncMap[K, V]) Has(k K) bool {
359+
m.l.RLock()
360+
defer m.l.RUnlock()
361+
_, ok := m.m[k]
362+
return ok
363+
}
364+
365+
func (m *SyncMap[K, V]) GetOrSet(k K, setter func() V) (v V, has bool) {
366+
m.l.RLock()
367+
v, has = m.m[k]
368+
m.l.RUnlock()
369+
if has {
370+
return
371+
}
372+
m.l.Lock()
373+
defer m.l.Unlock()
374+
v, has = m.m[k]
375+
if !has {
376+
v = setter()
377+
m.m[k] = v
378+
}
379+
return
380+
}

0 commit comments

Comments
 (0)