Categorygithub.com/enix223/goagent
modulepackage
0.2.0
Repository: https://github.com/enix223/goagent.git
Documentation: pkg.go.dev

# README

goagent

goagent is a library to help building task systems in golang. goagent currently supports rabbitmq and redis as MQ brokers.

1. Architecture

+---------+             +-------+------------+
| Agent.1 | <-------->  |       |            |
+---------+             |   g   |  RabbitMQ  |
                        |   o   |  Broker    |            Request
+---------+             |   a   |            | <---- TaskRequestTopic ----  +------------------+
| Agent.2 | <-------->  |   g   |------------+                              | Task distributor |
+---------+             |   e   |            | ----- TaskResultTopic  --->  +------------------+
                        |   n   |   Redis    |            Result
+---------+             |   t   |   Broker   |
| Agent.3 | <-------->  |       |            |
+---------+             +-------+------------+

2. Concepts

  • Agent id

    Agent ID is an idenfitication for each task agent.

  • Task request Topic

    Each task agent will subscribe the given task request topic. The task distributor will send task request to given task request topic for new task. Task request topic is not required to be unique accross different agents. Two or more agents can handle the same task request if you need to.

  • Task result topic

    When the agent finishes processing the request, result is send back the task distributor through Task result topic. The result should be in form of Result.

  • Parallel

    The parallel parameter is used to control how many tasks could be handled simultaneously. The upper boud is the number of cpus, the lower bound is 1. If parallel is set to 1, then only one task could be handled at a time, if the previous task not done, the succceeding task will be block until the previous one finished.

  • Task timeout

    Task timeout is used to control how long the handler function is allowed to handle the request. If the task handle function exceed the timeout limit, ErrTimeout is raised.

  • Request

    The request should conform to interface Request, which implement the GetID method. GetID should return the id for the request.

  • Result

    The task handler should return Response as task response. If Success field is true, means the task handler have process request successfully, otherwise, Error field will contain the error rease for the failure. Result field is the detail of the process result.

3. Examples

# Packages

No description provided by the author
No description provided by the author

# Functions

NewAgent create task agent.
SetAgentID set agent id.
SetBroker set broker.
SetLogger set agent logger.
SetParallel set the number of parallel handlers of the agent If current task handlers reach the limit, all incoming message will be block until any task handler finished.
SetParseRequestFunc set the function to parse request.
SetTaskHandlerFunc set the task handler function.
SetTaskRequestTopic set task request topic.
SetTaskResultTopic set task result topic.
SetTaskTimeout set the timeout interval for the task handle function If task running time exceed the task time, ErrTimeout error is raise.

# Variables

ErrTimeout taks timeout error.

# Structs

Agent task agent.
Options agent config.
Response task response.

# Interfaces

Broker task broker.
Logger logger.
Request task request.

# Type aliases

MessageHandleFunc Task handle function.
Option config option.
ParseRequestFunc parse request function.