1 Star 0 Fork 0

王仁一/modbus-mock

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
device_pool.go 1.22 KB
一键复制 编辑 原始数据 按行查看 历史
wangquanfu 提交于 2021-05-07 11:01 +08:00 . update model request
package main
import (
"context"
)
type deviceProtoctlType uint8
const (
tcpDeviceProtocol deviceProtoctlType = iota
rtuDeviceProtocol
)
var (
connectionRequestQueueSize uint32 = 100
)
type DevicePool struct {
deqChan chan *DeviceRequest
openerCh chan *DeviceRequest
ctx context.Context
}
func OpenDevicePool(parentCtx context.Context) *DevicePool {
devicePool := &DevicePool{
deqChan: make(chan *DeviceRequest, 1),
openerCh: make(chan *DeviceRequest, connectionRequestQueueSize),
ctx: parentCtx,
}
//生产 conn
go devicePool.connectionOpener()
//消费 conn
go devicePool.runConnDevice()
return devicePool
}
func (devicePool *DevicePool) connectionOpener() {
for {
select {
case <-devicePool.ctx.Done():
return
case d := <-devicePool.openerCh:
devicePool.deqChan <- &DeviceRequest{
ModpollParams: d.ModpollParams,
}
}
}
}
func (devicePool *DevicePool) runConnDevice() error {
for {
select {
default:
case device, ok := <-devicePool.deqChan:
if ok && device != nil {
deviceQ := &DeviceRequest{
ModpollParams: device.ModpollParams,
}
go deviceQ.Exec(devicePool.ctx)
}
case <-devicePool.ctx.Done():
return devicePool.ctx.Err()
}
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/handshake/modbus-mock.git
git@gitee.com:handshake/modbus-mock.git
handshake
modbus-mock
modbus-mock
master

搜索帮助