update for cl 104

This commit is contained in:
zhuxu 2026-04-17 11:25:55 +08:00
parent 3830fff0d5
commit 2de6483a5e
11 changed files with 42 additions and 46 deletions

View File

@ -9,4 +9,4 @@ steps:
GO111MODULE: on GO111MODULE: on
GOPROXY: https://goproxy.cn,direct GOPROXY: https://goproxy.cn,direct
commands: commands:
- go build -tags "custom,outputs.influxdb_v2" ./cmd/telegraf - go build -tags "custom,inputs.cl_104,outputs.influxdb_v2,parsers.cl_104" ./cmd/telegraf

View File

@ -40,9 +40,9 @@
[[inputs.cl_104]] [[inputs.cl_104]]
service_address = "tcp://:8899" service_address = "tcp://:8899"
path_cl = "/api/104" cl_url="ws://127.0.0.1:8899/api/104"
path_up = "/api/104up" api_path="/api/104"
pong_wait = "60s" pong_wait = "60s"
ping_period = "54s" ping_period = "54s"
write_waite = "10s" write_wait = "10s"
data_format = "cl_104" data_format = "cl_104"

View File

@ -1,4 +1,4 @@
//go:build custom || inputs || inputs.cl_104 //go:build !custom || inputs || inputs.cl_104
package all package all

View File

@ -1,4 +1,4 @@
//go:build custom || inputs || inputs.cl_kafka_consumer //go:build !custom || inputs || inputs.cl_kafka_consumer
package all package all

View File

@ -1,4 +1,4 @@
//go:build custom || inputs || inputs.cl_kafka_subscriber //go:build !custom || inputs || inputs.cl_kafka_subscriber
package all package all

View File

@ -40,9 +40,10 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
service_address = "tcp://:8080" service_address = "tcp://:8080"
## service_address = "tcp://:8443" ## service_address = "tcp://:8443"
## Paths to listen to. ## URL to connect to server
# path_cl="/api/104" cl_url="ws://127.0.0.1:8899/api/104"
# path_up="/api/104up" ## path for access
api_path="/api/104"
## maximum duration before timing out read of the request ## maximum duration before timing out read of the request
# read_timeout = "10s" # read_timeout = "10s"

View File

