keep_alive.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright (c) 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 session
  14. import (
  15. "context"
  16. "sync"
  17. "time"
  18. "github.com/vmware/govmomi/vim25/methods"
  19. "github.com/vmware/govmomi/vim25/soap"
  20. )
  21. type keepAlive struct {
  22. sync.Mutex
  23. roundTripper soap.RoundTripper
  24. idleTime time.Duration
  25. notifyRequest chan struct{}
  26. notifyStop chan struct{}
  27. notifyWaitGroup sync.WaitGroup
  28. // keepAlive executes a request in the background with the purpose of
  29. // keeping the session active. The response for this request is discarded.
  30. keepAlive func(soap.RoundTripper) error
  31. }
  32. func defaultKeepAlive(roundTripper soap.RoundTripper) error {
  33. _, _ = methods.GetCurrentTime(context.Background(), roundTripper)
  34. return nil
  35. }
  36. // KeepAlive wraps the specified soap.RoundTripper and executes a meaningless
  37. // API request in the background after the RoundTripper has been idle for the
  38. // specified amount of idle time. The keep alive process only starts once a
  39. // user logs in and runs until the user logs out again.
  40. func KeepAlive(roundTripper soap.RoundTripper, idleTime time.Duration) soap.RoundTripper {
  41. return KeepAliveHandler(roundTripper, idleTime, defaultKeepAlive)
  42. }
  43. // KeepAliveHandler works as KeepAlive() does, but the handler param can decide how to handle errors.
  44. // For example, if connectivity to ESX/VC is down long enough for a session to expire, a handler can choose to
  45. // Login() on a types.NotAuthenticated error. If handler returns non-nil, the keep alive go routine will be stopped.
  46. func KeepAliveHandler(roundTripper soap.RoundTripper, idleTime time.Duration, handler func(soap.RoundTripper) error) soap.RoundTripper {
  47. k := &keepAlive{
  48. roundTripper: roundTripper,
  49. idleTime: idleTime,
  50. notifyRequest: make(chan struct{}),
  51. }
  52. k.keepAlive = handler
  53. return k
  54. }
  55. func (k *keepAlive) start() {
  56. k.Lock()
  57. defer k.Unlock()
  58. if k.notifyStop != nil {
  59. return
  60. }
  61. // This channel must be closed to terminate idle timer.
  62. k.notifyStop = make(chan struct{})
  63. k.notifyWaitGroup.Add(1)
  64. go func() {
  65. defer k.notifyWaitGroup.Done()
  66. for t := time.NewTimer(k.idleTime); ; {
  67. select {
  68. case <-k.notifyStop:
  69. return
  70. case <-k.notifyRequest:
  71. t.Reset(k.idleTime)
  72. case <-t.C:
  73. if err := k.keepAlive(k.roundTripper); err != nil {
  74. k.stop()
  75. }
  76. t = time.NewTimer(k.idleTime)
  77. }
  78. }
  79. }()
  80. }
  81. func (k *keepAlive) stop() {
  82. k.Lock()
  83. defer k.Unlock()
  84. if k.notifyStop != nil {
  85. close(k.notifyStop)
  86. k.notifyWaitGroup.Wait()
  87. k.notifyStop = nil
  88. }
  89. }
  90. func (k *keepAlive) RoundTrip(ctx context.Context, req, res soap.HasFault) error {
  91. err := k.roundTripper.RoundTrip(ctx, req, res)
  92. if err != nil {
  93. return err
  94. }
  95. // Start ticker on login, stop ticker on logout.
  96. switch req.(type) {
  97. case *methods.LoginBody, *methods.LoginExtensionByCertificateBody, *methods.LoginByTokenBody:
  98. k.start()
  99. case *methods.LogoutBody:
  100. k.stop()
  101. }
  102. return nil
  103. }