Skip to content

Commit 28236ea

Browse files
committed
refactor: use long lived DHCP clients
1 parent c5102cc commit 28236ea

3 files changed

Lines changed: 29 additions & 19 deletions

File tree

dhcp.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"net"
66
"time"
77

8-
dhcp "github.com/insomniacslk/dhcp/dhcpv4"
9-
client "github.com/insomniacslk/dhcp/dhcpv4/client4"
8+
"github.com/insomniacslk/dhcp/dhcpv4"
9+
"github.com/insomniacslk/dhcp/dhcpv4/client4"
1010
"github.com/prometheus/client_golang/prometheus"
1111
"github.com/prometheus/client_golang/prometheus/promauto"
1212
)
@@ -39,9 +39,8 @@ func init() {
3939
prometheus.MustRegister(dhcpLeaseTime)
4040
}
4141

42-
func sampleDhcp(iface string, verbose bool) (net.IP, int, error) {
42+
func sampleDhcp(client client4.Client, verbose bool) (net.IP, int, error) {
4343
dhcpRequests.Inc()
44-
client := client.NewClient()
4544
start := time.Now()
4645
conversation, err := client.Exchange(iface)
4746
if err != nil {
@@ -57,12 +56,12 @@ func sampleDhcp(iface string, verbose bool) (net.IP, int, error) {
5756
fmt.Println(packet.Summary())
5857
}
5958

60-
if packet.MessageType() == dhcp.MessageTypeOffer {
59+
if packet.MessageType() == dhcpv4.MessageTypeOffer {
6160
dhcpOffers.Inc()
6261
}
6362

64-
if packet.MessageType() == dhcp.MessageTypeAck {
65-
if packet.Options.Has(dhcp.OptionIPAddressLeaseTime) {
63+
if packet.MessageType() == dhcpv4.MessageTypeAck {
64+
if packet.Options.Has(dhcpv4.OptionIPAddressLeaseTime) {
6665
dhcpLeaseTime.Set(packet.IPAddressLeaseTime(0).Seconds())
6766
}
6867

dhcp6.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"net"
66
"time"
77

8-
dhcp "github.com/insomniacslk/dhcp/dhcpv6"
9-
client "github.com/insomniacslk/dhcp/dhcpv6/client6"
8+
"github.com/insomniacslk/dhcp/dhcpv6"
9+
"github.com/insomniacslk/dhcp/dhcpv6/client6"
1010
"github.com/prometheus/client_golang/prometheus"
1111
"github.com/prometheus/client_golang/prometheus/promauto"
1212
)
@@ -39,9 +39,8 @@ func init() {
3939
prometheus.MustRegister(dhcp6Lifetime)
4040
}
4141

42-
func sampleDhcp6(iface string, verbose bool) (net.IP, int, error) {
42+
func sampleDhcp6(client client6.Client, verbose bool) (net.IP, int, error) {
4343
dhcp6Requests.Inc()
44-
client := client.NewClient()
4544
start := time.Now()
4645
conversation, err := client.Exchange(iface)
4746
if err != nil {
@@ -63,7 +62,7 @@ func sampleDhcp6(iface string, verbose bool) (net.IP, int, error) {
6362
return nil, 0, err
6463
}
6564

66-
if message.MessageType == dhcp.MessageTypeReply {
65+
if message.MessageType == dhcpv6.MessageTypeReply {
6766
dhcp6Replies.Inc()
6867

6968
iaNa := message.Options.OneIANA()
@@ -73,5 +72,5 @@ func sampleDhcp6(iface string, verbose bool) (net.IP, int, error) {
7372
yourIPAddr = naAddr.IPv6Addr
7473
}
7574
}
76-
return yourIPAddr, prefixBits, err
75+
return yourIPAddr, prefixBits, nil
7776
}

main.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strings"
1212
"time"
1313

14+
"github.com/insomniacslk/dhcp/dhcpv4/client4"
15+
"github.com/insomniacslk/dhcp/dhcpv6/client6"
1416
"github.com/prometheus/client_golang/prometheus/promhttp"
1517
"github.com/vishvananda/netlink"
1618
)
@@ -66,19 +68,29 @@ func main() {
6668
dnsTargetsList := strings.Split(dnsTargets, ",")
6769
icmpTargetsList := strings.Split(icmpTargets, ",")
6870

71+
ipv4DhcpClient := client4.Client{
72+
ReadTimeout: interval,
73+
WriteTimeout: interval,
74+
}
75+
76+
ipv6DhcpClient := client6.Client{
77+
ReadTimeout: interval,
78+
WriteTimeout: interval,
79+
}
80+
6981
http.Handle("/metrics", promhttp.Handler())
7082

7183
go func() {
7284
for {
7385

7486
if !disable4 {
75-
if err := observeIPv4(link); err != nil {
87+
if err := observeIPv4(link, ipv4DhcpClient); err != nil {
7688
slog.Warn("issue with ipv4", "err", err)
7789
}
7890
}
7991

8092
if !disable6 {
81-
if err := observeIPv6(link); err != nil {
93+
if err := observeIPv6(link, ipv6DhcpClient); err != nil {
8294
slog.Warn("issue with ipv6", "err", err)
8395
}
8496
}
@@ -104,8 +116,8 @@ func main() {
104116
}
105117
}
106118

107-
func observeIPv4(link netlink.Link) error {
108-
ip, prefix, err := sampleDhcp(iface, verbose)
119+
func observeIPv4(link netlink.Link, client client4.Client) error {
120+
ip, prefix, err := sampleDhcp(client, verbose)
109121
if err != nil {
110122
return fmt.Errorf("DHCPv4 sampling failed: %w", err)
111123
}
@@ -135,8 +147,8 @@ func observeIPv4(link netlink.Link) error {
135147
return nil
136148
}
137149

138-
func observeIPv6(link netlink.Link) error {
139-
ip6, prefix6, err := sampleDhcp6(iface, verbose)
150+
func observeIPv6(link netlink.Link, client client6.Client) error {
151+
ip6, prefix6, err := sampleDhcp6(client, verbose)
140152
if err != nil {
141153
return fmt.Errorf("DHCPv6 sampling failed: %w", err)
142154
}

0 commit comments

Comments
 (0)