keepalive

package
v0.37.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2024 License: Apache-2.0 Imports: 8 Imported by: 16

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HandlerREST

type HandlerREST struct {
	// contains filtered or unexported fields
}

HandlerREST is a keep alive implementation for use with rest.Client

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
	})
}
Output:

session valid=true
session valid=false
session valid=true
session valid=true
session valid=false
session valid=false

func NewHandlerREST

func NewHandlerREST(c *rest.Client, idle time.Duration, send func() error) *HandlerREST

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

func (h *HandlerREST) RoundTrip(req *http.Request) (*http.Response, error)

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

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
	})
}
Output:

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

func (h *HandlerSOAP) RoundTrip(ctx context.Context, req, res soap.HasFault) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL