Skip to content

Commit 66d393c

Browse files
committed
Fixed comments and added a mutex for the feature map.
1 parent 438d614 commit 66d393c

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

client.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net/url"
2121
"runtime"
2222
"strings"
23+
"sync"
2324
"time"
2425
)
2526

@@ -46,17 +47,18 @@ var (
4647
type Feature int
4748

4849
const (
49-
// EnableConversationsAPIWhatsAppSandbox Enables the WhatsApp sandbox for conversations API
50+
// FeatureConversationsAPIWhatsAppSandbox Enables the WhatsApp sandbox for conversations API.
5051
FeatureConversationsAPIWhatsAppSandbox Feature = iota
5152
)
5253

5354
// Client is used to access API with a given key.
5455
// Uses standard lib HTTP client internally, so should be reused instead of created as needed and it is safe for concurrent use.
5556
type Client struct {
56-
AccessKey string // The API access key
57-
HTTPClient *http.Client // The HTTP client to send requests on
58-
DebugLog *log.Logger // Optional logger for debugging purposes
59-
features map[Feature]bool // Enabled features
57+
AccessKey string // The API access key.
58+
HTTPClient *http.Client // The HTTP client to send requests on.
59+
DebugLog *log.Logger // Optional logger for debugging purposes.
60+
features map[Feature]bool // Enabled features.
61+
featuresMutex *sync.RWMutex //Mutex for accessing feature map.
6062
}
6163

6264
type contentType string
@@ -79,7 +81,8 @@ func New(accessKey string) *Client {
7981
HTTPClient: &http.Client{
8082
Timeout: httpClientTimeout,
8183
},
82-
features: make(map[Feature]bool),
84+
features: make(map[Feature]bool),
85+
featuresMutex: &sync.RWMutex{},
8386
}
8487
}
8588

@@ -89,18 +92,24 @@ func SetVoiceErrorReader(r errorReader) {
8992
voiceErrorReader = r
9093
}
9194

92-
// EnableFeatures enables a feature
95+
// EnableFeatures enables a feature.
9396
func (c *Client) EnableFeatures(feature Feature) {
97+
c.featuresMutex.Lock()
98+
defer c.featuresMutex.Unlock()
9499
c.features[feature] = true
95100
}
96101

97-
// DisableFeatures enables a feature
102+
// DisableFeatures disables a feature.
98103
func (c *Client) DisableFeatures(feature Feature) {
104+
c.featuresMutex.Lock()
105+
defer c.featuresMutex.Unlock()
99106
c.features[feature] = false
100107
}
101108

102-
// IsFeatureEnabled check if a feature is enabled
109+
// IsFeatureEnabled checks if a feature is enabled.
103110
func (c *Client) IsFeatureEnabled(feature Feature) bool {
111+
c.featuresMutex.RLock()
112+
defer c.featuresMutex.RUnlock()
104113
if enabled, ok := c.features[feature]; ok {
105114
return enabled
106115
}

0 commit comments

Comments
 (0)