Skip to content

Commit

Permalink
Merge pull request #167 from KevFan/rwlock-inmemory
Browse files Browse the repository at this point in the history
feat: RWMutex on InMemoryClient
  • Loading branch information
mikenairn authored Jun 26, 2024
2 parents dbf5cc4 + ec15d72 commit cdaae68
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion internal/external-dns/provider/inmemory/inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"strings"
"sync"

"github.com/go-logr/logr"

Expand Down Expand Up @@ -265,14 +266,18 @@ func (f *filter) EndpointZoneID(endpoint *endpoint.Endpoint, zones map[string]st
type Zone map[endpoint.EndpointKey]*endpoint.Endpoint

type InMemoryClient struct {
sync.RWMutex
zones map[string]Zone
}

func NewInMemoryClient() *InMemoryClient {
return &InMemoryClient{map[string]Zone{}}
return &InMemoryClient{zones: map[string]Zone{}}
}

func (c *InMemoryClient) Records(zone string) ([]*endpoint.Endpoint, error) {
c.RLock()
defer c.RUnlock()

if _, ok := c.zones[zone]; !ok {
return nil, ErrZoneNotFound
}
Expand All @@ -285,6 +290,9 @@ func (c *InMemoryClient) Records(zone string) ([]*endpoint.Endpoint, error) {
}

func (c *InMemoryClient) Zones() map[string]string {
c.RLock()
defer c.RUnlock()

zones := map[string]string{}
for zone := range c.zones {
zones[zone] = zone
Expand All @@ -293,6 +301,9 @@ func (c *InMemoryClient) Zones() map[string]string {
}

func (c *InMemoryClient) CreateZone(zone string) error {
c.Lock()
defer c.Unlock()

if _, ok := c.zones[zone]; ok {
return ErrZoneAlreadyExists
}
Expand All @@ -302,6 +313,9 @@ func (c *InMemoryClient) CreateZone(zone string) error {
}

func (c *InMemoryClient) DeleteZone(zone string) error {
c.Lock()
defer c.Unlock()

if _, ok := c.zones[zone]; ok {
delete(c.zones, zone)
return nil
Expand All @@ -310,13 +324,19 @@ func (c *InMemoryClient) DeleteZone(zone string) error {
}

func (c *InMemoryClient) GetZone(zone string) (Zone, error) {
c.RLock()
defer c.RUnlock()

if _, ok := c.zones[zone]; ok {
return c.zones[zone], nil
}
return nil, ErrZoneNotFound
}

func (c *InMemoryClient) ApplyChanges(ctx context.Context, zoneID string, changes *plan.Changes) error {
c.Lock()
defer c.Unlock()

if err := c.validateChangeBatch(zoneID, changes); err != nil {
return err
}
Expand Down

0 comments on commit cdaae68

Please sign in to comment.