-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: remove full status Update Requests Signed-off-by: Martin Buchleitner <mbuchleitner@infralovers.com> * fix: remove full status Update Requests Signed-off-by: Martin Buchleitner <mbuchleitner@infralovers.com> * merge update Signed-off-by: Martin Buchleitner <mbuchleitner@infralovers.com> * fix: update that loop ctrl Signed-off-by: Martin Buchleitner <mbuchleitner@infralovers.com> * fix: atomic increment Signed-off-by: Martin Buchleitner <mbuchleitner@infralovers.com> * refactor: split helper code to dedicated files Signed-off-by: Martin Buchleitner <mbuchleitner@infralovers.com> --------- Signed-off-by: Martin Buchleitner <mbuchleitner@infralovers.com>
- Loading branch information
Showing
6 changed files
with
196 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package wattpilot | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
var randomSource = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
||
func Keys[K comparable, V any](m map[K]V) []K { | ||
keys := make([]K, 0, len(m)) | ||
for k := range m { | ||
keys = append(keys, k) | ||
} | ||
return keys | ||
} | ||
|
||
func hasKey(data map[string]interface{}, key string) bool { | ||
_, isKnown := data[key] | ||
return isKnown | ||
} | ||
|
||
func sha256sum(data string) string { | ||
bs := sha256.Sum256([]byte(data)) | ||
return fmt.Sprintf("%x", bs) | ||
} | ||
func randomHexString(n int) string { | ||
b := make([]byte, (n+2)/2) // can be simplified to n/2 if n is always even | ||
|
||
if _, err := randomSource.Read(b); err != nil { | ||
panic(err) | ||
} | ||
|
||
return hex.EncodeToString(b)[1 : n+1] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package wattpilot | ||
|
||
import "sync" | ||
|
||
type Pubsub struct { | ||
mu sync.RWMutex | ||
subs map[string][]chan interface{} | ||
closed bool | ||
} | ||
|
||
func NewPubsub() *Pubsub { | ||
ps := &Pubsub{} | ||
ps.subs = make(map[string][]chan interface{}) | ||
return ps | ||
} | ||
|
||
func (ps *Pubsub) IsEmpty() bool { | ||
ps.mu.Lock() | ||
defer ps.mu.Unlock() | ||
|
||
return len(ps.subs) == 0 | ||
} | ||
|
||
func (ps *Pubsub) Subscribe(topic string) <-chan interface{} { | ||
ps.mu.Lock() | ||
defer ps.mu.Unlock() | ||
|
||
ch := make(chan interface{}, 1) | ||
ps.subs[topic] = append(ps.subs[topic], ch) | ||
return ch | ||
} | ||
|
||
func (ps *Pubsub) Publish(topic string, msg interface{}) { | ||
ps.mu.RLock() | ||
defer ps.mu.RUnlock() | ||
|
||
if ps.closed { | ||
return | ||
} | ||
for _, ch := range ps.subs[topic] { | ||
go func(ch chan interface{}) { | ||
ch <- msg | ||
}(ch) | ||
} | ||
} | ||
func (ps *Pubsub) Close() { | ||
ps.mu.Lock() | ||
defer ps.mu.Unlock() | ||
|
||
if !ps.closed { | ||
ps.closed = true | ||
for _, subs := range ps.subs { | ||
for _, ch := range subs { | ||
close(ch) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.