Skip to content

Commit

Permalink
added rpc clients management
Browse files Browse the repository at this point in the history
  • Loading branch information
billettc committed Jun 27, 2024
1 parent 1563afc commit eaebe79
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions rpc/clients.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package rpc

import (
"errors"

"github.com/hashicorp/go-multierror"
)

var ErrorNoMoreClient = errors.New("no more clients")

type Clients[C any] struct {
clients []C
next int
}

func (c *Clients[C]) Next() (client C, err error) {
if len(c.clients) <= c.next {
return client, ErrorNoMoreClient
}
client = c.clients[c.next]
c.next++
return client, nil
}

func WithClients[C any, V any](clients *Clients[C], f func(C) (v V, err error)) (v V, err error) {
var errs error
for {
client, err := clients.Next()
if err != nil {
return v, err
}
v, err := f(client)
if err != nil {
errs = multierror.Append(errs, err)
continue
}
return v, nil
}
}

0 comments on commit eaebe79

Please sign in to comment.