| 1 |
// Copyright 2026 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package server |
| 15 |
|
| 16 |
// raftTransport is an interface that defines the communication |
| 17 |
// mechanism for Raft nodes. |
| 18 |
type raftTransport interface { |
| 19 |
// Node returns the RaftNode associated with this transport. |
| 20 |
Node() RaftNode |
| 21 |
|
| 22 |
// Account returns the NATS Account this transport operates within. |
| 23 |
Account() *Account |
| 24 |
|
| 25 |
// Reset reconfigures the transport for a new account. |
| 26 |
// This involves tearing down existing client resources and |
| 27 |
// setting up new ones for the provided account. |
| 28 |
Reset(acc *Account) |
| 29 |
|
| 30 |
// Close shuts down the transport, releasing any associated resources |
| 31 |
// like internal clients and subscriptions. |
| 32 |
Close() |
| 33 |
|
| 34 |
// Publish sends a message to the specified subject. |
| 35 |
Publish(subject string, reply string, msg []byte) |
| 36 |
|
| 37 |
// Subscribe creates a subscription for to the specified subject. |
| 38 |
Subscribe(subject string, cb msgHandler) (*subscription, error) |
| 39 |
|
| 40 |
// Unsubscribe removes a previously established subscription. |
| 41 |
Unsubscribe(sub *subscription) |
| 42 |
} |
| 43 |
|
| 44 |
type newTransportFunc func(*Server, RaftNode) raftTransport |
| 45 |
|
| 46 |
// defaultTransport is the default implementation of the raftTransport interface. |
| 47 |
// It uses an internal NATS client to allow communication between Raft nodes. |
| 48 |
type defaultTransport struct { |
| 49 |
n RaftNode |
| 50 |
s *Server |
| 51 |
c *client |
| 52 |
sq *sendq |
| 53 |
acc *Account |
| 54 |
} |
| 55 |
|
| 56 |
func defaultRaftTransport(server *Server, raft RaftNode) raftTransport { |
| 57 |
return &defaultTransport{s: server, n: raft} |
| 58 |
} |
| 59 |
|
| 60 |
func (t *defaultTransport) Node() RaftNode { |
| 61 |
return t.n |
| 62 |
} |
| 63 |
|
| 64 |
func (t *defaultTransport) Account() *Account { |
| 65 |
return t.acc |
| 66 |
} |
| 67 |
|
| 68 |
func (t *defaultTransport) Reset(acc *Account) { |
| 69 |
t.Close() |
| 70 |
|
| 71 |
t.c = t.s.createInternalSystemClient() |
| 72 |
t.c.registerWithAccount(acc) |
| 73 |
if acc.sq == nil { |
| 74 |
acc.sq = t.s.newSendQ(acc) |
| 75 |
} |
| 76 |
t.sq = acc.sq |
| 77 |
t.acc = acc |
| 78 |
} |
| 79 |
|
| 80 |
func (t *defaultTransport) Close() { |
| 81 |
if c := t.c; c != nil { |
| 82 |
c.mu.Lock() |
| 83 |
subs := make([]*subscription, 0, len(c.subs)) |
| 84 |
for _, sub := range c.subs { |
| 85 |
subs = append(subs, sub) |
| 86 |
} |
| 87 |
c.mu.Unlock() |
| 88 |
for _, sub := range subs { |
| 89 |
t.Unsubscribe(sub) |
| 90 |
} |
| 91 |
c.closeConnection(InternalClient) |
| 92 |
t.c = nil |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func (t *defaultTransport) Publish(subject string, reply string, msg []byte) { |
| 97 |
if t.sq == nil { |
| 98 |
return |
| 99 |
} |
| 100 |
t.sq.send(subject, reply, nil, msg) |
| 101 |
} |
| 102 |
|
| 103 |
func (t *defaultTransport) Subscribe(subject string, cb msgHandler) (*subscription, error) { |
| 104 |
if t.c == nil { |
| 105 |
return nil, errNoInternalClient |
| 106 |
} |
| 107 |
return t.s.systemSubscribe(subject, _EMPTY_, false, t.c, cb) |
| 108 |
} |
| 109 |
|
| 110 |
func (t *defaultTransport) Unsubscribe(sub *subscription) { |
| 111 |
if t.c != nil && sub != nil { |
| 112 |
t.c.processUnsub(sub.sid) |
| 113 |
} |
| 114 |
} |