refactor(splitter): clarify mapper ownership names
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
e518f2df32
commit
b3fd1fef61
5 changed files with 28 additions and 28 deletions
|
|
@ -42,8 +42,8 @@ func TestNonceSplitter_GC_Good(t *testing.T) {
|
||||||
mapper.storage.slots[0] = -1
|
mapper.storage.slots[0] = -1
|
||||||
|
|
||||||
splitter := &NonceSplitter{
|
splitter := &NonceSplitter{
|
||||||
mappers: []*NonceMapper{mapper},
|
mappers: []*NonceMapper{mapper},
|
||||||
byID: map[int64]*NonceMapper{mapper.id: mapper},
|
mapperByID: map[int64]*NonceMapper{mapper.id: mapper},
|
||||||
}
|
}
|
||||||
|
|
||||||
splitter.GC()
|
splitter.GC()
|
||||||
|
|
@ -51,7 +51,7 @@ func TestNonceSplitter_GC_Good(t *testing.T) {
|
||||||
if len(splitter.mappers) != 0 {
|
if len(splitter.mappers) != 0 {
|
||||||
t.Fatalf("expected idle mapper to be reclaimed, got %d mapper(s)", len(splitter.mappers))
|
t.Fatalf("expected idle mapper to be reclaimed, got %d mapper(s)", len(splitter.mappers))
|
||||||
}
|
}
|
||||||
if _, ok := splitter.byID[mapper.id]; ok {
|
if _, ok := splitter.mapperByID[mapper.id]; ok {
|
||||||
t.Fatalf("expected reclaimed mapper to be removed from lookup table")
|
t.Fatalf("expected reclaimed mapper to be removed from lookup table")
|
||||||
}
|
}
|
||||||
if !strategy.disconnected {
|
if !strategy.disconnected {
|
||||||
|
|
@ -77,8 +77,8 @@ func TestNonceSplitter_GC_Ugly(t *testing.T) {
|
||||||
mapper.storage.slots[0] = 7
|
mapper.storage.slots[0] = 7
|
||||||
|
|
||||||
splitter := &NonceSplitter{
|
splitter := &NonceSplitter{
|
||||||
mappers: []*NonceMapper{mapper},
|
mappers: []*NonceMapper{mapper},
|
||||||
byID: map[int64]*NonceMapper{mapper.id: mapper},
|
mapperByID: map[int64]*NonceMapper{mapper.id: mapper},
|
||||||
}
|
}
|
||||||
|
|
||||||
splitter.GC()
|
splitter.GC()
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ func NewNonceSplitter(config *proxy.Config, eventBus *proxy.EventBus, factory po
|
||||||
factory = pool.NewStrategyFactory(config)
|
factory = pool.NewStrategyFactory(config)
|
||||||
}
|
}
|
||||||
return &NonceSplitter{
|
return &NonceSplitter{
|
||||||
byID: make(map[int64]*NonceMapper),
|
mapperByID: make(map[int64]*NonceMapper),
|
||||||
config: config,
|
config: config,
|
||||||
events: eventBus,
|
events: eventBus,
|
||||||
strategyFactory: factory,
|
strategyFactory: factory,
|
||||||
|
|
@ -51,14 +51,14 @@ func (s *NonceSplitter) OnLogin(event *proxy.LoginEvent) {
|
||||||
event.Miner.SetExtendedNiceHash(true)
|
event.Miner.SetExtendedNiceHash(true)
|
||||||
for _, mapper := range s.mappers {
|
for _, mapper := range s.mappers {
|
||||||
if mapper.Add(event.Miner) {
|
if mapper.Add(event.Miner) {
|
||||||
s.byID[mapper.id] = mapper
|
s.mapperByID[mapper.id] = mapper
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mapper := s.addMapperLocked()
|
mapper := s.addMapperLocked()
|
||||||
if mapper != nil {
|
if mapper != nil {
|
||||||
_ = mapper.Add(event.Miner)
|
_ = mapper.Add(event.Miner)
|
||||||
s.byID[mapper.id] = mapper
|
s.mapperByID[mapper.id] = mapper
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -68,7 +68,7 @@ func (s *NonceSplitter) OnSubmit(event *proxy.SubmitEvent) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.mu.RLock()
|
s.mu.RLock()
|
||||||
mapper := s.byID[event.Miner.MapperID()]
|
mapper := s.mapperByID[event.Miner.MapperID()]
|
||||||
s.mu.RUnlock()
|
s.mu.RUnlock()
|
||||||
if mapper != nil {
|
if mapper != nil {
|
||||||
mapper.Submit(event)
|
mapper.Submit(event)
|
||||||
|
|
@ -81,7 +81,7 @@ func (s *NonceSplitter) OnClose(event *proxy.CloseEvent) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.mu.RLock()
|
s.mu.RLock()
|
||||||
mapper := s.byID[event.Miner.MapperID()]
|
mapper := s.mapperByID[event.Miner.MapperID()]
|
||||||
s.mu.RUnlock()
|
s.mu.RUnlock()
|
||||||
if mapper != nil {
|
if mapper != nil {
|
||||||
mapper.Remove(event.Miner)
|
mapper.Remove(event.Miner)
|
||||||
|
|
@ -106,7 +106,7 @@ func (s *NonceSplitter) GC() {
|
||||||
if mapper.strategy != nil {
|
if mapper.strategy != nil {
|
||||||
mapper.strategy.Disconnect()
|
mapper.strategy.Disconnect()
|
||||||
}
|
}
|
||||||
delete(s.byID, mapper.id)
|
delete(s.mapperByID, mapper.id)
|
||||||
_ = free
|
_ = free
|
||||||
_ = dead
|
_ = dead
|
||||||
continue
|
continue
|
||||||
|
|
@ -169,7 +169,7 @@ func (s *NonceSplitter) Disconnect() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.mappers = nil
|
s.mappers = nil
|
||||||
s.byID = make(map[int64]*NonceMapper)
|
s.mapperByID = make(map[int64]*NonceMapper)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReloadPools reconnects each mapper strategy using the updated pool list.
|
// ReloadPools reconnects each mapper strategy using the updated pool list.
|
||||||
|
|
@ -196,17 +196,17 @@ func (s *NonceSplitter) ReloadPools() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *NonceSplitter) addMapperLocked() *NonceMapper {
|
func (s *NonceSplitter) addMapperLocked() *NonceMapper {
|
||||||
id := s.seq
|
id := s.nextMapperID
|
||||||
s.seq++
|
s.nextMapperID++
|
||||||
mapper := NewNonceMapper(id, s.config, nil)
|
mapper := NewNonceMapper(id, s.config, nil)
|
||||||
mapper.events = s.events
|
mapper.events = s.events
|
||||||
mapper.lastUsed = time.Now()
|
mapper.lastUsed = time.Now()
|
||||||
mapper.strategy = s.strategyFactory(mapper)
|
mapper.strategy = s.strategyFactory(mapper)
|
||||||
s.mappers = append(s.mappers, mapper)
|
s.mappers = append(s.mappers, mapper)
|
||||||
if s.byID == nil {
|
if s.mapperByID == nil {
|
||||||
s.byID = make(map[int64]*NonceMapper)
|
s.mapperByID = make(map[int64]*NonceMapper)
|
||||||
}
|
}
|
||||||
s.byID[mapper.id] = mapper
|
s.mapperByID[mapper.id] = mapper
|
||||||
mapper.Start()
|
mapper.Start()
|
||||||
return mapper
|
return mapper
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,10 +23,10 @@ import (
|
||||||
// s.Connect()
|
// s.Connect()
|
||||||
type NonceSplitter struct {
|
type NonceSplitter struct {
|
||||||
mappers []*NonceMapper
|
mappers []*NonceMapper
|
||||||
byID map[int64]*NonceMapper
|
mapperByID map[int64]*NonceMapper
|
||||||
config *proxy.Config
|
config *proxy.Config
|
||||||
events *proxy.EventBus
|
events *proxy.EventBus
|
||||||
strategyFactory pool.StrategyFactory
|
strategyFactory pool.StrategyFactory
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
seq int64
|
nextMapperID int64
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -246,8 +246,8 @@ func (s *SimpleSplitter) ReloadPools() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleSplitter) newMapperLocked() *SimpleMapper {
|
func (s *SimpleSplitter) newMapperLocked() *SimpleMapper {
|
||||||
id := s.seq
|
id := s.nextMapperID
|
||||||
s.seq++
|
s.nextMapperID++
|
||||||
mapper := NewSimpleMapper(id, nil)
|
mapper := NewSimpleMapper(id, nil)
|
||||||
mapper.events = s.events
|
mapper.events = s.events
|
||||||
mapper.strategy = s.factory(mapper)
|
mapper.strategy = s.factory(mapper)
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,11 @@ import (
|
||||||
//
|
//
|
||||||
// s := simple.NewSimpleSplitter(cfg, eventBus, strategyFactory)
|
// s := simple.NewSimpleSplitter(cfg, eventBus, strategyFactory)
|
||||||
type SimpleSplitter struct {
|
type SimpleSplitter struct {
|
||||||
active map[int64]*SimpleMapper // minerID → mapper
|
active map[int64]*SimpleMapper // minerID → mapper
|
||||||
idle map[int64]*SimpleMapper // mapperID → mapper (reuse pool, keyed by mapper seq)
|
idle map[int64]*SimpleMapper // mapperID → mapper (reuse pool, keyed by mapper ID)
|
||||||
config *proxy.Config
|
config *proxy.Config
|
||||||
events *proxy.EventBus
|
events *proxy.EventBus
|
||||||
factory pool.StrategyFactory
|
factory pool.StrategyFactory
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
seq int64 // monotonic mapper sequence counter
|
nextMapperID int64 // monotonic mapper ID counter
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue