arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/coordinator/proverspool.go
coordinator/proverspool.goRaw
 1package coordinator
 2
 3import (
 4	"context"
 5
 6	"github.com/hermeznetwork/hermez-node/common"
 7	"github.com/hermeznetwork/hermez-node/log"
 8	"github.com/hermeznetwork/hermez-node/prover"
 9	"github.com/hermeznetwork/tracerr"
10)
11
12// ProversPool contains the multiple prover clients
13type ProversPool struct {
14	pool chan prover.Client
15}
16
17// NewProversPool creates a new pool of provers.
18func NewProversPool(maxServerProofs int) *ProversPool {
19	return &ProversPool{
20		pool: make(chan prover.Client, maxServerProofs),
21	}
22}
23
24// Add a prover to the pool
25func (p *ProversPool) Add(serverProof prover.Client) {
26	p.pool <- serverProof
27}
28
29// Get returns the next available prover
30func (p *ProversPool) Get(ctx context.Context) (prover.Client, error) {
31	select {
32	case <-ctx.Done():
33		log.Info("ServerProofPool.Get done")
34		return nil, tracerr.Wrap(common.ErrDone)
35	case serverProof := <-p.pool:
36		return serverProof, nil
37	}
38}