Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sync status rpc interface #377

Merged
merged 4 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions p2p/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package p2p
import (
"math"
"sort"
"sync/atomic"
"time"

"github.com/qlcchain/go-qlc/common"
Expand Down Expand Up @@ -57,7 +58,8 @@ func (ss *ServiceSync) Start() {
return
case <-ticker.C:
now := time.Now().UTC().Unix()
if ss.lastSyncTime < now {
v := atomic.LoadInt64(&ss.lastSyncTime)
if v < now {
peerID, err := ss.netService.node.StreamManager().RandomPeer()
if err != nil {
continue
Expand All @@ -83,7 +85,7 @@ func (ss *ServiceSync) Start() {
}

func (ss *ServiceSync) LastSyncTime(t time.Time) {
ss.lastSyncTime = t.Add(syncTimeout).UTC().Unix()
atomic.StoreInt64(&ss.lastSyncTime, t.Add(syncTimeout).UTC().Unix())
}

// Stop sync service
Expand All @@ -96,7 +98,8 @@ func (ss *ServiceSync) Stop() {
func (ss *ServiceSync) onFrontierReq(message *Message) error {
ss.netService.node.logger.Debug("receive FrontierReq")
now := time.Now().UTC().Unix()
if ss.lastSyncTime < now {
v := atomic.LoadInt64(&ss.lastSyncTime)
if v < now {
var fs []*types.Frontier
fs, err := ss.qlcLedger.GetFrontiers()
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions rpc/api/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ import (
"github.com/qlcchain/go-qlc/ledger"
"github.com/qlcchain/go-qlc/log"
"go.uber.org/zap"
"sync/atomic"
"time"
)

const syncTimeout = 10 * time.Second

var lastSyncTime int64

type NetApi struct {
ledger *ledger.Ledger
eb event.EventBus
logger *zap.SugaredLogger
}

func syncingTime(t time.Time) {
atomic.StoreInt64(&lastSyncTime, t.Add(syncTimeout).UTC().Unix())
}
func NewNetApi(l *ledger.Ledger, eb event.EventBus) *NetApi {
_ = eb.Subscribe(string(common.EventSyncing), syncingTime)
return &NetApi{ledger: l, eb: eb, logger: log.NewLogger("api_net")}
}

Expand All @@ -41,3 +51,11 @@ func (q *NetApi) ConnectPeersInfo() *PeersInfo {
}
return i
}
func (q *NetApi) Syncing() bool {
now := time.Now().UTC().Unix()
gythialy marked this conversation as resolved.
Show resolved Hide resolved
v := atomic.LoadInt64(&lastSyncTime)
if v < now {
return false
}
return true
}