** Copyright (C) 2001-2023 Zabbix SIA
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"git.zabbix.com/ap/plugin-support/plugin"
// pluginAgent manages plugin usage
type pluginAgent struct {
// queue of tasks to perform
// maximum plugin capacity
// force active check on first configuration
forceActiveChecksOnStart int
// refcount us used to track plugin usage by clients
// usrprm is used to indicate that plugin is user parameter
// peekTask() returns next task in the queue without removing it from queue or nil
// if the queue is empty.
func (p *pluginAgent) peekTask() performer {
// popTask() returns next task in the queue and removes it from queue.
// nil is returned for empty queues.
func (p *pluginAgent) popTask() performer {
func (p *pluginAgent) enqueueTask(task performer) {
heap.Push(&p.tasks, task)
func (p *pluginAgent) removeTask(index int) {
heap.Remove(&p.tasks, index)
func (p *pluginAgent) reserveCapacity(task performer) {
p.usedCapacity += task.getWeight()
func (p *pluginAgent) releaseCapacity(task performer) {
p.usedCapacity -= task.getWeight()
func (p *pluginAgent) queued() bool {
func (p *pluginAgent) hasCapacity() bool {
return len(p.tasks) != 0 && p.maxCapacity-p.usedCapacity >= p.tasks[0].getWeight()