Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions dbus/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package dbus

import (
"errors"
"log"
"time"

"github.com/godbus/dbus"
Expand Down Expand Up @@ -176,6 +177,16 @@ type SubStateUpdate struct {
// is full, it attempts to write an error to errCh; if errCh is full, the error
// passes silently.
func (c *Conn) SetSubStateSubscriber(updateCh chan<- *SubStateUpdate, errCh chan<- error) {
if c == nil {
msg := "nil receiver"
select {
case errCh <- errors.New(msg):
default:
log.Printf("full error channel while reporting: %s\n", msg)
}
return
}

c.subStateSubscriber.Lock()
defer c.subStateSubscriber.Unlock()
c.subStateSubscriber.updateCh = updateCh
Expand All @@ -190,7 +201,9 @@ func (c *Conn) sendSubStateUpdate(unitPath dbus.ObjectPath) {
return
}

if c.shouldIgnore(unitPath) {
isIgnored := c.shouldIgnore(unitPath)
defer c.cleanIgnore()
if isIgnored {
return
}

Expand All @@ -199,23 +212,45 @@ func (c *Conn) sendSubStateUpdate(unitPath dbus.ObjectPath) {
select {
case c.subStateSubscriber.errCh <- err:
default:
log.Printf("full error channel while reporting: %s\n", err)
}
return
}
defer c.updateIgnore(unitPath, info)

name := info["Id"].(string)
substate := info["SubState"].(string)
name, ok := info["Id"].(string)
if !ok {
msg := "failed to cast info.Id"
select {
case c.subStateSubscriber.errCh <- errors.New(msg):
default:
log.Printf("full error channel while reporting: %s\n", err)
}
return
}
substate, ok := info["SubState"].(string)
if !ok {
msg := "failed to cast info.SubState"
select {
case c.subStateSubscriber.errCh <- errors.New(msg):
default:
log.Printf("full error channel while reporting: %s\n", msg)
}
return
}

update := &SubStateUpdate{name, substate}
select {
case c.subStateSubscriber.updateCh <- update:
default:
msg := "update channel is full"
select {
case c.subStateSubscriber.errCh <- errors.New("update channel full!"):
case c.subStateSubscriber.errCh <- errors.New(msg):
default:
log.Printf("full error channel while reporting: %s\n", msg)
}
return
}

c.updateIgnore(unitPath, info)
}

// The ignore functions work around a wart in the systemd dbus interface.
Expand All @@ -238,10 +273,13 @@ func (c *Conn) shouldIgnore(path dbus.ObjectPath) bool {
}

func (c *Conn) updateIgnore(path dbus.ObjectPath, info map[string]interface{}) {
c.cleanIgnore()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why move this out of here?

(seems fine to me either way, but if anyone other than sendSubStateUpdate starts using the ignore functions, then they'll have to remember to call cleanIgnore themselves)

Copy link
Contributor Author

@lucab lucab Jan 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because updateIgnore is only called after we get a proper and non-ignored info, while cleanIgnore is generic bookkeeping that doesn't need any of it. I was just reading the code, and I felt it was out of place, so I turned it into a non-conditional defer.

You are right about the multiple callers. This code may need some more intrusive re-architecting once we get there, the whole ignore thing is quite hackish.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense—sounds good.

loadState, ok := info["LoadState"].(string)
if !ok {
return
}

// unit is unloaded - it will trigger bad systemd dbus behavior
if info["LoadState"].(string) == "not-found" {
if loadState == "not-found" {
c.subStateSubscriber.ignore[path] = time.Now().UnixNano() + ignoreInterval
}
}
Expand Down Expand Up @@ -294,9 +332,11 @@ func (c *Conn) sendPropertiesUpdate(unitPath dbus.ObjectPath, changedProps map[s
select {
case c.propertiesSubscriber.updateCh <- update:
default:
msg := "update channel is full"
select {
case c.propertiesSubscriber.errCh <- errors.New("update channel is full"):
case c.propertiesSubscriber.errCh <- errors.New(msg):
default:
log.Printf("full error channel while reporting: %s\n", msg)
}
return
}
Expand Down