Add thread-safe Add/Del methods and refactor client locking
- Add Add and Del methods to Client for dynamic host management. - Add RWMutex to Client to protect the devices map. - Add Transport to Config to allow mocking HTTP transport in tests. - Add getDeviceByHost helper to centralize device lookup locking. - Refactor GetAll* methods to snapshot host keys before iteration to avoid concurrent map read/write panic. - Add tests for thread safety and Add/Del functionality.
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
||||
|
||||
// Client handles communication with EdgeOS devices.
|
||||
type Client struct {
|
||||
mu sync.RWMutex
|
||||
devices map[string]*deviceClient
|
||||
}
|
||||
|
||||
@@ -31,33 +32,42 @@ type deviceClient struct {
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func newDeviceClient(cfg Config) *deviceClient {
|
||||
// Ensure scheme is set
|
||||
if cfg.Scheme == "" {
|
||||
cfg.Scheme = "https"
|
||||
}
|
||||
|
||||
var tr http.RoundTripper
|
||||
if cfg.Transport != nil {
|
||||
tr = cfg.Transport
|
||||
} else {
|
||||
defaultTr := http.DefaultTransport.(*http.Transport).Clone()
|
||||
if defaultTr.TLSClientConfig == nil {
|
||||
defaultTr.TLSClientConfig = &tls.Config{}
|
||||
}
|
||||
defaultTr.TLSClientConfig.InsecureSkipVerify = cfg.Insecure
|
||||
tr = defaultTr
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Transport: tr,
|
||||
Timeout: cfg.Timeout,
|
||||
}
|
||||
|
||||
return &deviceClient{
|
||||
config: cfg,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
// MustNew creates a new Client with the given configurations.
|
||||
// It panics if a configuration is invalid (though currently we just accept all).
|
||||
func MustNew(ctx context.Context, configs []Config) *Client {
|
||||
devices := make(map[string]*deviceClient)
|
||||
|
||||
for _, cfg := range configs {
|
||||
// Use Host as the key.
|
||||
// Ensure scheme is set
|
||||
if cfg.Scheme == "" {
|
||||
cfg.Scheme = "https"
|
||||
}
|
||||
|
||||
tr := http.DefaultTransport.(*http.Transport).Clone()
|
||||
if tr.TLSClientConfig == nil {
|
||||
tr.TLSClientConfig = &tls.Config{}
|
||||
}
|
||||
tr.TLSClientConfig.InsecureSkipVerify = cfg.Insecure
|
||||
|
||||
client := &http.Client{
|
||||
Transport: tr,
|
||||
Timeout: cfg.Timeout,
|
||||
}
|
||||
|
||||
devices[cfg.Host] = &deviceClient{
|
||||
config: cfg,
|
||||
client: client,
|
||||
}
|
||||
devices[cfg.Host] = newDeviceClient(cfg)
|
||||
}
|
||||
|
||||
return &Client{
|
||||
@@ -65,6 +75,51 @@ func MustNew(ctx context.Context, configs []Config) *Client {
|
||||
}
|
||||
}
|
||||
|
||||
// Add adds a new device to the client.
|
||||
// It returns an error if a device with the same host already exists.
|
||||
func (c *Client) Add(cfg *Config) error {
|
||||
if cfg == nil {
|
||||
return fmt.Errorf("config cannot be nil")
|
||||
}
|
||||
|
||||
d := newDeviceClient(*cfg)
|
||||
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if _, ok := c.devices[cfg.Host]; ok {
|
||||
return fmt.Errorf("device already exists: %s", cfg.Host)
|
||||
}
|
||||
|
||||
c.devices[cfg.Host] = d
|
||||
return nil
|
||||
}
|
||||
|
||||
// Del removes a device from the client.
|
||||
// It returns an error if the device does not exist.
|
||||
func (c *Client) Del(host string) error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if _, ok := c.devices[host]; !ok {
|
||||
return fmt.Errorf("device not found: %s", host)
|
||||
}
|
||||
|
||||
delete(c.devices, host)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) getDeviceByHost(host string) (*deviceClient, error) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
d, ok := c.devices[host]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("device not found: %s", host)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *deviceClient) login(ctx context.Context) error {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user