flushLog attempts to flush any pending logs to the appserver. It should not be called concurrently.
(force bool)
| 564 | // flushLog attempts to flush any pending logs to the appserver. |
| 565 | // It should not be called concurrently. |
| 566 | func (c *aeContext) flushLog(force bool) (flushed bool) { |
| 567 | c.pendingLogs.Lock() |
| 568 | // Grab up to 30 MB. We can get away with up to 32 MB, but let's be cautious. |
| 569 | n, rem := 0, 30<<20 |
| 570 | for ; n < len(c.pendingLogs.lines); n++ { |
| 571 | ll := c.pendingLogs.lines[n] |
| 572 | // Each log line will require about 3 bytes of overhead. |
| 573 | nb := proto.Size(ll) + 3 |
| 574 | if nb > rem { |
| 575 | break |
| 576 | } |
| 577 | rem -= nb |
| 578 | } |
| 579 | lines := c.pendingLogs.lines[:n] |
| 580 | c.pendingLogs.lines = c.pendingLogs.lines[n:] |
| 581 | c.pendingLogs.Unlock() |
| 582 | |
| 583 | if len(lines) == 0 && !force { |
| 584 | // Nothing to flush. |
| 585 | return false |
| 586 | } |
| 587 | |
| 588 | rescueLogs := false |
| 589 | defer func() { |
| 590 | if rescueLogs { |
| 591 | c.pendingLogs.Lock() |
| 592 | c.pendingLogs.lines = append(lines, c.pendingLogs.lines...) |
| 593 | c.pendingLogs.Unlock() |
| 594 | } |
| 595 | }() |
| 596 | |
| 597 | buf, err := proto.Marshal(&logpb.UserAppLogGroup{ |
| 598 | LogLine: lines, |
| 599 | }) |
| 600 | if err != nil { |
| 601 | log.Printf("internal.flushLog: marshaling UserAppLogGroup: %v", err) |
| 602 | rescueLogs = true |
| 603 | return false |
| 604 | } |
| 605 | |
| 606 | req := &logpb.FlushRequest{ |
| 607 | Logs: buf, |
| 608 | } |
| 609 | res := &basepb.VoidProto{} |
| 610 | c.pendingLogs.Lock() |
| 611 | c.pendingLogs.flushes++ |
| 612 | c.pendingLogs.Unlock() |
| 613 | if err := Call(toContext(c), "logservice", "Flush", req, res); err != nil { |
| 614 | log.Printf("internal.flushLog: Flush RPC: %v", err) |
| 615 | rescueLogs = true |
| 616 | return false |
| 617 | } |
| 618 | return true |
| 619 | } |
| 620 | |
| 621 | const ( |
| 622 | // Log flushing parameters. |
no test coverage detected