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 ( ...@@ -4,13 +4,14 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"fmt" "fmt"
"github.com/coreos/go-iptables/iptables"
"io/ioutil" "io/ioutil"
"net" "net"
"os" "os"
"strings" "strings"
"text/template" "text/template"
"github.com/coreos/go-iptables/iptables"
"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/disk" "github.com/containernetworking/plugins/plugins/ipam/host-local/backend/disk"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
...@@ -20,7 +21,7 @@ type dnsNameLock struct { ...@@ -20,7 +21,7 @@ type dnsNameLock struct {
lock *disk.FileLock lock *disk.FileLock
} }
// release unlocks and closes the disk lock // release unlocks and closes the disk lock.
func (m *dnsNameLock) release() error { func (m *dnsNameLock) release() error {
if err := m.lock.Unlock(); err != nil { if err := m.lock.Unlock(); err != nil {
return err return err
...@@ -28,16 +29,18 @@ func (m *dnsNameLock) release() error { ...@@ -28,16 +29,18 @@ func (m *dnsNameLock) release() error {
return m.lock.Close() return m.lock.Close()
} }
// getLock returns a dnsNameLock. the lock should be that of the configuration // acquire locks the disk lock.
// directory for the domain. 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) { func getLock(path string) (*dnsNameLock, error) {
l, err := disk.NewFileLock(path) l, err := disk.NewFileLock(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := l.Lock(); err != nil {
return nil, err
}
return &dnsNameLock{l}, nil return &dnsNameLock{l}, nil
} }
......
...@@ -75,6 +75,9 @@ func cmdAdd(args *skel.CmdArgs) error { ...@@ -75,6 +75,9 @@ func cmdAdd(args *skel.CmdArgs) error {
if err != nil { if err != nil {
return err return err
} }
if err := lock.acquire(); err != nil {
return err
}
defer func() { defer func() {
if err := lock.release(); err != nil { if err := lock.release(); err != nil {
logrus.Errorf("unable to release lock for '%s': %q", dnsNameConf.AddOnHostsFile, err) logrus.Errorf("unable to release lock for '%s': %q", dnsNameConf.AddOnHostsFile, err)
...@@ -121,6 +124,9 @@ func cmdDel(args *skel.CmdArgs) error { ...@@ -121,6 +124,9 @@ func cmdDel(args *skel.CmdArgs) error {
if err != nil { if err != nil {
return err return err
} }
if err := lock.acquire(); err != nil {
return err
}
defer func() { defer func() {
// if the lock isn't given up by another process // if the lock isn't given up by another process
if err := lock.release(); err != nil { if err := lock.release(); err != nil {
...@@ -171,6 +177,9 @@ func cmdCheck(args *skel.CmdArgs) error { ...@@ -171,6 +177,9 @@ func cmdCheck(args *skel.CmdArgs) error {
if err != nil { if err != nil {
return err return err
} }
if err := lock.acquire(); err != nil {
return err
}
defer func() { defer func() {
// if the lock isn't given up by another process // if the lock isn't given up by another process
if err := lock.release(); err != nil { 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