Skip to content
Snippets Groups Projects
Commit 28a382d8 authored by Valentin Rothberg's avatar Valentin Rothberg
Browse files

split acquiring the lock out of getLock


Split the logic to acquire the disk locks into a separate method
`dnsNameLock.acquire() error` to make the API more flexible and
less prone to errors.

Signed-off-by: default avatarValentin Rothberg <rothberg@redhat.com>
parent daba1b3d
No related branches found
No related tags found
No related merge requests found
......@@ -4,13 +4,14 @@ import (
"bufio"
"bytes"
"fmt"
"github.com/coreos/go-iptables/iptables"
"io/ioutil"
"net"
"os"
"strings"
"text/template"
"github.com/coreos/go-iptables/iptables"
"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/disk"
"github.com/sirupsen/logrus"
)
......@@ -20,7 +21,7 @@ type dnsNameLock struct {
lock *disk.FileLock
}
// release unlocks and closes the disk lock
// release unlocks and closes the disk lock.
func (m *dnsNameLock) release() error {
if err := m.lock.Unlock(); err != nil {
return err
......@@ -28,16 +29,18 @@ func (m *dnsNameLock) release() error {
return m.lock.Close()
}
// getLock returns a dnsNameLock. the lock should be that of the configuration
// directory for the domain.
// acquire locks the disk lock.
func (m *dnsNameLock) acquire() error {
return m.lock.Lock()
}
// getLock returns a dnsNameLock synchronizing the configuration directory for
// the domain.
func getLock(path string) (*dnsNameLock, error) {
l, err := disk.NewFileLock(path)
if err != nil {
return nil, err
}
if err := l.Lock(); err != nil {
return nil, err
}
return &dnsNameLock{l}, nil
}
......
......@@ -75,6 +75,9 @@ func cmdAdd(args *skel.CmdArgs) error {
if err != nil {
return err
}
if err := lock.acquire(); err != nil {
return err
}
defer func() {
if err := lock.release(); err != nil {
logrus.Errorf("unable to release lock for '%s': %q", dnsNameConf.AddOnHostsFile, err)
......@@ -121,6 +124,9 @@ func cmdDel(args *skel.CmdArgs) error {
if err != nil {
return err
}
if err := lock.acquire(); err != nil {
return err
}
defer func() {
// if the lock isn't given up by another process
if err := lock.release(); err != nil {
......@@ -171,6 +177,9 @@ func cmdCheck(args *skel.CmdArgs) error {
if err != nil {
return err
}
if err := lock.acquire(); err != nil {
return err
}
defer func() {
// if the lock isn't given up by another process
if err := lock.release(); err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment