Skip to content

Commit

Permalink
[add] reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Command M committed Sep 23, 2019
1 parent 6f1f10c commit 228cb90
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
8 changes: 5 additions & 3 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ func (db *database) calcRevenueToday(totalRevenue uint64) {
}

// clean the share
db.client.HDel("share")
_, err = db.client.HDel("share").Result()
if err != nil {
logger.Error(err)
}
_, _ = fmt.Fprintf(f, "\n")

allMinersRevenueTable := make(map[string]interface{})
Expand Down
41 changes: 34 additions & 7 deletions nodeStratumClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package main
import (
"context"
"encoding/json"
"io"
"net"

"github.com/google/logger"
)

type nodeClient struct {
c net.Conn
enc *json.Encoder
dec *json.Decoder
conf *config
c net.Conn
enc *json.Encoder
dec *json.Decoder
}

func initNodeStratumClient(conf *config) *nodeClient {
Expand All @@ -28,9 +30,10 @@ func initNodeStratumClient(conf *config) *nodeClient {
enc := json.NewEncoder(conn)
dec := json.NewDecoder(conn)
nc := &nodeClient{
c: conn,
enc: enc,
dec: dec,
conf: conf,
c: conn,
enc: enc,
dec: dec,
}

return nc
Expand All @@ -48,7 +51,12 @@ func (nc *nodeClient) registerHandler(ctx context.Context, callback func(sr json
err := nc.dec.Decode(&sr)
if err != nil {
logger.Error(err)
return
if err == io.EOF {
if nc.reconnect() != nil {
return
}
}
continue
}

resp, _ := sr.MarshalJSON()
Expand All @@ -59,6 +67,25 @@ func (nc *nodeClient) registerHandler(ctx context.Context, callback func(sr json
}
}

func (nc *nodeClient) reconnect() error {
ip := net.ParseIP(nc.conf.Node.Address)
raddr := &net.TCPAddr{
IP: ip,
Port: nc.conf.Node.StratumPort,
}
conn, err := net.DialTCP("tcp4", nil, raddr)
if err != nil {
logger.Error(err)
return err
}

nc.c = conn
nc.enc = json.NewEncoder(conn)
nc.dec = json.NewDecoder(conn)

return nil
}

func (nc *nodeClient) close() {
_ = nc.c.Close()
}

0 comments on commit 228cb90

Please sign in to comment.