72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package simple
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"dappco.re/go/core/proxy"
|
|
"dappco.re/go/core/proxy/pool"
|
|
)
|
|
|
|
// SimpleMapper holds one outbound pool connection and serves at most one active miner
|
|
// at a time. It becomes idle when the miner disconnects and may be reclaimed for the
|
|
// next login.
|
|
//
|
|
// m := simple.NewSimpleMapper(id, strategy)
|
|
type SimpleMapper struct {
|
|
id int64
|
|
miner *proxy.Miner // nil when idle
|
|
strategy pool.Strategy
|
|
events *proxy.EventBus
|
|
pending map[int64]int64
|
|
idleAt time.Time // zero when active
|
|
stopped bool
|
|
mu sync.Mutex
|
|
}
|
|
|
|
// NewSimpleMapper stores the mapper ID and strategy.
|
|
//
|
|
// mapper := simple.NewSimpleMapper(1, strategy)
|
|
func NewSimpleMapper(id int64, strategy pool.Strategy) *SimpleMapper {
|
|
return &SimpleMapper{id: id, strategy: strategy, pending: make(map[int64]int64)}
|
|
}
|
|
|
|
func (m *SimpleMapper) OnJob(job proxy.Job) {
|
|
if !job.IsValid() || m.miner == nil {
|
|
return
|
|
}
|
|
|
|
m.miner.ForwardJob(job, job.Algo)
|
|
}
|
|
|
|
func (m *SimpleMapper) OnResultAccepted(sequence int64, accepted bool, errorMessage string) {
|
|
if m.miner == nil {
|
|
return
|
|
}
|
|
|
|
m.mu.Lock()
|
|
requestID, exists := m.pending[sequence]
|
|
if !exists {
|
|
m.mu.Unlock()
|
|
return
|
|
}
|
|
delete(m.pending, sequence)
|
|
m.mu.Unlock()
|
|
|
|
if accepted {
|
|
if m.events != nil {
|
|
m.events.Dispatch(proxy.Event{Type: proxy.EventAccept, Miner: m.miner, Diff: m.miner.Diff()})
|
|
}
|
|
m.miner.Success(requestID, "OK")
|
|
return
|
|
}
|
|
|
|
if m.events != nil {
|
|
m.events.Dispatch(proxy.Event{Type: proxy.EventReject, Miner: m.miner, Diff: m.miner.Diff(), Error: errorMessage})
|
|
}
|
|
m.miner.ReplyWithError(requestID, errorMessage)
|
|
}
|
|
|
|
func (m *SimpleMapper) OnDisconnect() {
|
|
m.stopped = true
|
|
}
|