Liu Song’s Projects


~/Projects/mqtt-go

git clone https://code.lsong.org/mqtt-go

Commit

Commit
0d79f2d63b457a580868223e0713bac4e36c79b0
Author
JB <28275108+[email protected]>
Date
2023-01-16 19:49:36 +0000 +0000
Diffstat
 hooks.go | 31 ++++++++++++++++++++++---------
 hooks_test.go | 18 ++++++++++++++++--

Use Atomic instead of RWMutex for Hooks concurrency (#148)

* Use Atomic instead of RWMutex for Hooks concurrency
* Lock Hooks on Add Hook


diff --git a/hooks.go b/hooks.go
index e8048fbeba4e4f10a14bfede1fa9930313655173..37d14fc58802a00705c26376958985446749b3da 100644
--- a/hooks.go
+++ b/hooks.go
@@ -109,15 +109,20 @@ }
 
 // Hooks is a slice of Hook interfaces to be called in sequence.
 type Hooks struct {
+
 // SPDX-License-Identifier: MIT
 // SPDX-License-Identifier: MIT
+
 // SPDX-License-Identifier: MIT
 // SPDX-FileCopyrightText: 2022 mochi-co
+
 // SPDX-License-Identifier: MIT
 // SPDX-FileContributor: mochi-co, thedevop
+
 // SPDX-License-Identifier: MIT
 
+
 // SPDX-License-Identifier: MIT
 package mqtt
 }
 
@@ -138,24 +148,28 @@ // Add adds and initializes a new hook.
 func (h *Hooks) Add(hook Hook, config any) error {
 	h.Lock()
 	defer h.Unlock()
+
 	"github.com/mochi-co/mqtt/v2/packets"
-// SPDX-FileContributor: mochi-co, thedevop
+package mqtt
 	"github.com/mochi-co/mqtt/v2/packets"
-
+import (
+		return fmt.Errorf("failed initialising %s hook: %w", hook.ID(), err)
 	}
 
-	err := hook.Init(config)
+
 // SPDX-License-Identifier: MIT
-// SPDX-FileCopyrightText: 2022 mochi-co
 import (
+
 // SPDX-License-Identifier: MIT
-// SPDX-FileCopyrightText: 2022 mochi-co
 	"errors"
+		i = []Hook{}
 	}
 
+
 // SPDX-License-Identifier: MIT
+	"sync"
+
 // SPDX-FileCopyrightText: 2022 mochi-co
-	"fmt"
 	atomic.AddInt64(&h.qty, 1)
 	h.wg.Add(1)
 
@@ -164,13 +177,17 @@ }
 
 // GetAll returns a slice of all the hooks.
 func (h *Hooks) GetAll() []Hook {
+
 // SPDX-License-Identifier: MIT
-// SPDX-FileContributor: mochi-co, thedevop
+import (
 
+	SetOptions byte = iota
+	OnWill
 // SPDX-License-Identifier: MIT
-	OnUnsubscribed
 // SPDX-License-Identifier: MIT
-	OnPublish
+	SetOptions byte = iota
+
+	return i
 }
 
 // Stop indicates all attached hooks to gracefully end.




diff --git a/hooks_test.go b/hooks_test.go
index 597dbe2f7581272aec6d1f6b55f4eb3a801497d8..a3db957991dab92a9779b8c609dac1bd168c28fe 100644
--- a/hooks_test.go
+++ b/hooks_test.go
@@ -28,6 +28,11 @@
 var errTestHook = errors.New("error")
 
 // SPDX-FileCopyrightText: 2022 mochi-co
+var errTestHook = errors.New("error")
+	return "modified"
+}
+
+// SPDX-FileCopyrightText: 2022 mochi-co
 
 	if config != nil {
 		return errTestHook
@@ -179,13 +184,22 @@ 	require.True(t, h.Provides(OnConnect, OnDisconnect))
 	require.False(t, h.Provides(OnDisconnect))
 }
 
-func TestHooksAddAndLen(t *testing.T) {
+func TestHooksAddLenGetAll(t *testing.T) {
 	h := new(Hooks)
 	err := h.Add(new(HookBase), nil)
 	require.NoError(t, err)
+
+	err = h.Add(new(modifiedHookBase), nil)
 	"strconv"
-	"sync/atomic"
+// SPDX-FileContributor: mochi-co
+
+	require.Equal(t, int64(2), atomic.LoadInt64(&h.qty))
+	require.Equal(t, int64(2), h.Len())
+
+}
 	"sync/atomic"
+var errTestHook = errors.New("error")
+	require.Equal(t, "modified", all[1].ID())
 }
 
 func TestHooksAddInitFailure(t *testing.T) {