File: remote.go

package info (click to toggle)
golang-github-digitalocean-go-libvirt 0.0~git20250902.14aca49-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,188 kB
  • sloc: yacc: 188; sh: 76; xml: 50; makefile: 3
file content (61 lines) | stat: -rw-r--r-- 1,297 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package dialers

import (
	"net"
	"time"
)

const (
	// defaultRemotePort specifies the default libvirtd port.
	defaultRemotePort = "16509"

	// defaultRemoteTimeout specifies the default libvirt dial timeout.
	defaultRemoteTimeout = 20 * time.Second
)

// Remote implements connecting to a remote server's libvirt using tcp
type Remote struct {
	timeout    time.Duration
	host, port string
}

// RemoteOption is a function for setting remote dialer options.
type RemoteOption func(*Remote)

// WithRemoteTimeout sets the dial timeout.
func WithRemoteTimeout(timeout time.Duration) RemoteOption {
	return func(r *Remote) {
		r.timeout = timeout
	}
}

// UsePort sets the port to dial for libirt on the target host server.
func UsePort(port string) RemoteOption {
	return func(r *Remote) {
		r.port = port
	}
}

// NewRemote is a dialer for connecting to libvirt running on another server.
func NewRemote(hostAddr string, opts ...RemoteOption) *Remote {
	r := &Remote{
		timeout: defaultRemoteTimeout,
		host:    hostAddr,
		port:    defaultRemotePort,
	}

	for _, opt := range opts {
		opt(r)
	}

	return r
}

// Dial connects to libvirt running on another server.
func (r *Remote) Dial() (net.Conn, error) {
	return net.DialTimeout(
		"tcp",
		net.JoinHostPort(r.host, r.port),
		r.timeout,
	)
}