SetAddrs replaces the shards in use, such that you can increase and decrease number of shards, that you use. It will reuse shards that existed before and close the ones that will not be used anymore.
(addrs map[string]string)
| 352 | // decrease number of shards, that you use. It will reuse shards that |
| 353 | // existed before and close the ones that will not be used anymore. |
| 354 | func (c *ringSharding) SetAddrs(addrs map[string]string) { |
| 355 | c.setAddrsMu.Lock() |
| 356 | defer c.setAddrsMu.Unlock() |
| 357 | |
| 358 | cleanup := func(shards map[string]*ringShard) { |
| 359 | for addr, shard := range shards { |
| 360 | if err := shard.Client.Close(); err != nil { |
| 361 | internal.Logger.Printf(context.Background(), "shard.Close %s failed: %s", addr, err) |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | c.mu.RLock() |
| 367 | if c.closed { |
| 368 | c.mu.RUnlock() |
| 369 | return |
| 370 | } |
| 371 | existing := c.shards |
| 372 | c.mu.RUnlock() |
| 373 | |
| 374 | shards, created, unused := c.newRingShards(addrs, existing) |
| 375 | |
| 376 | c.mu.Lock() |
| 377 | if c.closed { |
| 378 | cleanup(created) |
| 379 | c.mu.Unlock() |
| 380 | return |
| 381 | } |
| 382 | c.shards = shards |
| 383 | c.rebalanceLocked() |
| 384 | c.mu.Unlock() |
| 385 | |
| 386 | cleanup(unused) |
| 387 | } |
| 388 | |
| 389 | func (c *ringSharding) newRingShards( |
| 390 | addrs map[string]string, existing *ringShards, |
no test coverage detected