-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
libct/cg/sd: reconnect and retry on dbus connection error #2923
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cdbed6f
libct/cg/sd: add dbus manager
wzshiming bacfc2c
libct/cg/sd: add isDbusError
kolyshkin 15fee98
libct/cg/sd: add renew dbus connection
wzshiming 6122bc8
Privatize NewUserSystemDbus
wzshiming 47ef9a1
libct/cg/sd: retry on dbus disconnect
kolyshkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// +build linux | ||
|
||
package systemd | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
systemdDbus "github.com/coreos/go-systemd/v22/dbus" | ||
dbus "github.com/godbus/dbus/v5" | ||
) | ||
|
||
type dbusConnManager struct { | ||
conn *systemdDbus.Conn | ||
rootless bool | ||
sync.RWMutex | ||
} | ||
|
||
// newDbusConnManager initializes systemd dbus connection manager. | ||
func newDbusConnManager(rootless bool) *dbusConnManager { | ||
return &dbusConnManager{ | ||
rootless: rootless, | ||
} | ||
} | ||
|
||
// getConnection lazily initializes and returns systemd dbus connection. | ||
func (d *dbusConnManager) getConnection() (*systemdDbus.Conn, error) { | ||
// In the case where d.conn != nil | ||
// Use the read lock the first time to ensure | ||
// that Conn can be acquired at the same time. | ||
d.RLock() | ||
if conn := d.conn; conn != nil { | ||
d.RUnlock() | ||
return conn, nil | ||
} | ||
d.RUnlock() | ||
|
||
// In the case where d.conn == nil | ||
// Use write lock to ensure that only one | ||
// will be created | ||
d.Lock() | ||
defer d.Unlock() | ||
if conn := d.conn; conn != nil { | ||
return conn, nil | ||
} | ||
|
||
conn, err := d.newConnection() | ||
if err != nil { | ||
return nil, err | ||
} | ||
d.conn = conn | ||
return conn, nil | ||
} | ||
|
||
func (d *dbusConnManager) newConnection() (*systemdDbus.Conn, error) { | ||
if d.rootless { | ||
return newUserSystemdDbus() | ||
} | ||
return systemdDbus.NewWithContext(context.TODO()) | ||
} | ||
|
||
// resetConnection resets the connection to its initial state | ||
// (so it can be reconnected if necessary). | ||
func (d *dbusConnManager) resetConnection(conn *systemdDbus.Conn) { | ||
d.Lock() | ||
defer d.Unlock() | ||
if d.conn != nil && d.conn == conn { | ||
d.conn.Close() | ||
d.conn = nil | ||
} | ||
} | ||
|
||
var errDbusConnClosed = dbus.ErrClosed.Error() | ||
|
||
// retryOnDisconnect calls op, and if the error it returns is about closed dbus | ||
// connection, the connection is re-established and the op is retried. This helps | ||
// with the situation when dbus is restarted and we have a stale connection. | ||
func (d *dbusConnManager) retryOnDisconnect(op func(*systemdDbus.Conn) error) error { | ||
for { | ||
conn, err := d.getConnection() | ||
if err != nil { | ||
return err | ||
} | ||
err = op(conn) | ||
if !isDbusError(err, errDbusConnClosed) { | ||
return err | ||
} | ||
d.resetConnection(conn) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be
But it doesn't really matter.