bf9395e022
CI / license-header (push) Has been skipped
CI / e2e-dry-run (push) Has been skipped
CI / fast-gate (push) Failing after 0s
Test PR Label Logic / test-pr-labels (push) Failing after 1s
Skill Format Check / check-format (push) Failing after 2s
CI / security (push) Failing after 5s
CI / unit-test (push) Has been skipped
CI / lint (push) Has been skipped
CI / script-test (push) Has been skipped
CI / deterministic-gate (push) Has been skipped
CI / coverage (push) Has been skipped
CI / results (push) Has been cancelled
CI / deadcode (push) Has been cancelled
CI / e2e-live (push) Has been cancelled
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package consume
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/larksuite/cli/internal/event"
|
|
)
|
|
|
|
type APIClient = event.APIClient
|
|
|
|
// CheckRemoteConnections returns the count of active WebSocket connections for this app.
|
|
func CheckRemoteConnections(ctx context.Context, client APIClient) (int, error) {
|
|
raw, err := client.CallAPI(ctx, "GET", "/open-apis/event/v1/connection", nil)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("connection check: %w", err)
|
|
}
|
|
var result struct {
|
|
Code int `json:"code"`
|
|
Msg string `json:"msg"`
|
|
Data struct {
|
|
OnlineInstanceCnt int `json:"online_instance_cnt"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(raw, &result); err != nil {
|
|
return 0, fmt.Errorf("connection check: decode: %w (body=%s)", err, truncateForError(raw))
|
|
}
|
|
// Distinguish "verified zero" from "check failed" — non-zero code decodes Cnt=0.
|
|
if result.Code != 0 {
|
|
return 0, fmt.Errorf("connection check: api error code=%d msg=%q", result.Code, result.Msg)
|
|
}
|
|
return result.Data.OnlineInstanceCnt, nil
|
|
}
|
|
|
|
// truncateForError bounds length and collapses control chars to defang log injection.
|
|
func truncateForError(b []byte) string {
|
|
const max = 256
|
|
s := string(b)
|
|
if len(s) > max {
|
|
s = s[:max] + "…(truncated)"
|
|
}
|
|
out := make([]byte, 0, len(s))
|
|
for _, r := range s {
|
|
if r == '\n' || r == '\r' || r == '\t' {
|
|
out = append(out, ' ')
|
|
continue
|
|
}
|
|
out = append(out, string(r)...)
|
|
}
|
|
return string(out)
|
|
}
|