Skip to content

Commit

Permalink
Merge pull request #50 from aelsabbahy/feature/lazy_dbus
Browse files Browse the repository at this point in the history
Load dbus lazily
  • Loading branch information
aelsabbahy committed Mar 12, 2016
2 parents 04d3850 + a8cc8c2 commit fc49302
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion system/service_dbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ServiceDbus struct {
func NewServiceDbus(service string, system *System, config util.Config) Service {
return &ServiceDbus{
service: service,
dbus: system.Dbus,
dbus: system.Dbus(),
}
}

Expand Down
22 changes: 15 additions & 7 deletions system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ type System struct {
NewDNS func(string, *System, util2.Config) DNS
NewProcess func(string, *System, util2.Config) Process
NewGossfile func(string, *System, util2.Config) Gossfile
Dbus *dbus.Conn
dbus *dbus.Conn
ports map[string][]GOnetstat.Process
dbusOnce sync.Once
portsOnce sync.Once
procOnce sync.Once
procMap map[string][]ps.Process
Expand All @@ -47,6 +48,19 @@ func (s *System) Ports() map[string][]GOnetstat.Process {
return s.ports
}

func (s *System) Dbus() *dbus.Conn {
s.dbusOnce.Do(func() {
dbus, err := dbus.New()
if err != nil {
fmt.Println(err)
// FIXME: Do we really want to exit here?
os.Exit(1)
}
s.dbus = dbus
})
return s.dbus
}

func (s *System) ProcMap() map[string][]ps.Process {
s.procOnce.Do(func() {
s.procMap = GetProcs()
Expand Down Expand Up @@ -102,12 +116,6 @@ func (s *System) detectService() {
switch {
case util.IsRunningSystemd():
s.NewService = NewServiceDbus
dbus, err := dbus.New()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
s.Dbus = dbus
case isUbuntu():
s.NewService = NewServiceUpstart
case isAlpine():
Expand Down

0 comments on commit fc49302

Please sign in to comment.