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
|
||||
|
||||
splitter := &NonceSplitter{
|
||||
mappers: []*NonceMapper{mapper},
|
||||
byID: map[int64]*NonceMapper{mapper.id: mapper},
|
||||
mappers: []*NonceMapper{mapper},
|
||||
mapperByID: map[int64]*NonceMapper{mapper.id: mapper},
|
||||
}
|
||||
|
||||
splitter.GC()
|
||||
|
|
@ -51,7 +51,7 @@ func TestNonceSplitter_GC_Good(t *testing.T) {
|
|||
if len(splitter.mappers) != 0 {
|
||||
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")
|
||||
}
|
||||
if !strategy.disconnected {
|
||||
|
|
@ -77,8 +77,8 @@ func TestNonceSplitter_GC_Ugly(t *testing.T) {
|
|||
mapper.storage.slots[0] = 7
|
||||
|
||||
splitter := &NonceSplitter{
|
||||
mappers: []*NonceMapper{mapper},
|
||||
byID: map[int64]*NonceMapper{mapper.id: mapper},
|
||||
mappers: []*NonceMapper{mapper},
|
||||
mapperByID: map[int64]*NonceMapper{mapper.id: mapper},
|
||||
}
|
||||
|
||||
splitter.GC()
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ func NewNonceSplitter(config *proxy.Config, eventBus *proxy.EventBus, factory po
|
|||
factory = pool.NewStrategyFactory(config)
|
||||
}
|
||||
return &NonceSplitter{
|
||||
byID: make(map[int64]*NonceMapper),
|
||||
mapperByID: make(map[int64]*NonceMapper),
|
||||
config: config,
|
||||
events: eventBus,
|
||||
strategyFactory: factory,
|
||||
|
|
@ -51,14 +51,14 @@ func (s *NonceSplitter) OnLogin(event *proxy.LoginEvent) {
|
|||
event.Miner.SetExtendedNiceHash(true)
|
||||
for _, mapper := range s.mappers {
|
||||
if mapper.Add(event.Miner) {
|
||||
s.byID[mapper.id] = mapper
|
||||
s.mapperByID[mapper.id] = mapper
|
||||
return
|
||||
}
|
||||
}
|
||||
mapper := s.addMapperLocked()
|
||||
if mapper != nil {
|
||||
_ = 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
|
||||
}
|
||||
s.mu.RLock()
|
||||
mapper := s.byID[event.Miner.MapperID()]
|
||||
mapper := s.mapperByID[event.Miner.MapperID()]
|
||||
s.mu.RUnlock()
|
||||
if mapper != nil {
|
||||
mapper.Submit(event)
|
||||
|
|
@ -81,7 +81,7 @@ func (s *NonceSplitter) OnClose(event *proxy.CloseEvent) {
|
|||
return
|
||||
}
|
||||
s.mu.RLock()
|
||||
mapper := s.byID[event.Miner.MapperID()]
|
||||
mapper := s.mapperByID[event.Miner.MapperID()]
|
||||
s.mu.RUnlock()
|
||||
if mapper != nil {
|
||||
mapper.Remove(event.Miner)
|
||||
|
|
@ -106,7 +106,7 @@ func (s *NonceSplitter) GC() {
|
|||
if mapper.strategy != nil {
|
||||
mapper.strategy.Disconnect()
|
||||
}
|
||||
delete(s.byID, mapper.id)
|
||||
delete(s.mapperByID, mapper.id)
|
||||
_ = free
|
||||
_ = dead
|
||||
continue
|
||||
|
|
@ -169,7 +169,7 @@ func (s *NonceSplitter) Disconnect() {
|
|||
}
|
||||
}
|
||||
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.
|
||||
|
|
@ -196,17 +196,17 @@ func (s *NonceSplitter) ReloadPools() {
|
|||
}
|
||||
|
||||
func (s *NonceSplitter) addMapperLocked() *NonceMapper {
|
||||
id := s.seq
|
||||
s.seq++
|
||||
id := s.nextMapperID
|
||||
s.nextMapperID++
|
||||
mapper := NewNonceMapper(id, s.config, nil)
|
||||
mapper.events = s.events
|
||||
mapper.lastUsed = time.Now()
|
||||
mapper.strategy = s.strategyFactory(mapper)
|
||||
s.mappers = append(s.mappers, mapper)
|
||||
if s.byID == nil {
|
||||
s.byID = make(map[int64]*NonceMapper)
|
||||
if s.mapperByID == nil {
|
||||
s.mapperByID = make(map[int64]*NonceMapper)
|
||||
}
|
||||
s.byID[mapper.id] = mapper
|
||||
s.mapperByID[mapper.id] = mapper
|
||||
mapper.Start()
|
||||
return mapper
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,10 +23,10 @@ import (
|
|||
// s.Connect()
|
||||
type NonceSplitter struct {
|
||||
mappers []*NonceMapper
|
||||
byID map[int64]*NonceMapper
|
||||
mapperByID map[int64]*NonceMapper
|
||||
config *proxy.Config
|
||||
events *proxy.EventBus
|
||||
strategyFactory pool.StrategyFactory
|
||||
mu sync.RWMutex
|
||||
seq int64
|
||||
nextMapperID int64
|
||||
}
|
||||
|
|
|
|||
|
|
@ -246,8 +246,8 @@ func (s *SimpleSplitter) ReloadPools() {
|
|||
}
|
||||
|
||||
func (s *SimpleSplitter) newMapperLocked() *SimpleMapper {
|
||||
id := s.seq
|
||||
s.seq++
|
||||
id := s.nextMapperID
|
||||
s.nextMapperID++
|
||||
mapper := NewSimpleMapper(id, nil)
|
||||
mapper.events = s.events
|
||||
mapper.strategy = s.factory(mapper)
|
||||
|
|
|
|||
|
|
@ -18,11 +18,11 @@ import (
|
|||
//
|
||||
// s := simple.NewSimpleSplitter(cfg, eventBus, strategyFactory)
|
||||
type SimpleSplitter struct {
|
||||
active map[int64]*SimpleMapper // minerID → mapper
|
||||
idle map[int64]*SimpleMapper // mapperID → mapper (reuse pool, keyed by mapper seq)
|
||||
config *proxy.Config
|
||||
events *proxy.EventBus
|
||||
factory pool.StrategyFactory
|
||||
mu sync.Mutex
|
||||
seq int64 // monotonic mapper sequence counter
|
||||
active map[int64]*SimpleMapper // minerID → mapper
|
||||
idle map[int64]*SimpleMapper // mapperID → mapper (reuse pool, keyed by mapper ID)
|
||||
config *proxy.Config
|
||||
events *proxy.EventBus
|
||||
factory pool.StrategyFactory
|
||||
mu sync.Mutex
|
||||
nextMapperID int64 // monotonic mapper ID counter
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue