improves concurrent GetAll operations
This commit is contained in:
@@ -2,6 +2,7 @@ package edgeos
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@@ -24,36 +25,28 @@ func (c *Client) GetInterfaces(ctx context.Context, host string) ([]Interface, e
|
|||||||
// GetAllInterfaces retrieves interfaces for all devices.
|
// GetAllInterfaces retrieves interfaces for all devices.
|
||||||
func (c *Client) GetAllInterfaces(ctx context.Context) (map[string][]Interface, error) {
|
func (c *Client) GetAllInterfaces(ctx context.Context) (map[string][]Interface, error) {
|
||||||
results := make(map[string][]Interface)
|
results := make(map[string][]Interface)
|
||||||
var mu sync.Mutex
|
var (
|
||||||
var wg sync.WaitGroup
|
mu sync.Mutex
|
||||||
|
wg sync.WaitGroup
|
||||||
// Use a buffered channel or just loop?
|
errs error
|
||||||
// Since we return error if any fails? Or partial results?
|
)
|
||||||
// Usually partial results + error or composite error.
|
|
||||||
// I will return partial results and the last error for now, or just stop on error?
|
|
||||||
// "methods to get ... for either all device"
|
|
||||||
// I will implement parallel fetch.
|
|
||||||
|
|
||||||
for host := range c.devices {
|
for host := range c.devices {
|
||||||
wg.Add(1)
|
wg.Go(func() {
|
||||||
go func(h string) {
|
res, err := c.GetInterfaces(ctx, host)
|
||||||
defer wg.Done()
|
|
||||||
res, err := c.GetInterfaces(ctx, h)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// For now, log error or ignore?
|
mu.Lock()
|
||||||
// We should probably return an error map or just return what we have?
|
errs = errors.Join(errs, err)
|
||||||
// I will just skip failed ones for this implementation or log?
|
mu.Unlock()
|
||||||
// I'll return what succeeds.
|
|
||||||
// The prompt doesn't specify error handling strategy for "all".
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
results[h] = res
|
results[host] = res
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
}(host)
|
})
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
return results, nil
|
return results, errs
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDevice retrieves the device info for a specific device.
|
// GetDevice retrieves the device info for a specific device.
|
||||||
@@ -74,24 +67,28 @@ func (c *Client) GetDevice(ctx context.Context, host string) (*Device, error) {
|
|||||||
// GetAllDevices retrieves device info for all devices.
|
// GetAllDevices retrieves device info for all devices.
|
||||||
func (c *Client) GetAllDevices(ctx context.Context) (map[string]*Device, error) {
|
func (c *Client) GetAllDevices(ctx context.Context) (map[string]*Device, error) {
|
||||||
results := make(map[string]*Device)
|
results := make(map[string]*Device)
|
||||||
var mu sync.Mutex
|
var (
|
||||||
var wg sync.WaitGroup
|
mu sync.Mutex
|
||||||
|
wg sync.WaitGroup
|
||||||
|
errs error
|
||||||
|
)
|
||||||
|
|
||||||
for host := range c.devices {
|
for host := range c.devices {
|
||||||
wg.Add(1)
|
wg.Go(func() {
|
||||||
go func(h string) {
|
res, err := c.GetDevice(ctx, host)
|
||||||
defer wg.Done()
|
|
||||||
res, err := c.GetDevice(ctx, h)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
mu.Lock()
|
||||||
|
errs = errors.Join(errs, err)
|
||||||
|
mu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
results[h] = res
|
results[host] = res
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
}(host)
|
})
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
return results, nil
|
return results, errs
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSystem retrieves the system info for a specific device.
|
// GetSystem retrieves the system info for a specific device.
|
||||||
@@ -112,24 +109,28 @@ func (c *Client) GetSystem(ctx context.Context, host string) (*System, error) {
|
|||||||
// GetAllSystems retrieves system info for all devices.
|
// GetAllSystems retrieves system info for all devices.
|
||||||
func (c *Client) GetAllSystems(ctx context.Context) (map[string]*System, error) {
|
func (c *Client) GetAllSystems(ctx context.Context) (map[string]*System, error) {
|
||||||
results := make(map[string]*System)
|
results := make(map[string]*System)
|
||||||
var mu sync.Mutex
|
var (
|
||||||
var wg sync.WaitGroup
|
mu sync.Mutex
|
||||||
|
wg sync.WaitGroup
|
||||||
|
errs error
|
||||||
|
)
|
||||||
|
|
||||||
for host := range c.devices {
|
for host := range c.devices {
|
||||||
wg.Add(1)
|
wg.Go(func() {
|
||||||
go func(h string) {
|
res, err := c.GetSystem(ctx, host)
|
||||||
defer wg.Done()
|
|
||||||
res, err := c.GetSystem(ctx, h)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
mu.Lock()
|
||||||
|
errs = errors.Join(errs, err)
|
||||||
|
mu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
results[h] = res
|
results[host] = res
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
}(host)
|
})
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
return results, nil
|
return results, errs
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetVLANs retrieves the VLANs for a specific device.
|
// GetVLANs retrieves the VLANs for a specific device.
|
||||||
@@ -150,24 +151,28 @@ func (c *Client) GetVLANs(ctx context.Context, host string) (*VLANs, error) {
|
|||||||
// GetAllVLANs retrieves VLANs for all devices.
|
// GetAllVLANs retrieves VLANs for all devices.
|
||||||
func (c *Client) GetAllVLANs(ctx context.Context) (map[string]*VLANs, error) {
|
func (c *Client) GetAllVLANs(ctx context.Context) (map[string]*VLANs, error) {
|
||||||
results := make(map[string]*VLANs)
|
results := make(map[string]*VLANs)
|
||||||
var mu sync.Mutex
|
var (
|
||||||
var wg sync.WaitGroup
|
mu sync.Mutex
|
||||||
|
wg sync.WaitGroup
|
||||||
|
errs error
|
||||||
|
)
|
||||||
|
|
||||||
for host := range c.devices {
|
for host := range c.devices {
|
||||||
wg.Add(1)
|
wg.Go(func() {
|
||||||
go func(h string) {
|
res, err := c.GetVLANs(ctx, host)
|
||||||
defer wg.Done()
|
|
||||||
res, err := c.GetVLANs(ctx, h)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
mu.Lock()
|
||||||
|
errs = errors.Join(errs, err)
|
||||||
|
mu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
results[h] = res
|
results[host] = res
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
}(host)
|
})
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
return results, nil
|
return results, errs
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetServices retrieves the services for a specific device.
|
// GetServices retrieves the services for a specific device.
|
||||||
@@ -188,24 +193,28 @@ func (c *Client) GetServices(ctx context.Context, host string) (*Services, error
|
|||||||
// GetAllServices retrieves services for all devices.
|
// GetAllServices retrieves services for all devices.
|
||||||
func (c *Client) GetAllServices(ctx context.Context) (map[string]*Services, error) {
|
func (c *Client) GetAllServices(ctx context.Context) (map[string]*Services, error) {
|
||||||
results := make(map[string]*Services)
|
results := make(map[string]*Services)
|
||||||
var mu sync.Mutex
|
var (
|
||||||
var wg sync.WaitGroup
|
mu sync.Mutex
|
||||||
|
wg sync.WaitGroup
|
||||||
|
errs error
|
||||||
|
)
|
||||||
|
|
||||||
for host := range c.devices {
|
for host := range c.devices {
|
||||||
wg.Add(1)
|
wg.Go(func() {
|
||||||
go func(h string) {
|
res, err := c.GetServices(ctx, host)
|
||||||
defer wg.Done()
|
|
||||||
res, err := c.GetServices(ctx, h)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
mu.Lock()
|
||||||
|
errs = errors.Join(errs, err)
|
||||||
|
mu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
results[h] = res
|
results[host] = res
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
}(host)
|
})
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
return results, nil
|
return results, errs
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetStatistics retrieves the statistics for a specific device.
|
// GetStatistics retrieves the statistics for a specific device.
|
||||||
@@ -226,24 +235,28 @@ func (c *Client) GetStatistics(ctx context.Context, host string) ([]Statistics,
|
|||||||
// GetAllStatistics retrieves statistics for all devices.
|
// GetAllStatistics retrieves statistics for all devices.
|
||||||
func (c *Client) GetAllStatistics(ctx context.Context) (map[string][]Statistics, error) {
|
func (c *Client) GetAllStatistics(ctx context.Context) (map[string][]Statistics, error) {
|
||||||
results := make(map[string][]Statistics)
|
results := make(map[string][]Statistics)
|
||||||
var mu sync.Mutex
|
var (
|
||||||
var wg sync.WaitGroup
|
mu sync.Mutex
|
||||||
|
wg sync.WaitGroup
|
||||||
|
errs error
|
||||||
|
)
|
||||||
|
|
||||||
for host := range c.devices {
|
for host := range c.devices {
|
||||||
wg.Add(1)
|
wg.Go(func() {
|
||||||
go func(h string) {
|
res, err := c.GetStatistics(ctx, host)
|
||||||
defer wg.Done()
|
|
||||||
res, err := c.GetStatistics(ctx, h)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
mu.Lock()
|
||||||
|
errs = errors.Join(errs, err)
|
||||||
|
mu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
results[h] = res
|
results[host] = res
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
}(host)
|
})
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
return results, nil
|
return results, errs
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNeighbors retrieves the neighbors for a specific device.
|
// GetNeighbors retrieves the neighbors for a specific device.
|
||||||
@@ -264,22 +277,26 @@ func (c *Client) GetNeighbors(ctx context.Context, host string) ([]Neighbor, err
|
|||||||
// GetAllNeighbors retrieves neighbors for all devices.
|
// GetAllNeighbors retrieves neighbors for all devices.
|
||||||
func (c *Client) GetAllNeighbors(ctx context.Context) (map[string][]Neighbor, error) {
|
func (c *Client) GetAllNeighbors(ctx context.Context) (map[string][]Neighbor, error) {
|
||||||
results := make(map[string][]Neighbor)
|
results := make(map[string][]Neighbor)
|
||||||
var mu sync.Mutex
|
var (
|
||||||
var wg sync.WaitGroup
|
mu sync.Mutex
|
||||||
|
wg sync.WaitGroup
|
||||||
|
errs error
|
||||||
|
)
|
||||||
|
|
||||||
for host := range c.devices {
|
for host := range c.devices {
|
||||||
wg.Add(1)
|
wg.Go(func() {
|
||||||
go func(h string) {
|
res, err := c.GetNeighbors(ctx, host)
|
||||||
defer wg.Done()
|
|
||||||
res, err := c.GetNeighbors(ctx, h)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
mu.Lock()
|
||||||
|
errs = errors.Join(errs, err)
|
||||||
|
mu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
results[h] = res
|
results[host] = res
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
}(host)
|
})
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
return results, nil
|
return results, errs
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -361,4 +361,4 @@ type Neighbor struct {
|
|||||||
IP string `json:"ip"`
|
IP string `json:"ip"`
|
||||||
ZoneID string `json:"zoneID"`
|
ZoneID string `json:"zoneID"`
|
||||||
Addresses []NeighborAddress `json:"addresses"`
|
Addresses []NeighborAddress `json:"addresses"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user