-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from tock-ibm/hb-dispatch
Heartbeat dispatch & skeleton
- Loading branch information
Showing
6 changed files
with
143 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright IBM Corp. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package bft | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/SmartBFT-Go/consensus/pkg/api" | ||
"github.com/SmartBFT-Go/consensus/smartbftprotos" | ||
) | ||
|
||
const ( | ||
DefaultHeartbeatTimeout = 60 * time.Second | ||
) | ||
|
||
//go:generate mockery -dir . -name HeartbeatTimeoutHandler -case underscore -output ./mocks/ | ||
|
||
// HeartbeatTimeoutHandler defines who to call when a heartbeat timeout expires. | ||
type HeartbeatTimeoutHandler interface { | ||
OnHeartbeatTimeout(view uint64, leaderID uint64) | ||
} | ||
|
||
type HeartbeatMonitor struct { | ||
logger api.Logger | ||
hbTimeout time.Duration | ||
hbInterval time.Duration | ||
comm Comm | ||
handler HeartbeatTimeoutHandler | ||
mutex sync.Mutex | ||
timer *time.Timer | ||
view uint64 | ||
leaderID uint64 | ||
follower bool | ||
} | ||
|
||
func NewHeartbeatMonitor( | ||
logger api.Logger, | ||
heartbeatTimeout time.Duration, | ||
comm Comm, | ||
) *HeartbeatMonitor { | ||
if heartbeatTimeout/10 < time.Nanosecond { | ||
return nil | ||
} | ||
|
||
hm := &HeartbeatMonitor{ | ||
logger: logger, | ||
hbTimeout: heartbeatTimeout, | ||
hbInterval: heartbeatTimeout / 10, | ||
comm: comm, | ||
} | ||
return hm | ||
} | ||
|
||
func (hm *HeartbeatMonitor) SetTimeoutHandler(handler HeartbeatTimeoutHandler) { | ||
// TODO | ||
} | ||
|
||
// StartFollower will start following the heartbeats of the leader of the view. | ||
func (hm *HeartbeatMonitor) StartFollower(view uint64, leaderID uint64) { | ||
// TODO | ||
} | ||
|
||
// StartLeader will start sending heartbeats to all followers. | ||
func (hm *HeartbeatMonitor) StartLeader(view uint64, leaderID uint64) { | ||
// TODO | ||
} | ||
|
||
// ProcessMsg handles an incoming heartbeat. | ||
func (hm *HeartbeatMonitor) ProcessMsg(sender uint64, msg *smartbftprotos.HeartBeat) { | ||
// TODO | ||
} | ||
|
||
// Close stops following or sending heartbeats. | ||
func (hm *HeartbeatMonitor) Close() { | ||
// TODO | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright IBM Corp. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package bft_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/SmartBFT-Go/consensus/internal/bft" | ||
"github.com/SmartBFT-Go/consensus/internal/bft/mocks" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestHeartbeatMonitor_New(t *testing.T) { | ||
basicLog, err := zap.NewDevelopment() | ||
assert.NoError(t, err) | ||
log := basicLog.Sugar() | ||
|
||
comm := &mocks.CommMock{} | ||
handler := &mocks.HeartbeatTimeoutHandler{} | ||
|
||
hm := bft.NewHeartbeatMonitor(log, time.Hour, comm) | ||
assert.NotNil(t, hm) | ||
hm.SetTimeoutHandler(handler) | ||
hm.Close() | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters