From 975e46f7267f3f8f8526eae3626f3980a5871f77 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 20 Oct 2025 11:49:03 +0200 Subject: [PATCH] libnetwork/datastore: small cleanups - Use an intermediate struct for (un)marshaling dummyObject - Remove dummyObject.SkipSave as it would always be set to "false" (i.e., persisted to disk). - Minor cleanups in handling locks and some unused vars Signed-off-by: Sebastiaan van Stijn --- daemon/libnetwork/datastore/cache.go | 8 +--- daemon/libnetwork/datastore/datastore_test.go | 47 +++++++++++-------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/daemon/libnetwork/datastore/cache.go b/daemon/libnetwork/datastore/cache.go index 7263db6e46..0f468f8cb9 100644 --- a/daemon/libnetwork/datastore/cache.go +++ b/daemon/libnetwork/datastore/cache.go @@ -21,8 +21,6 @@ func newCache(ds store.Store) *cache { } func (c *cache) kmap(kvObject KVObject) (kvMap, error) { - var err error - c.mu.Lock() keyPrefix := Key(kvObject.KeyPrefix()...) kmap, ok := c.kmm[keyPrefix] @@ -52,8 +50,7 @@ func (c *cache) kmap(kvObject KVObject) (kvMap, error) { } dstO := kvObject.New() - err = dstO.SetValue(kvPair.Value) - if err != nil { + if err := dstO.SetValue(kvPair.Value); err != nil { return nil, err } @@ -116,19 +113,18 @@ func (c *cache) del(kvObject KVObject, atomic bool) error { } c.mu.Lock() + defer c.mu.Unlock() // If atomic is true, cache needs to maintain its own index // for atomicity and del needs to be atomic. if atomic { if prev, ok := kmap[Key(kvObject.Key()...)]; ok { if prev.Index() != kvObject.Index() { - c.mu.Unlock() return ErrKeyModified } } } delete(kmap, Key(kvObject.Key()...)) - c.mu.Unlock() return nil } diff --git a/daemon/libnetwork/datastore/datastore_test.go b/daemon/libnetwork/datastore/datastore_test.go index 21e5e7da7e..556c4f6ad6 100644 --- a/daemon/libnetwork/datastore/datastore_test.go +++ b/daemon/libnetwork/datastore/datastore_test.go @@ -77,7 +77,6 @@ type dummyObject struct { ID string DBIndex uint64 DBExists bool - SkipSave bool ReturnValue bool } @@ -119,27 +118,34 @@ func (n *dummyObject) Exists() bool { } func (n *dummyObject) Skip() bool { - return n.SkipSave + return false +} + +type tmpStruct struct { + Name string `json:"name"` + NetworkType string `json:"networkType"` + EnableIPv6 bool `json:"enableIPv6"` + Generic options.Generic `json:"generic"` } func (n *dummyObject) MarshalJSON() ([]byte, error) { - return json.Marshal(map[string]any{ - "name": n.Name, - "networkType": n.NetworkType, - "enableIPv6": n.EnableIPv6, - "generic": n.Generic, + return json.Marshal(tmpStruct{ + Name: n.Name, + NetworkType: n.NetworkType, + EnableIPv6: n.EnableIPv6, + Generic: n.Generic, }) } func (n *dummyObject) UnmarshalJSON(b []byte) error { - var netMap map[string]any + var netMap tmpStruct if err := json.Unmarshal(b, &netMap); err != nil { return err } - n.Name = netMap["name"].(string) - n.NetworkType = netMap["networkType"].(string) - n.EnableIPv6 = netMap["enableIPv6"].(bool) - n.Generic = netMap["generic"].(map[string]any) + n.Name = netMap.Name + n.NetworkType = netMap.NetworkType + n.EnableIPv6 = netMap.EnableIPv6 + n.Generic = netMap.Generic return nil } @@ -199,22 +205,23 @@ func (r *recStruct) Skip() bool { } func dummyKVObject(id string, retValue bool) *dummyObject { - cDict := map[string]string{ - "foo": "bar", - "hello": "world", - } return &dummyObject{ Name: "testNw", NetworkType: "bridge", EnableIPv6: true, - Rec: &recStruct{Name: "gen", Field1: 5, Dict: cDict}, + Rec: &recStruct{Name: "gen", Field1: 5, Dict: map[string]string{ + "foo": "bar", + "hello": "world", + }}, ID: id, DBIndex: 0, ReturnValue: retValue, DBExists: false, - SkipSave: false, - Generic: map[string]any{ - "label1": &recStruct{Name: "value1", Field1: 1, Dict: cDict}, + Generic: options.Generic{ + "label1": &recStruct{Name: "value1", Field1: 1, Dict: map[string]string{ + "foo": "bar", + "hello": "world", + }}, "label2": "subnet=10.1.1.0/16", }, }