github.com/vmware/govmomi@v0.51.0/session/keepalive/example_test.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package keepalive_test
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"log"
    11  	"time"
    12  
    13  	"github.com/vmware/govmomi"
    14  	"github.com/vmware/govmomi/session/keepalive"
    15  	"github.com/vmware/govmomi/simulator"
    16  	"github.com/vmware/govmomi/simulator/sim25"
    17  	"github.com/vmware/govmomi/vapi/rest"
    18  	"github.com/vmware/govmomi/vim25"
    19  )
    20  
    21  var (
    22  	sessionCheckPause  = time.Second / 2
    23  	sessionIdleTimeout = sessionCheckPause / 2
    24  	keepAliveIdle      = sessionIdleTimeout / 2
    25  )
    26  
    27  func ExampleHandlerSOAP() {
    28  	simulator.Run(func(ctx context.Context, vc *vim25.Client) error {
    29  		// Using the authenticated vc client, timeout will apply to new sessions.
    30  		sim25.SetSessionTimeout(ctx, vc, sessionIdleTimeout)
    31  
    32  		c, err := govmomi.NewClient(ctx, vc.URL(), true)
    33  		if err != nil {
    34  			return err
    35  		}
    36  		m := c.SessionManager
    37  
    38  		err = m.Login(ctx, simulator.DefaultLogin) // New session
    39  		if err != nil {
    40  			return err
    41  		}
    42  
    43  		// check twice if session is valid, sleeping > SessionIdleTimeout in between
    44  		check := func() {
    45  			for i := 0; i < 2; i++ {
    46  				s, err := m.UserSession(ctx)
    47  				if err != nil {
    48  					log.Fatal(err)
    49  				}
    50  
    51  				fmt.Printf("session valid=%t\n", s != nil)
    52  				if i == 0 {
    53  					time.Sleep(sessionCheckPause)
    54  				}
    55  			}
    56  		}
    57  
    58  		// session will expire here
    59  		check()
    60  
    61  		// this starts the keep alive handler when Login is called, and stops the handler when Logout is called
    62  		c.RoundTripper = keepalive.NewHandlerSOAP(c.RoundTripper, keepAliveIdle, nil)
    63  
    64  		err = m.Login(ctx, simulator.DefaultLogin)
    65  		if err != nil {
    66  			return err
    67  		}
    68  
    69  		// session will not expire here, with the keep alive handler in place.
    70  		check()
    71  
    72  		err = m.Logout(ctx)
    73  		if err != nil {
    74  			return err
    75  		}
    76  
    77  		// Logout() also stops the keep alive handler, session is no longer valid.
    78  		check()
    79  
    80  		return nil
    81  	})
    82  	// Output:
    83  	// session valid=true
    84  	// session valid=false
    85  	// session valid=true
    86  	// session valid=true
    87  	// session valid=false
    88  	// session valid=false
    89  }
    90  
    91  func ExampleHandlerREST() {
    92  	simulator.Run(func(ctx context.Context, vc *vim25.Client) error {
    93  		// Using the authenticated vc client, timeout will apply to new sessions.
    94  		sim25.SetSessionTimeout(ctx, vc, sessionIdleTimeout)
    95  
    96  		c := rest.NewClient(vc)
    97  		err := c.Login(ctx, simulator.DefaultLogin) // New session
    98  		if err != nil {
    99  			return err
   100  		}
   101  
   102  		// check twice if session is valid, sleeping > SessionIdleTimeout in between.
   103  		check := func() {
   104  			for i := 0; i < 2; i++ {
   105  				s, err := c.Session(ctx)
   106  				if err != nil {
   107  					log.Fatal(err)
   108  				}
   109  
   110  				fmt.Printf("session valid=%t\n", s != nil)
   111  				if i == 0 {
   112  					time.Sleep(sessionCheckPause)
   113  				}
   114  			}
   115  		}
   116  
   117  		// session will expire here
   118  		check()
   119  
   120  		// this starts the keep alive handler when Login is called, and stops the handler when Logout is called
   121  		c.Transport = keepalive.NewHandlerREST(c, keepAliveIdle, nil)
   122  
   123  		err = c.Login(ctx, simulator.DefaultLogin)
   124  		if err != nil {
   125  			return err
   126  		}
   127  
   128  		// session will not expire here, with the keep alive handler in place.
   129  		check()
   130  
   131  		err = c.Logout(ctx)
   132  		if err != nil {
   133  			return err
   134  		}
   135  
   136  		// Logout() also stops the keep alive handler, session is no longer valid.
   137  		check()
   138  
   139  		return nil
   140  	})
   141  	// Output:
   142  	// session valid=true
   143  	// session valid=false
   144  	// session valid=true
   145  	// session valid=true
   146  	// session valid=false
   147  	// session valid=false
   148  }