Add: generational monitor
This commit is contained in:
parent
4e4da6b88a
commit
afa496e5e9
41
monitor.go
41
monitor.go
|
@ -123,8 +123,8 @@ type MonitorEvent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type MonitorEvents struct {
|
type MonitorEvents struct {
|
||||||
GenerateBy *MonitorEvent
|
GeneratedBy *MonitorEvent
|
||||||
Changes []*MonitorEvent
|
Changes []*MonitorEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -198,7 +198,7 @@ func NewMonitor(opts ...MonitorOption) *Monitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (monitor *Monitor) monitor() {
|
func (monitor *Monitor) monitor() {
|
||||||
changesEvents := make([]*MonitorEvent, 0, 2)
|
changesEvents := make([]*MonitorEvent, 0)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
msgs, err := monitor.conn.Receive()
|
msgs, err := monitor.conn.Receive()
|
||||||
|
@ -217,10 +217,10 @@ func (monitor *Monitor) monitor() {
|
||||||
changesEvents = append(changesEvents, event)
|
changesEvents = append(changesEvents, event)
|
||||||
|
|
||||||
monitor.eventCh <- &MonitorEvents{
|
monitor.eventCh <- &MonitorEvents{
|
||||||
GenerateBy: event,
|
GeneratedBy: event,
|
||||||
Changes: changesEvents,
|
Changes: changesEvents,
|
||||||
}
|
}
|
||||||
changesEvents = make([]*MonitorEvent, 0, 2)
|
changesEvents = make([]*MonitorEvent, 0)
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -298,11 +298,11 @@ func (monitor *Monitor) monitor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
monitor.eventCh <- &MonitorEvents{
|
monitor.eventCh <- &MonitorEvents{
|
||||||
GenerateBy: event,
|
GeneratedBy: event,
|
||||||
Changes: changesEvents,
|
Changes: changesEvents,
|
||||||
}
|
}
|
||||||
|
|
||||||
changesEvents = make([]*MonitorEvent, 0, 2)
|
changesEvents = make([]*MonitorEvent, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -331,7 +331,28 @@ func (monitor *Monitor) Close() error {
|
||||||
// calling Close on Monitor or encountering a netlink conn error while Receive.
|
// calling Close on Monitor or encountering a netlink conn error while Receive.
|
||||||
// Caller may receive a MonitorEventTypeOOB event which contains an error we didn't
|
// Caller may receive a MonitorEventTypeOOB event which contains an error we didn't
|
||||||
// handle, for now.
|
// handle, for now.
|
||||||
func (cc *Conn) AddMonitor(monitor *Monitor) (chan *MonitorEvents, error) {
|
func (cc *Conn) AddMonitor(monitor *Monitor) (chan *MonitorEvent, error) {
|
||||||
|
generationalEventCh, err := cc.AddGenerationalMonitor(monitor)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
eventCh := make(chan *MonitorEvent)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(eventCh)
|
||||||
|
for monitorEvents := range generationalEventCh {
|
||||||
|
for _, event := range monitorEvents.Changes {
|
||||||
|
eventCh <- event
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return eventCh, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cc *Conn) AddGenerationalMonitor(monitor *Monitor) (chan *MonitorEvents, error) {
|
||||||
conn, closer, err := cc.netlinkConn()
|
conn, closer, err := cc.netlinkConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -21,7 +21,7 @@ func ExampleNewMonitor() {
|
||||||
|
|
||||||
mon := nftables.NewMonitor()
|
mon := nftables.NewMonitor()
|
||||||
defer mon.Close()
|
defer mon.Close()
|
||||||
events, err := conn.AddMonitor(mon)
|
events, err := conn.AddGenerationalMonitor(mon)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ func TestMonitor(t *testing.T) {
|
||||||
|
|
||||||
// default to monitor all
|
// default to monitor all
|
||||||
monitor := nftables.NewMonitor()
|
monitor := nftables.NewMonitor()
|
||||||
events, err := c.AddMonitor(monitor)
|
events, err := c.AddGenerationalMonitor(monitor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
12
set.go
12
set.go
|
@ -268,12 +268,6 @@ type Set struct {
|
||||||
KeyByteOrder binaryutil.ByteOrder
|
KeyByteOrder binaryutil.ByteOrder
|
||||||
}
|
}
|
||||||
|
|
||||||
type SetElementsInfo struct {
|
|
||||||
TableName string
|
|
||||||
SetName string
|
|
||||||
Elements []SetElement
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetElement represents a data point within a set.
|
// SetElement represents a data point within a set.
|
||||||
type SetElement struct {
|
type SetElement struct {
|
||||||
Key []byte
|
Key []byte
|
||||||
|
@ -803,15 +797,10 @@ func elementsFromMsg(fam byte, msg netlink.Message) ([]SetElement, error) {
|
||||||
}
|
}
|
||||||
ad.ByteOrder = binary.BigEndian
|
ad.ByteOrder = binary.BigEndian
|
||||||
|
|
||||||
var info = &SetElementsInfo{}
|
|
||||||
var elements []SetElement
|
var elements []SetElement
|
||||||
for ad.Next() {
|
for ad.Next() {
|
||||||
b := ad.Bytes()
|
b := ad.Bytes()
|
||||||
switch ad.Type() {
|
switch ad.Type() {
|
||||||
case unix.NFTA_SET_ELEM_LIST_TABLE:
|
|
||||||
info.TableName = ad.String()
|
|
||||||
case unix.NFTA_SET_ELEM_LIST_SET:
|
|
||||||
info.SetName = ad.String()
|
|
||||||
case unix.NFTA_SET_ELEM_LIST_ELEMENTS:
|
case unix.NFTA_SET_ELEM_LIST_ELEMENTS:
|
||||||
ad, err := netlink.NewAttributeDecoder(b)
|
ad, err := netlink.NewAttributeDecoder(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -829,6 +818,7 @@ func elementsFromMsg(fam byte, msg netlink.Message) ([]SetElement, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return elements, nil
|
return elements, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue