package keepalive
import "github.com/vmware/govmomi/session/keepalive"
Index ¶
- type HandlerREST
- func NewHandlerREST(c *rest.Client, idle time.Duration, send func() error) *HandlerREST
- func (h *HandlerREST) RoundTrip(req *http.Request) (*http.Response, error)
- func (h HandlerREST) Start()
- func (h HandlerREST) Stop()
- type HandlerSOAP
Examples ¶
Types ¶
type HandlerREST ¶
type HandlerREST struct {
// contains filtered or unexported fields
}
HandlerREST is a keep alive implementation for use with rest.Client
Code:play
Output:Example¶
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/vmware/govmomi/session/keepalive"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/vapi/rest"
"github.com/vmware/govmomi/vim25"
)
var (
sessionCheckPause = time.Second / 2
sessionIdleTimeout = sessionCheckPause / 2
keepAliveIdle = sessionIdleTimeout / 2
)
func main() {
simulator.Run(func(ctx context.Context, vc *vim25.Client) error {
c := rest.NewClient(vc)
err := c.Login(ctx, simulator.DefaultLogin)
if err != nil {
return err
}
// check twice if session is valid, sleeping > SessionIdleTimeout in between.
check := func() {
for i := 0; i < 2; i++ {
s, err := c.Session(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("session valid=%t\n", s != nil)
if i == 0 {
time.Sleep(sessionCheckPause)
}
}
}
// session will expire here
check()
// this starts the keep alive handler when Login is called, and stops the handler when Logout is called
c.Transport = keepalive.NewHandlerREST(c, keepAliveIdle, nil)
err = c.Login(ctx, simulator.DefaultLogin)
if err != nil {
return err
}
// session will not expire here, with the keep alive handler in place.
check()
err = c.Logout(ctx)
if err != nil {
return err
}
// Logout() also stops the keep alive handler, session is no longer valid.
check()
return nil
})
}
session valid=true
session valid=false
session valid=true
session valid=true
session valid=false
session valid=false
func NewHandlerREST ¶
NewHandlerREST returns an http.RoundTripper for use with a rest.Client The idle time specifies the interval in between send() requests. Defaults to 10 minutes. The send func is used to keep a session alive. Defaults to calling the rest.Client.Session() method The keep alive goroutine starts when a Login method is called and runs until Logout is called or send returns an error.
func (*HandlerREST) RoundTrip ¶
RoundTrip implements http.RoundTripper
func (HandlerREST) Start ¶
func (h HandlerREST) Start()
Start explicitly starts the keep alive go routine. For use with session cache.Client, as cached sessions may not involve Login/Logout via RoundTripper.
func (HandlerREST) Stop ¶
func (h HandlerREST) Stop()
Stop explicitly stops the keep alive go routine. For use with session cache.Client, as cached sessions may not involve Login/Logout via RoundTripper.
type HandlerSOAP ¶
type HandlerSOAP struct {
// contains filtered or unexported fields
}
HandlerSOAP is a keep alive implementation for use with vim25.Client
Code:play
Output:Example¶
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/vmware/govmomi/session"
"github.com/vmware/govmomi/session/keepalive"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/vim25"
)
var (
sessionCheckPause = time.Second / 2
sessionIdleTimeout = sessionCheckPause / 2
keepAliveIdle = sessionIdleTimeout / 2
)
func main() {
simulator.Run(func(ctx context.Context, c *vim25.Client) error {
// No need for initial Login() here as simulator.Run already has
m := session.NewManager(c)
// check twice if session is valid, sleeping > SessionIdleTimeout in between
check := func() {
for i := 0; i < 2; i++ {
s, err := m.UserSession(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("session valid=%t\n", s != nil)
if i == 0 {
time.Sleep(sessionCheckPause)
}
}
}
// session will expire here
check()
// this starts the keep alive handler when Login is called, and stops the handler when Logout is called
c.RoundTripper = keepalive.NewHandlerSOAP(c.RoundTripper, keepAliveIdle, nil)
err := m.Login(ctx, simulator.DefaultLogin)
if err != nil {
return err
}
// session will not expire here, with the keep alive handler in place.
check()
err = m.Logout(ctx)
if err != nil {
return err
}
// Logout() also stops the keep alive handler, session is no longer valid.
check()
return nil
})
}
session valid=true
session valid=false
session valid=true
session valid=true
session valid=false
session valid=false
func NewHandlerSOAP ¶
func NewHandlerSOAP(c soap.RoundTripper, idle time.Duration, send func() error) *HandlerSOAP
NewHandlerSOAP returns a soap.RoundTripper for use with a vim25.Client The idle time specifies the interval in between send() requests. Defaults to 10 minutes. The send func is used to keep a session alive. Defaults to calling vim25 GetCurrentTime(). The keep alive goroutine starts when a Login method is called and runs until Logout is called or send returns an error.
func (*HandlerSOAP) RoundTrip ¶
RoundTrip implements soap.RoundTripper
func (HandlerSOAP) Start ¶
func (h HandlerSOAP) Start()
Start explicitly starts the keep alive go routine. For use with session cache.Client, as cached sessions may not involve Login/Logout via RoundTripper.
func (HandlerSOAP) Stop ¶
func (h HandlerSOAP) Stop()
Stop explicitly stops the keep alive go routine. For use with session cache.Client, as cached sessions may not involve Login/Logout via RoundTripper.
Source Files ¶
- Version
- v0.48.1 (latest)
- Published
- Feb 11, 2025
- Platform
- linux/amd64
- Imports
- 8 packages
- Last checked
- 2 months ago –
Tools for package owners.