pool.mx raw

   1  package pool
   2  
   3  // Pool tracks worker FDs and busy state for a single worker type.
   4  // Holds only the common management state; request/response types are
   5  // caller-specific and handled by the caller.
   6  type Pool struct {
   7  	FDs     []int32
   8  	Busy    []bool
   9  	Readers []FrameReader
  10  }
  11  
  12  // NewPool creates a Pool with capacity n.
  13  func NewPool(n int) Pool {
  14  	return Pool{
  15  		FDs:     []int32{:0:n},
  16  		Busy:    []bool{:n},
  17  		Readers: []FrameReader{:n},
  18  	}
  19  }
  20  
  21  // FindByFD returns the index of the worker whose FD matches fd, or -1.
  22  func (p *Pool) FindByFD(fd int32) int {
  23  	for i, wfd := range p.FDs {
  24  		if wfd == fd {
  25  			return i
  26  		}
  27  	}
  28  	return -1
  29  }
  30  
  31  // AddFD appends a new worker FD to the pool.
  32  func (p *Pool) AddFD(fd int32) {
  33  	p.FDs = append(p.FDs, fd)
  34  }
  35  
  36  // Len returns the number of workers.
  37  func (p *Pool) Len() int { return len(p.FDs) }
  38  
  39  // IdleIndex returns the index of the first idle worker, or -1 if all busy.
  40  func (p *Pool) IdleIndex() int {
  41  	for i, busy := range p.Busy {
  42  		if !busy {
  43  			return i
  44  		}
  45  	}
  46  	return -1
  47  }
  48