@@ -20,6 +20,7 @@ import (
20
20
"net/url"
21
21
"runtime"
22
22
"strings"
23
+ "sync"
23
24
"time"
24
25
)
25
26
@@ -46,17 +47,18 @@ var (
46
47
type Feature int
47
48
48
49
const (
49
- // EnableConversationsAPIWhatsAppSandbox Enables the WhatsApp sandbox for conversations API
50
+ // FeatureConversationsAPIWhatsAppSandbox Enables the WhatsApp sandbox for conversations API.
50
51
FeatureConversationsAPIWhatsAppSandbox Feature = iota
51
52
)
52
53
53
54
// Client is used to access API with a given key.
54
55
// Uses standard lib HTTP client internally, so should be reused instead of created as needed and it is safe for concurrent use.
55
56
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.
60
62
}
61
63
62
64
type contentType string
@@ -79,7 +81,8 @@ func New(accessKey string) *Client {
79
81
HTTPClient : & http.Client {
80
82
Timeout : httpClientTimeout ,
81
83
},
82
- features : make (map [Feature ]bool ),
84
+ features : make (map [Feature ]bool ),
85
+ featuresMutex : & sync.RWMutex {},
83
86
}
84
87
}
85
88
@@ -89,18 +92,24 @@ func SetVoiceErrorReader(r errorReader) {
89
92
voiceErrorReader = r
90
93
}
91
94
92
- // EnableFeatures enables a feature
95
+ // EnableFeatures enables a feature.
93
96
func (c * Client ) EnableFeatures (feature Feature ) {
97
+ c .featuresMutex .Lock ()
98
+ defer c .featuresMutex .Unlock ()
94
99
c .features [feature ] = true
95
100
}
96
101
97
- // DisableFeatures enables a feature
102
+ // DisableFeatures disables a feature.
98
103
func (c * Client ) DisableFeatures (feature Feature ) {
104
+ c .featuresMutex .Lock ()
105
+ defer c .featuresMutex .Unlock ()
99
106
c .features [feature ] = false
100
107
}
101
108
102
- // IsFeatureEnabled check if a feature is enabled
109
+ // IsFeatureEnabled checks if a feature is enabled.
103
110
func (c * Client ) IsFeatureEnabled (feature Feature ) bool {
111
+ c .featuresMutex .RLock ()
112
+ defer c .featuresMutex .RUnlock ()
104
113
if enabled , ok := c .features [feature ]; ok {
105
114
return enabled
106
115
}
0 commit comments