Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove quit chan from Manager #61

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion decred.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ func main() {
amgr.AddAddresses([]netip.AddrPort{seeder})

var wg sync.WaitGroup

wg.Add(1)
go func() {
defer wg.Done()
amgr.run(ctx) // only returns on context cancellation
log.Print("Address manager done.")
}()

wg.Add(1)
go func() {
defer wg.Done()
Expand All @@ -177,6 +185,5 @@ func main() {

// Wait for crawler and http server, then stop address manager.
wg.Wait()
amgr.Stop()
log.Print("Bye!")
}
23 changes: 5 additions & 18 deletions manager.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) 2018-2021 The Decred developers
// Copyright (c) 2018-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package main

import (
"context"
"encoding/json"
"fmt"
"log"
Expand Down Expand Up @@ -34,8 +35,6 @@ type Manager struct {
mtx sync.RWMutex

nodes map[string]*Node
wg sync.WaitGroup
quit chan struct{}
peersFile string
}

Expand Down Expand Up @@ -72,7 +71,6 @@ func NewManager(dataDir string) (*Manager, error) {
amgr := Manager{
nodes: make(map[string]*Node),
peersFile: filepath.Join(dataDir, peersFilename),
quit: make(chan struct{}),
}

err = amgr.deserializePeers()
Expand All @@ -86,18 +84,9 @@ func NewManager(dataDir string) (*Manager, error) {
}
}

amgr.wg.Add(1)
go amgr.addressHandler()

davecgh marked this conversation as resolved.
Show resolved Hide resolved
return &amgr, nil
}

func (m *Manager) Stop() {
close(m.quit)
m.wg.Wait() // wait for addressHandler
log.Print("Address manager done.")
}

func (m *Manager) AddAddresses(addrPorts []netip.AddrPort) int {
var count int

Expand Down Expand Up @@ -239,10 +228,8 @@ func (m *Manager) Good(addrPort netip.AddrPort, services wire.ServiceFlag, pver
m.mtx.Unlock()
}

// addressHandler is the main handler for the address manager. It must be run
// as a goroutine.
func (m *Manager) addressHandler() {
defer m.wg.Done()
// run is the main handler for the address manager.
func (m *Manager) run(ctx context.Context) {
pruneAddressTicker := time.NewTicker(pruneAddressInterval)
defer pruneAddressTicker.Stop()
dumpAddressTicker := time.NewTicker(dumpAddressInterval)
Expand All @@ -254,7 +241,7 @@ out:
m.savePeers()
case <-pruneAddressTicker.C:
m.prunePeers()
case <-m.quit:
case <-ctx.Done():
break out
}
}
Expand Down
Loading