feat(registry, grpc): introduce channel ID constants and logging enhancements

- Added constants for channel ID byte size and short length to improve clarity and maintainability.
- Implemented ShortChannelID function to truncate channel IDs for log messages, enhancing readability.
- Updated gRPC tunnel logging to utilize the new ShortChannelID function, ensuring consistent and concise channel ID representation in logs.

Made-with: Cursor
This commit is contained in:
Max 2026-03-13 18:41:15 +08:00
parent 82bb44cbda
commit b667ec310b
2 changed files with 24 additions and 7 deletions

View file

@ -401,14 +401,29 @@ func (r *Registry) bridgeTunnelConn(taiID string, targetPort int, localConn net.
r.logger.Error("no bridge function configured", "tai_id", taiID, "port", targetPort) r.logger.Error("no bridge function configured", "tai_id", taiID, "port", targetPort)
} }
// ChannelIDBytes is the number of random bytes used to generate a channel ID.
// The resulting hex string is 2× this value (64 characters).
const ChannelIDBytes = 32
// ChannelIDShortLen is the max characters shown in log messages.
const ChannelIDShortLen = 16
func generateChannelID() (string, error) { func generateChannelID() (string, error) {
b := make([]byte, 32) b := make([]byte, ChannelIDBytes)
if _, err := rand.Read(b); err != nil { if _, err := rand.Read(b); err != nil {
return "", err return "", err
} }
return hex.EncodeToString(b), nil return hex.EncodeToString(b), nil
} }
// ShortChannelID truncates a channel ID for log display.
func ShortChannelID(id string) string {
if len(id) <= ChannelIDShortLen {
return id
}
return id[:ChannelIDShortLen]
}
type contextCancel struct { type contextCancel struct {
done chan struct{} done chan struct{}
} }

View file

@ -175,12 +175,13 @@ func (h *TunnelHandler) Forward(stream taipb.TaiTunnel_ForwardServer) error {
} }
channelID := vals[0] channelID := vals[0]
h.logger.Debug("[forward] Forward stream arrived", "channel_id", channelID[:16]) short := registry.ShortChannelID(channelID)
h.logger.Debug("[forward] Forward stream arrived", "channel_id", short)
if ch, ok := h.pending.LoadAndDelete(channelID); ok { if ch, ok := h.pending.LoadAndDelete(channelID); ok {
ch.(chan taipb.TaiTunnel_ForwardServer) <- stream ch.(chan taipb.TaiTunnel_ForwardServer) <- stream
} else { } else {
h.logger.Warn("[forward] no pending channel (expired?)", "channel_id", channelID[:16]) h.logger.Warn("[forward] no pending channel (expired?)", "channel_id", short)
return fmt.Errorf("no pending channel for %s", channelID) return fmt.Errorf("no pending channel for %s", channelID)
} }
@ -216,8 +217,9 @@ func (h *TunnelHandler) RequestForward(taiID string, targetPort int) (taipb.TaiT
return nil, fmt.Errorf("tai %s: register stream type mismatch", taiID) return nil, fmt.Errorf("tai %s: register stream type mismatch", taiID)
} }
short := registry.ShortChannelID(channelID)
h.logger.Debug("[forward] sending open command", h.logger.Debug("[forward] sending open command",
"tai_id", taiID, "port", targetPort, "channel_id", channelID[:16]) "tai_id", taiID, "port", targetPort, "channel_id", short)
mu.Lock() mu.Lock()
sendErr := regStream.Send(&taipb.TunnelControl{ sendErr := regStream.Send(&taipb.TunnelControl{
@ -231,15 +233,15 @@ func (h *TunnelHandler) RequestForward(taiID string, targetPort int) (taipb.TaiT
} }
h.logger.Debug("[forward] open sent, waiting for callback", h.logger.Debug("[forward] open sent, waiting for callback",
"tai_id", taiID, "port", targetPort, "channel_id", channelID[:16]) "tai_id", taiID, "port", targetPort, "channel_id", short)
select { select {
case fwd := <-waitCh: case fwd := <-waitCh:
h.logger.Debug("[forward] callback received", h.logger.Debug("[forward] callback received",
"tai_id", taiID, "channel_id", channelID[:16]) "tai_id", taiID, "channel_id", short)
return fwd, nil return fwd, nil
case <-time.After(10 * time.Second): case <-time.After(10 * time.Second):
return nil, fmt.Errorf("tai %s: forward timeout (10s) channel=%s", taiID, channelID[:16]) return nil, fmt.Errorf("tai %s: forward timeout (10s) channel=%s", taiID, short)
case <-regStream.Context().Done(): case <-regStream.Context().Done():
return nil, fmt.Errorf("tai %s: register stream closed while waiting for forward", taiID) return nil, fmt.Errorf("tai %s: register stream closed while waiting for forward", taiID)
} }