diff options
| author | Paul Buetow <paul@buetow.org> | 2024-07-03 11:45:14 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-07-03 11:45:14 +0300 |
| commit | bfa17633390b7d7074bd584019e808eca4dd59a2 (patch) | |
| tree | 7f37104d27a7012604bd05e2994944091c62379b /internal/easyhttp | |
| parent | 4bde5943fa9bee99e8ad1e570d05e142cbacade7 (diff) | |
add no servers configured error
Diffstat (limited to 'internal/easyhttp')
| -rw-r--r-- | internal/easyhttp/easyhttp.go | 12 | ||||
| -rw-r--r-- | internal/easyhttp/saferrors.go | 8 |
2 files changed, 12 insertions, 8 deletions
diff --git a/internal/easyhttp/easyhttp.go b/internal/easyhttp/easyhttp.go index bb963ad..7b72b9e 100644 --- a/internal/easyhttp/easyhttp.go +++ b/internal/easyhttp/easyhttp.go @@ -1,11 +1,11 @@ package easyhttp import ( - "errors" + "encoding/json" "fmt" "io" "net/http" - "encoding/json" + "sync" ) func Get(uri, apiKey string) ([]byte, error) { @@ -50,14 +50,18 @@ func PostData[T any](uri, apiKey string, data *T, servers ...string) error { if len(servers) == 0 { return fmt.Errorf("no server configured") } - var errs SafeErrors + var errs safErrors + var wg sync.WaitGroup for _, server := range servers { + wg.Add(1) go func(server string){ - errs.Append(postData[T](fmt.Sprintf("%s/%s"), apiKey, data)) + defer wg.Done() + errs.Append(postData[T](fmt.Sprintf("%s/%s", server, uri), apiKey, data)) }(server) } + wg.Wait() return errs.Join() } diff --git a/internal/easyhttp/saferrors.go b/internal/easyhttp/saferrors.go index 32dd77f..464c90d 100644 --- a/internal/easyhttp/saferrors.go +++ b/internal/easyhttp/saferrors.go @@ -6,12 +6,12 @@ import ( ) // Safe errors -type SafErrors struct { +type safErrors struct { errs []error mutex sync.Mutex } -func (errs *SafErrors) Append(err error) { +func (errs *safErrors) Append(err error) { if err == nil { return } @@ -20,12 +20,12 @@ func (errs *SafErrors) Append(err error) { errs.errs = append(errs.errs, err) } -func (errs *SafErrors) Join() error { +func (errs *safErrors) Join() error { errs.mutex.Lock() defer errs.mutex.Unlock() return errors.Join(errs.errs...) } -func (errs *SafErrors) Error() string { +func (errs *safErrors) Error() string { return errs.Join().Error() } |