@ -32,8 +32,8 @@ type CL104 struct {
ServiceAddress string `toml:"service_address"` ServiceAddress string `toml:"service_address"`
ReadTimeout config.Duration `toml:"read_timeout"` ReadTimeout config.Duration `toml:"read_timeout"`
WriteTimeout config.Duration `toml:"write_timeout"` WriteTimeout config.Duration `toml:"write_timeout"`
PathCl string `toml:"path_cl"` CLURL string `toml:"cl_url"`
PathUp string `toml:"path_up"` ApiPath string `toml:"api_path"`
PongWait config.Duration `toml:"pong_wait"` PongWait config.Duration `toml:"pong_wait"`
PingPeriod config.Duration `toml:"ping_period"` PingPeriod config.Duration `toml:"ping_period"`
WriteWait config.Duration `toml:"write_wait"` WriteWait config.Duration `toml:"write_wait"`
@ -52,7 +52,6 @@ type CL104 struct {
listener net.Listener listener net.Listener
url *url.URL url *url.URL
route map[string]func(res http.ResponseWriter, req *http.Request)
upChan chan []byte // confirm upChan chan []byte // confirm
clChan chan []byte // command clChan chan []byte // command
@ -116,10 +115,6 @@ func (h *CL104) Init() error {
h.url = u h.url = u
h.tlsConf = tlsConf h.tlsConf = tlsConf
h.route = map[string]func(res http.ResponseWriter, req *http.Request){
h.PathCl: h.serveClstream,
h.PathUp: h.serveUpstream,
}
h.upChan = make(chan []byte, 16) h.upChan = make(chan []byte, 16)
h.clChan = make(chan []byte, 16) h.clChan = make(chan []byte, 16)
@ -164,6 +159,9 @@ func (h *CL104) Start(acc telegraf.Accumulator) error {
server := h.createHTTPServer() server := h.createHTTPServer()
ctx, cancel := context.WithCancel(context.Background())
go h.connectingCL(ctx, cancel)
h.wg.Add(1) h.wg.Add(1)
go func() { go func() {
defer h.wg.Done() defer h.wg.Done()
@ -172,6 +170,7 @@ func (h *CL104) Start(acc telegraf.Accumulator) error {
h.Log.Errorf("Serve failed: %v", err) h.Log.Errorf("Serve failed: %v", err)
} }
close(h.close) close(h.close)
cancel()
} }
}() }()
@ -193,9 +192,9 @@ func (h *CL104) Stop() {
// ServeHTTP implements [http.Handler] // ServeHTTP implements [http.Handler]
func (h *CL104) ServeHTTP(res http.ResponseWriter, req *http.Request) { func (h *CL104) ServeHTTP(res http.ResponseWriter, req *http.Request) {
handler := h.serveUpstream
handler, ok := h.route[req.URL.Path] if req.URL.Path != h.ApiPath {
if !ok {
handler = http.NotFound handler = http.NotFound
} }

View File

@ -4,43 +4,39 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
) )
var clConnNum int64 func (h *CL104) connectingCL(ctx context.Context, cancel context.CancelFunc) {
for {
func (h *CL104) serveClstream(res http.ResponseWriter, req *http.Request) { select {
select { case <-ctx.Done():
case <-h.close:
res.WriteHeader(http.StatusGone)
return
default:
if atomic.SwapInt64(&clConnNum, 1) > 0 {
res.WriteHeader(http.StatusConflict)
return return
case <-h.close:
return
default:
h.newConnectCL(ctx, cancel)
time.Sleep(time.Second * 5)
} }
} }
}
conn, err := upgrader.Upgrade(res, req, nil) func (h *CL104) newConnectCL(ctx context.Context, cancel context.CancelFunc) {
c, _, err := websocket.DefaultDialer.DialContext(ctx, h.CLURL, nil)
if err != nil { if err != nil {
h.Log.Error(err) h.Log.Error("client dial:", err)
return return
} }
defer conn.Close() defer c.Close()
stopCtx, stopCancel := context.WithCancel(context.Background())
defer stopCancel()
session := &wsSession{ session := &wsSession{
conn: conn, conn: c,
ctx: stopCtx, ctx: ctx,
cancel: stopCancel, cancel: cancel,
ctrlCh: make(chan wsMsg, 2), ctrlCh: make(chan wsMsg, 2),
} }
@ -74,7 +70,6 @@ func (h *CL104) startClWorkers(session *wsSession) {
}() }()
wg.Wait() wg.Wait()
atomic.SwapInt64(&clConnNum, 0)
} }
func (h *CL104) monitorClWrite(session *wsSession) { func (h *CL104) monitorClWrite(session *wsSession) {

View File

@ -6,9 +6,10 @@
service_address = "tcp://:8080" service_address = "tcp://:8080"
## service_address = "tcp://:8443" ## service_address = "tcp://:8443"
## Paths to listen to. ## URL to connect to server
# path_cl="/api/104" cl_url="ws://127.0.0.1:8899/api/104"
# path_up="/api/104up" ## path for access
api_path="/api/104"
## maximum duration before timing out read of the request ## maximum duration before timing out read of the request
# read_timeout = "10s" # read_timeout = "10s"

View File

@ -1,4 +1,4 @@
//go:build custom || parsers || parsers.cl_104 //go:build !custom || parsers || parsers.cl_104
package all package all

View File

@ -1,4 +1,4 @@
//go:build custom || parsers || parsers.phasor_binary //go:build !custom || parsers || parsers.phasor_binary
package all package all