Skip to content

Commit

Permalink
fix: add RaftConfig to Config
Browse files Browse the repository at this point in the history
Signed-off-by: Zixuan Liu <[email protected]>
  • Loading branch information
nodece committed Mar 1, 2021
1 parent 9f869eb commit d9f0241
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hraftdispatcher
import (
"crypto/tls"
"github.com/casbin/casbin/v2"
"github.com/hashicorp/raft"
)

// Config holds dispatcher config.
Expand All @@ -21,4 +22,6 @@ type Config struct {
// You have to provide a peer certificate.
// We recommend using cfssl tool to create this certificates.
TLSConfig *tls.Config
// RaftConfig provides any necessary configuration for the Raft server.
RaftConfig *raft.Config
}
3 changes: 2 additions & 1 deletion dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ func NewHRaftDispatcher(config *Config) (*HRaftDispatcher, error) {
MaxPool: 5,
Logger: nil,
},
Enforcer: config.Enforcer,
Enforcer: config.Enforcer,
RaftConfig: config.RaftConfig,
}
s, err := store.NewStore(storeConfig)
if err != nil {
Expand Down
16 changes: 14 additions & 2 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Store struct {
serverID string

raft *raft.Raft
raftConfig *raft.Config
networkTransportConfig *raft.NetworkTransportConfig
transport raft.Transport
snapshotStore raft.SnapshotStore
Expand All @@ -60,6 +61,7 @@ type Config struct {
Dir string
NetworkTransportConfig *raft.NetworkTransportConfig
Enforcer casbin.IDistributedEnforcer
RaftConfig *raft.Config
}

// NewStore return a instance of Store.
Expand All @@ -70,15 +72,25 @@ func NewStore(config *Config) (*Store, error) {
logger: zap.NewExample(),
networkTransportConfig: config.NetworkTransportConfig,
enforcer: config.Enforcer,
raftConfig: config.RaftConfig,
}

return s, nil
}

// Start performs initialization and runs server
func (s *Store) Start(enableBootstrap bool) error {
config := raft.DefaultConfig()
config.LocalID = raft.ServerID(s.serverID)
var config *raft.Config
if s.raftConfig == nil {
config = raft.DefaultConfig()
s.raftConfig = config
} else {
config = s.raftConfig
}

if len(config.LocalID) == 0 {
config.LocalID = raft.ServerID(s.serverID)
}

var transport raft.Transport
if s.inMemory {
Expand Down

0 comments on commit d9f0241

Please sign in to comment.