1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-24 07:04:51 +02:00

wrap DefaultClient.Send with a single lock/unlock and rename mux to mu for consistency

This commit is contained in:
Gani Georgiev
2025-09-13 23:42:25 +03:00
parent a095549304
commit 68ab174f69

View File

@@ -84,7 +84,7 @@ type DefaultClient struct {
subscriptions map[string]SubscriptionOptions subscriptions map[string]SubscriptionOptions
channel chan Message channel chan Message
id string id string
mux sync.RWMutex mu sync.RWMutex
isDiscarded bool isDiscarded bool
} }
@@ -100,16 +100,16 @@ func NewDefaultClient() *DefaultClient {
// Id implements the [Client.Id] interface method. // Id implements the [Client.Id] interface method.
func (c *DefaultClient) Id() string { func (c *DefaultClient) Id() string {
c.mux.RLock() c.mu.RLock()
defer c.mux.RUnlock() defer c.mu.RUnlock()
return c.id return c.id
} }
// Channel implements the [Client.Channel] interface method. // Channel implements the [Client.Channel] interface method.
func (c *DefaultClient) Channel() chan Message { func (c *DefaultClient) Channel() chan Message {
c.mux.RLock() c.mu.RLock()
defer c.mux.RUnlock() defer c.mu.RUnlock()
return c.channel return c.channel
} }
@@ -119,8 +119,8 @@ func (c *DefaultClient) Channel() chan Message {
// It returns a shallow copy of the client subscriptions matching the prefixes. // It returns a shallow copy of the client subscriptions matching the prefixes.
// If no prefix is specified, returns all subscriptions. // If no prefix is specified, returns all subscriptions.
func (c *DefaultClient) Subscriptions(prefixes ...string) map[string]SubscriptionOptions { func (c *DefaultClient) Subscriptions(prefixes ...string) map[string]SubscriptionOptions {
c.mux.RLock() c.mu.RLock()
defer c.mux.RUnlock() defer c.mu.RUnlock()
// no prefix -> return copy of all subscriptions // no prefix -> return copy of all subscriptions
if len(prefixes) == 0 { if len(prefixes) == 0 {
@@ -152,8 +152,8 @@ func (c *DefaultClient) Subscriptions(prefixes ...string) map[string]Subscriptio
// //
// Empty subscriptions (aka. "") are ignored. // Empty subscriptions (aka. "") are ignored.
func (c *DefaultClient) Subscribe(subs ...string) { func (c *DefaultClient) Subscribe(subs ...string) {
c.mux.Lock() c.mu.Lock()
defer c.mux.Unlock() defer c.mu.Unlock()
for _, s := range subs { for _, s := range subs {
if s == "" { if s == "" {
@@ -199,8 +199,8 @@ func (c *DefaultClient) Subscribe(subs ...string) {
// //
// If subs is not set, this method removes all registered client's subscriptions. // If subs is not set, this method removes all registered client's subscriptions.
func (c *DefaultClient) Unsubscribe(subs ...string) { func (c *DefaultClient) Unsubscribe(subs ...string) {
c.mux.Lock() c.mu.Lock()
defer c.mux.Unlock() defer c.mu.Unlock()
if len(subs) > 0 { if len(subs) > 0 {
for _, s := range subs { for _, s := range subs {
@@ -216,8 +216,8 @@ func (c *DefaultClient) Unsubscribe(subs ...string) {
// HasSubscription implements the [Client.HasSubscription] interface method. // HasSubscription implements the [Client.HasSubscription] interface method.
func (c *DefaultClient) HasSubscription(sub string) bool { func (c *DefaultClient) HasSubscription(sub string) bool {
c.mux.RLock() c.mu.RLock()
defer c.mux.RUnlock() defer c.mu.RUnlock()
_, ok := c.subscriptions[sub] _, ok := c.subscriptions[sub]
@@ -226,32 +226,32 @@ func (c *DefaultClient) HasSubscription(sub string) bool {
// Get implements the [Client.Get] interface method. // Get implements the [Client.Get] interface method.
func (c *DefaultClient) Get(key string) any { func (c *DefaultClient) Get(key string) any {
c.mux.RLock() c.mu.RLock()
defer c.mux.RUnlock() defer c.mu.RUnlock()
return c.store[key] return c.store[key]
} }
// Set implements the [Client.Set] interface method. // Set implements the [Client.Set] interface method.
func (c *DefaultClient) Set(key string, value any) { func (c *DefaultClient) Set(key string, value any) {
c.mux.Lock() c.mu.Lock()
defer c.mux.Unlock() defer c.mu.Unlock()
c.store[key] = value c.store[key] = value
} }
// Unset implements the [Client.Unset] interface method. // Unset implements the [Client.Unset] interface method.
func (c *DefaultClient) Unset(key string) { func (c *DefaultClient) Unset(key string) {
c.mux.Lock() c.mu.Lock()
defer c.mux.Unlock() defer c.mu.Unlock()
delete(c.store, key) delete(c.store, key)
} }
// Discard implements the [Client.Discard] interface method. // Discard implements the [Client.Discard] interface method.
func (c *DefaultClient) Discard() { func (c *DefaultClient) Discard() {
c.mux.Lock() c.mu.Lock()
defer c.mux.Unlock() defer c.mu.Unlock()
if c.isDiscarded { if c.isDiscarded {
return return
@@ -264,17 +264,20 @@ func (c *DefaultClient) Discard() {
// IsDiscarded implements the [Client.IsDiscarded] interface method. // IsDiscarded implements the [Client.IsDiscarded] interface method.
func (c *DefaultClient) IsDiscarded() bool { func (c *DefaultClient) IsDiscarded() bool {
c.mux.RLock() c.mu.RLock()
defer c.mux.RUnlock() defer c.mu.RUnlock()
return c.isDiscarded return c.isDiscarded
} }
// Send sends the specified message to the client's channel (if not discarded). // Send sends the specified message to the client's channel (if not discarded).
func (c *DefaultClient) Send(m Message) { func (c *DefaultClient) Send(m Message) {
if c.IsDiscarded() { c.mu.RLock()
defer c.mu.RUnlock()
if c.isDiscarded {
return return
} }
c.Channel() <- m c.channel <- m
} }