(ctx context.Context, client *home.Client)
| 1487 | } |
| 1488 | |
| 1489 | func (s *Service) startHomeUsageForwarder(ctx context.Context, client *home.Client) { |
| 1490 | if s == nil || client == nil { |
| 1491 | return |
| 1492 | } |
| 1493 | if ctx == nil { |
| 1494 | ctx = context.Background() |
| 1495 | } |
| 1496 | |
| 1497 | sleep := func(d time.Duration) bool { |
| 1498 | if d <= 0 { |
| 1499 | return true |
| 1500 | } |
| 1501 | timer := time.NewTimer(d) |
| 1502 | defer timer.Stop() |
| 1503 | select { |
| 1504 | case <-ctx.Done(): |
| 1505 | return false |
| 1506 | case <-timer.C: |
| 1507 | return true |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | go func() { |
| 1512 | for { |
| 1513 | select { |
| 1514 | case <-ctx.Done(): |
| 1515 | return |
| 1516 | default: |
| 1517 | } |
| 1518 | |
| 1519 | if !client.HeartbeatOK() { |
| 1520 | if !sleep(time.Second) { |
| 1521 | return |
| 1522 | } |
| 1523 | continue |
| 1524 | } |
| 1525 | |
| 1526 | items := redisqueue.PopOldest(64) |
| 1527 | if len(items) == 0 { |
| 1528 | if !sleep(500 * time.Millisecond) { |
| 1529 | return |
| 1530 | } |
| 1531 | continue |
| 1532 | } |
| 1533 | |
| 1534 | for i := range items { |
| 1535 | if errPush := client.LPushUsage(ctx, items[i]); errPush != nil { |
| 1536 | for j := i; j < len(items); j++ { |
| 1537 | redisqueue.Enqueue(items[j]) |
| 1538 | } |
| 1539 | if !sleep(time.Second) { |
| 1540 | return |
| 1541 | } |
| 1542 | break |
| 1543 | } |
| 1544 | } |
| 1545 | } |
| 1546 | }() |
no test coverage detected