@@ -155,7 +155,6 @@ type raft struct {
155155
156156 created time.Time // Time that the group was created
157157 accName string // Account name of the asset this raft group is for
158- acc * Account // Account that NRG traffic will be sent/received in
159158 group string // Raft group
160159 sd string // Store directory
161160 id string // Node ID
@@ -202,7 +201,6 @@ type raft struct {
202201 vote string // Our current vote state
203202
204203 s * Server // Reference to top-level server
205- c * client // Internal client for subscriptions
206204 js * jetStream // JetStream, if running, to see if we are out of resources
207205
208206 hasleader atomic.Bool // Is there a group leader right now?
@@ -221,7 +219,7 @@ type raft struct {
221219 asubj string // Append entries subject
222220 areply string // Append entries responses subject
223221
224- sq * sendq // Send queue for outbound RPC messages
222+ t raftTransport // Transport that handles Raft messaging
225223 aesub * subscription // Subscription for handleAppendEntry callbacks
226224
227225 wtv []byte // Term and vote to be written
@@ -319,6 +317,8 @@ type RaftConfig struct {
319317 // We need to protect against losing state due to the new peers starting with an empty log.
320318 // Therefore, these empty servers can't try to become leader until they at least have _some_ state.
321319 ScaleUp bool
320+
321+ Transport raftTransportFactory
322322}
323323
324324var (
@@ -464,6 +464,12 @@ func (s *Server) initRaftNode(accName string, cfg *RaftConfig, labels pprofLabel
464464 observer : cfg .Observer ,
465465 }
466466
467+ factory := cfg .Transport
468+ if factory == nil {
469+ factory = defaultRaftTransport
470+ }
471+ n .t = factory (s , n )
472+
467473 // Setup our internal subscriptions for proposals, votes and append entries.
468474 // If we fail to do this for some reason then this is fatal — we cannot
469475 // continue setting up or the Raft node may be partially/totally isolated.
@@ -658,7 +664,10 @@ func (n *raft) IsSystemAccount() bool {
658664func (n * raft ) GetTrafficAccountName () string {
659665 n .RLock ()
660666 defer n .RUnlock ()
661- return n .acc .GetName ()
667+ if n .t == nil {
668+ return (* Account )(nil ).GetName ()
669+ }
670+ return n .t .Account ().GetName ()
662671}
663672
664673func (n * raft ) RecreateInternalSubs () error {
@@ -708,7 +717,7 @@ func (n *raft) recreateInternalSubsLocked() error {
708717 }
709718 }
710719 }
711- if n .aesub != nil && n .acc == nrgAcc {
720+ if n .aesub != nil && n .t . Account () == nrgAcc {
712721 // Subscriptions already exist and the account NRG state
713722 // hasn't changed.
714723 return nil
@@ -719,33 +728,11 @@ func (n *raft) recreateInternalSubsLocked() error {
719728 // the next step...
720729 n .cancelCatchup ()
721730
722- // If we have an existing client then tear down any existing
723- // subscriptions and close the internal client.
724- if c := n .c ; c != nil {
725- c .mu .Lock ()
726- subs := make ([]* subscription , 0 , len (c .subs ))
727- for _ , sub := range c .subs {
728- subs = append (subs , sub )
729- }
730- c .mu .Unlock ()
731- for _ , sub := range subs {
732- n .unsubscribe (sub )
733- }
734- c .closeConnection (InternalClient )
735- }
736-
737- if n .acc != nrgAcc {
731+ if n .t .Account () != nrgAcc {
738732 n .debug ("Subscribing in '%s'" , nrgAcc .GetName ())
739733 }
740734
741- c := n .s .createInternalSystemClient ()
742- c .registerWithAccount (nrgAcc )
743- if nrgAcc .sq == nil {
744- nrgAcc .sq = n .s .newSendQ (nrgAcc )
745- }
746- n .c = c
747- n .sq = nrgAcc .sq
748- n .acc = nrgAcc
735+ n .t .Reset (nrgAcc )
749736
750737 // Recreate any internal subscriptions for voting, append
751738 // entries etc in the new account.
@@ -2325,17 +2312,12 @@ func (n *raft) newInbox() string {
23252312// Our internal subscribe.
23262313// Lock should be held.
23272314func (n * raft ) subscribe (subject string , cb msgHandler ) (* subscription , error ) {
2328- if n .c == nil {
2329- return nil , errNoInternalClient
2330- }
2331- return n .s .systemSubscribe (subject , _EMPTY_ , false , n .c , cb )
2315+ return n .t .Subscribe (subject , cb )
23322316}
23332317
23342318// Lock should be held.
23352319func (n * raft ) unsubscribe (sub * subscription ) {
2336- if n .c != nil && sub != nil {
2337- n .c .processUnsub (sub .sid )
2338- }
2320+ n .t .Unsubscribe (sub )
23392321}
23402322
23412323// Lock should be held.
@@ -2460,19 +2442,7 @@ runner:
24602442 n .Lock ()
24612443 defer n .Unlock ()
24622444
2463- if c := n .c ; c != nil {
2464- var subs []* subscription
2465- c .mu .Lock ()
2466- for _ , sub := range c .subs {
2467- subs = append (subs , sub )
2468- }
2469- c .mu .Unlock ()
2470- for _ , sub := range subs {
2471- n .unsubscribe (sub )
2472- }
2473- c .closeConnection (InternalClient )
2474- n .c = nil
2475- }
2445+ n .t .Close ()
24762446
24772447 // Unregistering ipQueues do not prevent them from push/pop
24782448 // just will remove them from the central monitoring map
@@ -5131,15 +5101,11 @@ func (n *raft) requestVote() {
51315101}
51325102
51335103func (n * raft ) sendRPC (subject , reply string , msg []byte ) {
5134- if n .sq != nil {
5135- n .sq .send (subject , reply , nil , msg )
5136- }
5104+ n .t .Publish (subject , reply , msg )
51375105}
51385106
51395107func (n * raft ) sendReply (subject string , msg []byte ) {
5140- if n .sq != nil {
5141- n .sq .send (subject , _EMPTY_ , nil , msg )
5142- }
5108+ n .t .Publish (subject , _EMPTY_ , msg )
51435109}
51445110
51455111func (n * raft ) wonElection (votes int ) bool {
0 commit comments