54 lines
915 B
Go
54 lines
915 B
Go
package manager
|
|
|
|
import (
|
|
"gitlink.org.cn/cloudream/common/pkgs/mq"
|
|
schmq "gitlink.org.cn/cloudream/scheduler/common/pkgs/mq"
|
|
)
|
|
|
|
type Client struct {
|
|
roundTripper mq.RoundTripper
|
|
}
|
|
|
|
func NewClient(cfg *schmq.Config) (*Client, error) {
|
|
rabbitCli, err := mq.NewRabbitMQTransport(cfg.MakeConnectingURL(), ServerQueueName, "")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Client{
|
|
roundTripper: rabbitCli,
|
|
}, nil
|
|
}
|
|
|
|
func NewClientWithRoundTripper(rd mq.RoundTripper) (*Client, error) {
|
|
return &Client{
|
|
roundTripper: rd,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) Close() {
|
|
c.roundTripper.Close()
|
|
}
|
|
|
|
type Pool interface {
|
|
Acquire() (*Client, error)
|
|
Release(cli *Client)
|
|
}
|
|
|
|
type pool struct {
|
|
mqcfg *schmq.Config
|
|
}
|
|
|
|
func NewPool(mqcfg *schmq.Config) Pool {
|
|
return &pool{
|
|
mqcfg: mqcfg,
|
|
}
|
|
}
|
|
func (p *pool) Acquire() (*Client, error) {
|
|
return NewClient(p.mqcfg)
|
|
}
|
|
|
|
func (p *pool) Release(cli *Client) {
|
|
cli.Close()
|
|
}
|