code: func periodicallyRedialingConn(ctx context.Context, renewPeriod time.Duration, dial func() (net.Conn, error)) (func() net.Conn, error) {
  	var (
  		err error
  
  		// guard access to c
  		mu sync.RWMutex
  		c  net.Conn
  	)
  
  	// get an initial connection
  	if c, err = dial(); err != nil {
  		return nil, err
  	}
  
  	// periodically re-dial
  	go func() {
  		ticker := time.NewTicker(renewPeriod)
  		for {
  			select {
  			case <-ticker.C:
  				newConn, err := dial()
  				if err != nil {
  					continue // we don't have anything better to replace `c` with
  				}
  				mu.Lock()
  				c = newConn
  				mu.Unlock()
  			case <-ctx.Done():
  				return
  			}
  		}
  	}()
  
  	return func() net.Conn {
  		mu.RLock()
  		defer mu.RUnlock()
  		return c
  	}, nil
  }
docs: // periodicallyRedialingConn creates a goroutine to periodically re-dial a connection, so the hostname can be
  // re-resolved if the IP changes.
  // Returns a func that provides the latest Conn value.