2015-10-08 16:45:23 +00:00
|
|
|
package subscriber
|
|
|
|
|
|
|
|
import (
|
2021-07-08 19:00:40 +00:00
|
|
|
"context"
|
2015-10-08 16:45:23 +00:00
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2015-11-18 09:32:39 +00:00
|
|
|
// UDP supports writing points over UDP using the line protocol.
|
2015-10-08 16:45:23 +00:00
|
|
|
type UDP struct {
|
|
|
|
addr string
|
|
|
|
}
|
|
|
|
|
2015-11-18 09:32:39 +00:00
|
|
|
// NewUDP returns a new UDP listener with default options.
|
2015-10-08 16:45:23 +00:00
|
|
|
func NewUDP(addr string) *UDP {
|
|
|
|
return &UDP{addr: addr}
|
|
|
|
}
|
|
|
|
|
2015-11-18 09:32:39 +00:00
|
|
|
// WritePoints writes points over UDP transport.
|
2021-07-08 19:00:40 +00:00
|
|
|
func (u *UDP) WritePointsContext(_ context.Context, request WriteRequest) (err error) {
|
2015-10-08 16:45:23 +00:00
|
|
|
var addr *net.UDPAddr
|
|
|
|
var con *net.UDPConn
|
|
|
|
addr, err = net.ResolveUDPAddr("udp", u.addr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
con, err = net.DialUDP("udp", nil, addr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer con.Close()
|
|
|
|
|
2021-07-08 19:00:40 +00:00
|
|
|
for i := range request.pointOffsets {
|
|
|
|
// write the point without the trailing newline
|
|
|
|
pointRaw := request.PointAt(i)
|
|
|
|
_, err = con.Write(pointRaw[:pointRaw[len(pointRaw)-1]])
|
2015-10-08 16:45:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|