lease_updater.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package nfc
  14. import (
  15. "context"
  16. "log"
  17. "net/url"
  18. "sync"
  19. "sync/atomic"
  20. "time"
  21. "github.com/vmware/govmomi/vim25/progress"
  22. "github.com/vmware/govmomi/vim25/types"
  23. )
  24. type FileItem struct {
  25. types.OvfFileItem
  26. URL *url.URL
  27. ch chan progress.Report
  28. }
  29. func NewFileItem(u *url.URL, item types.OvfFileItem) FileItem {
  30. return FileItem{
  31. OvfFileItem: item,
  32. URL: u,
  33. ch: make(chan progress.Report),
  34. }
  35. }
  36. func (o FileItem) Sink() chan<- progress.Report {
  37. return o.ch
  38. }
  39. // File converts the FileItem.OvfFileItem to an OvfFile
  40. func (o FileItem) File() types.OvfFile {
  41. return types.OvfFile{
  42. DeviceId: o.DeviceId,
  43. Path: o.Path,
  44. Size: o.Size,
  45. }
  46. }
  47. type LeaseUpdater struct {
  48. pos int64 // Number of bytes (keep first to ensure 64 bit aligment)
  49. total int64 // Total number of bytes (keep first to ensure 64 bit aligment)
  50. lease *Lease
  51. done chan struct{} // When lease updater should stop
  52. wg sync.WaitGroup // Track when update loop is done
  53. }
  54. func newLeaseUpdater(ctx context.Context, lease *Lease, info *LeaseInfo) *LeaseUpdater {
  55. l := LeaseUpdater{
  56. lease: lease,
  57. done: make(chan struct{}),
  58. }
  59. for _, item := range info.Items {
  60. l.total += item.Size
  61. go l.waitForProgress(item)
  62. }
  63. // Kickstart update loop
  64. l.wg.Add(1)
  65. go l.run()
  66. return &l
  67. }
  68. func (l *LeaseUpdater) waitForProgress(item FileItem) {
  69. var pos, total int64
  70. total = item.Size
  71. for {
  72. select {
  73. case <-l.done:
  74. return
  75. case p, ok := <-item.ch:
  76. // Return in case of error
  77. if ok && p.Error() != nil {
  78. return
  79. }
  80. if !ok {
  81. // Last element on the channel, add to total
  82. atomic.AddInt64(&l.pos, total-pos)
  83. return
  84. }
  85. // Approximate progress in number of bytes
  86. x := int64(float32(total) * (p.Percentage() / 100.0))
  87. atomic.AddInt64(&l.pos, x-pos)
  88. pos = x
  89. }
  90. }
  91. }
  92. func (l *LeaseUpdater) run() {
  93. defer l.wg.Done()
  94. tick := time.NewTicker(2 * time.Second)
  95. defer tick.Stop()
  96. for {
  97. select {
  98. case <-l.done:
  99. return
  100. case <-tick.C:
  101. // From the vim api HttpNfcLeaseProgress(percent) doc, percent ==
  102. // "Completion status represented as an integer in the 0-100 range."
  103. // Always report the current value of percent, as it will renew the
  104. // lease even if the value hasn't changed or is 0.
  105. percent := int32(float32(100*atomic.LoadInt64(&l.pos)) / float32(l.total))
  106. err := l.lease.Progress(context.TODO(), percent)
  107. if err != nil {
  108. log.Printf("NFC lease progress: %s", err)
  109. return
  110. }
  111. }
  112. }
  113. }
  114. func (l *LeaseUpdater) Done() {
  115. close(l.done)
  116. l.wg.Wait()
  117. }