Skip to content

Commit

Permalink
Merge pull request #55 from XDagger/develop
Browse files Browse the repository at this point in the history
pool restart detecting
  • Loading branch information
swordlet authored Nov 12, 2022
2 parents a358684 + 2303887 commit 902548f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 16 deletions.
4 changes: 3 additions & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (

"github.com/swordlet/xmrig2xdag/config"
"github.com/swordlet/xmrig2xdag/logger"
"github.com/swordlet/xmrig2xdag/proxy"
"github.com/swordlet/xmrig2xdag/tcp"
)

var (
version = "1.1.3"
version = "1.1.4"

// cmd line options
configFile *string
Expand Down Expand Up @@ -97,6 +98,7 @@ func main() {
holdOpen := make(chan bool, 1)

go tcp.StartServer()
go proxy.PoolDetect()

printWelcomeMessage()

Expand Down
38 changes: 38 additions & 0 deletions server/proxy/detect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package proxy

import (
"math"
"time"

"github.com/swordlet/xmrig2xdag/xdag"
)

func PoolDetect() {
p := &Proxy{
ID: 0,
aliveSince: time.Now(),
currentJob: &Job{},
PrevJobID: NewLen(28),
submissions: make(chan *share),
done: make(chan int),
ready: true,
lastSend: time.Now(),
miniResult: math.MaxUint64,
notify: make(chan []byte, 2),
address: detectProxy,
}
timer := time.NewTicker(10 * time.Minute)
for {
<-timer.C
if poolIsDown.Load() > 0 {
p.fieldIn = 0
p.fieldOut = 0
p.recvCount = 0
p.isClosed = false
eofCount.Store(0)
xdag.PoolDown.Store(0)
go p.Run(detectProxy)
}

}
}
47 changes: 37 additions & 10 deletions server/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const (
isCrypto = true

eofLimit = 3

detectProxy = "XDAG_POOL_RESTART_DETECT_PROXY"
)

var poolIsDown atomic.Uint64
Expand Down Expand Up @@ -155,7 +157,7 @@ func NewProxy(id uint64) *Proxy {
func (p *Proxy) Run(minerName string) {
var retryCount = 0
for {
if poolIsDown.Load() > 0 {
if poolIsDown.Load() > 0 && minerName != detectProxy {
p.shutdown(2)
return
}
Expand All @@ -165,7 +167,11 @@ func (p *Proxy) Run(minerName string) {
}
retryCount += 1
if retryCount > 3 {
p.shutdown(2)
if minerName != detectProxy {
p.shutdown(2)
} else {
p.shutdown(-1)
}
return
}
logger.Get().Printf("Proxy[%d] Failed to acquire pool connection %d times. Retrying in %s.Error: %s\n",
Expand All @@ -175,6 +181,9 @@ func (p *Proxy) Run(minerName string) {
}

for {
if minerName == detectProxy && poolIsDown.Load() == 0 { // pool restart , quit detect proxy
return
}
select {
// these are from workers
case s := <-p.submissions:
Expand Down Expand Up @@ -251,6 +260,11 @@ func (p *Proxy) handleNotification(notif []byte) {
copy(p.recvByte[:32], data[:])
} else if p.recvCount == 1 {
p.recvCount = 0
if p.address == detectProxy { // pool restart, close detect proxy, restore miners connection
poolIsDown.Store(0)
p.shutdown(-1)
return
}
copy(p.recvByte[32:], data[:])

if p.shares > initDiffCount+1 && time.Since(p.targetSince) >= refreshDiffInterval {
Expand Down Expand Up @@ -369,29 +383,42 @@ func (p *Proxy) shutdown(cl int) {

if cl == 0 {
logger.Get().Printf("proxy [%d] shutdown by worker <%s>\n", p.ID, p.address)
p.Conn.Close()
if p.Conn != nil {
p.Conn.Close()
}
} else if cl == 1 {
logger.Get().Printf("proxy [%d] shutdown by pool <%s>\n", p.ID, p.address)
p.worker.Close()
if p.worker != nil {
p.worker.Close()
}
if p.fieldIn == 0 && p.fieldOut < 18 && p.Conn.EOFcount.Load() > 0 {
eofCount.Add(1)
if eofCount.Load() > eofLimit { // connection eof immediately after connect
poolIsDown.Add(1)
logger.Get().Println("*** Pool is down. Please shutdown proxy and wait for pool recovery.")
logger.Get().Println("*** Pool is down. Please wait for pool recovery.")
}
}
} else if cl == 2 {
poolIsDown.Add(1)
logger.Get().Printf("proxy [%d] pool is down <%s>\n", p.ID, p.address)
logger.Get().Println("*** Pool is down. Please shutdown proxy and wait for pool recovery.")
p.worker.Close()
logger.Get().Println("*** Pool is down. Please wait for pool recovery.")
if p.worker != nil {
p.worker.Close()
}
} else if cl == -1 {
logger.Get().Printf("proxy [%d] shutdown, <%s>\n", p.ID, p.address)
p.Conn.Close()
p.worker.Close()
if p.Conn != nil {
p.Conn.Close()
}
if p.worker != nil {
p.worker.Close()
}
}

close(p.done)
p.director.removeProxy(p.ID)
if p.ID > 0 {
p.director.removeProxy(p.ID)
}
p.worker = nil
p.Conn = nil
p.SS = nil
Expand Down
10 changes: 5 additions & 5 deletions server/xdag/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/swordlet/xmrig2xdag/logger"
)

var poolDown atomic.Uint64
var PoolDown atomic.Uint64

// Connection to XDAG pool
type Connection struct {
Expand Down Expand Up @@ -51,7 +51,7 @@ func (c *Connection) StartWriter() {
defer c.Stop()

for {
if poolDown.Load() > 0 {
if PoolDown.Load() > 0 {
return
}
select {
Expand All @@ -77,7 +77,7 @@ func (c *Connection) StartReader() {
defer c.Stop()

for {
if poolDown.Load() > 0 {
if PoolDown.Load() > 0 {
return
}
select {
Expand All @@ -97,7 +97,7 @@ func (c *Connection) StartReader() {
switch errType := err.(type) {
case net.Error:
if errType.Timeout() {
poolDown.Add(1)
PoolDown.Add(1)
}
}

Expand Down Expand Up @@ -145,7 +145,7 @@ func (c *Connection) Stop() {
close(c.msgBuffChan)
//set flag
c.isClosed = true
if poolDown.Load() > 0 {
if PoolDown.Load() > 0 {
c.done <- 2
} else {
c.done <- 1
Expand Down

0 comments on commit 902548f

Please sign in to comment.