@@ -15,6 +15,7 @@ import (
15
15
"github.com/hashicorp/consul/lib"
16
16
"github.com/hashicorp/consul/logging"
17
17
"github.com/hashicorp/consul/tlsutil"
18
+ "github.com/hashicorp/consul/types"
18
19
"github.com/hashicorp/go-hclog"
19
20
"github.com/hashicorp/serf/serf"
20
21
"golang.org/x/time/rate"
@@ -59,9 +60,9 @@ type Client struct {
59
60
// Connection pool to consul servers
60
61
connPool * pool.ConnPool
61
62
62
- // routers is responsible for the selection and maintenance of
63
+ // router is responsible for the selection and maintenance of
63
64
// Consul servers this agent uses for RPC requests
64
- routers * router.Manager
65
+ router * router.Router
65
66
66
67
// rpcLimiter is used to rate limit the total number of RPCs initiated
67
68
// from an agent.
@@ -120,12 +121,14 @@ func NewClient(config *Config, options ...ConsulOption) (*Client, error) {
120
121
}
121
122
}
122
123
124
+ logger := flat .logger .NamedIntercept (logging .ConsulClient )
125
+
123
126
// Create client
124
127
c := & Client {
125
128
config : config ,
126
129
connPool : connPool ,
127
130
eventCh : make (chan serf.Event , serfEventBacklog ),
128
- logger : flat . logger . NamedIntercept ( logging . ConsulClient ) ,
131
+ logger : logger ,
129
132
shutdownCh : make (chan struct {}),
130
133
tlsConfigurator : tlsConfigurator ,
131
134
}
@@ -160,15 +163,22 @@ func NewClient(config *Config, options ...ConsulOption) (*Client, error) {
160
163
return nil , fmt .Errorf ("Failed to start lan serf: %v" , err )
161
164
}
162
165
163
- // Start maintenance task for servers
164
- c .routers = router .New (c .logger , c .shutdownCh , c .serf , c .connPool , "" )
165
- go c .routers .Start ()
166
+ rpcRouter := flat .router
167
+ if rpcRouter == nil {
168
+ rpcRouter = router .NewRouter (logger , config .Datacenter , fmt .Sprintf ("%s.%s" , config .NodeName , config .Datacenter ))
169
+ }
170
+
171
+ if err := rpcRouter .AddArea (types .AreaLAN , c .serf , c .connPool ); err != nil {
172
+ c .Shutdown ()
173
+ return nil , fmt .Errorf ("Failed to add LAN area to the RPC router: %w" , err )
174
+ }
175
+ c .router = rpcRouter
166
176
167
177
// Start LAN event handlers after the router is complete since the event
168
178
// handlers depend on the router and the router depends on Serf.
169
179
go c .lanEventHandler ()
170
180
171
- // This needs to happen after initializing c.routers to prevent a race
181
+ // This needs to happen after initializing c.router to prevent a race
172
182
// condition where the router manager is used when the pointer is nil
173
183
if c .acls .ACLsEnabled () {
174
184
go c .monitorACLMode ()
@@ -276,7 +286,7 @@ func (c *Client) RPC(method string, args interface{}, reply interface{}) error {
276
286
firstCheck := time .Now ()
277
287
278
288
TRY:
279
- server := c .routers . FindServer ()
289
+ manager , server := c .router . FindLANRoute ()
280
290
if server == nil {
281
291
return structs .ErrNoServers
282
292
}
301
311
"error" , rpcErr ,
302
312
)
303
313
metrics .IncrCounterWithLabels ([]string {"client" , "rpc" , "failed" }, 1 , []metrics.Label {{Name : "server" , Value : server .Name }})
304
- c . routers .NotifyFailedServer (server )
314
+ manager .NotifyFailedServer (server )
305
315
if retry := canRetry (args , rpcErr ); ! retry {
306
316
return rpcErr
307
317
}
323
333
// operation.
324
334
func (c * Client ) SnapshotRPC (args * structs.SnapshotRequest , in io.Reader , out io.Writer ,
325
335
replyFn structs.SnapshotReplyFn ) error {
326
- server := c .routers . FindServer ()
336
+ manager , server := c .router . FindLANRoute ()
327
337
if server == nil {
328
338
return structs .ErrNoServers
329
339
}
@@ -339,6 +349,7 @@ func (c *Client) SnapshotRPC(args *structs.SnapshotRequest, in io.Reader, out io
339
349
var reply structs.SnapshotResponse
340
350
snap , err := SnapshotRPC (c .connPool , c .config .Datacenter , server .ShortName , server .Addr , args , in , & reply )
341
351
if err != nil {
352
+ manager .NotifyFailedServer (server )
342
353
return err
343
354
}
344
355
defer func () {
@@ -367,7 +378,7 @@ func (c *Client) SnapshotRPC(args *structs.SnapshotRequest, in io.Reader, out io
367
378
// Stats is used to return statistics for debugging and insight
368
379
// for various sub-systems
369
380
func (c * Client ) Stats () map [string ]map [string ]string {
370
- numServers := c .routers .NumServers ()
381
+ numServers := c .router . GetLANManager () .NumServers ()
371
382
372
383
toString := func (v uint64 ) string {
373
384
return strconv .FormatUint (v , 10 )
0 commit comments