灵隙(synapse) v1.0.0 版本

This commit is contained in:
lychang
2026-06-29 00:48:45 +08:00
parent 6d1234efff
commit db716f94ac
6 changed files with 334 additions and 107 deletions

View File

@@ -41,6 +41,10 @@ type HTTPTransport struct {
// Callbacks
callbackEndpoint string
callbackHandler MessageCallbackHandler
// Pending callbacks (for SendWithCallback)
pendingCallbacks map[string]chan *types.UnifiedSynapseMessage
pendingMu sync.RWMutex
}
// HTTPTransportConfig holds HTTP transport configuration
@@ -68,11 +72,12 @@ func NewHTTPTransport(config *HTTPTransportConfig) *HTTPTransport {
}
t := &HTTPTransport{
baseURL: config.BaseURL,
timeout: config.Timeout,
maxRetries: config.MaxRetries,
retryDelay: config.RetryDelay,
authSecret: config.AuthSecret,
baseURL: config.BaseURL,
timeout: config.Timeout,
maxRetries: config.MaxRetries,
retryDelay: config.RetryDelay,
authSecret: config.AuthSecret,
pendingCallbacks: make(map[string]chan *types.UnifiedSynapseMessage),
client: &http.Client{
Timeout: config.Timeout,
},
@@ -177,6 +182,18 @@ func (t *HTTPTransport) SendWithCallback(
correlationID = msg.ID
}
// Register pending callback
t.pendingMu.Lock()
t.pendingCallbacks[correlationID] = respChan
t.pendingMu.Unlock()
// Cleanup on exit
defer func() {
t.pendingMu.Lock()
delete(t.pendingCallbacks, correlationID)
t.pendingMu.Unlock()
}()
// Start timeout goroutine
go func() {
select {
@@ -240,7 +257,33 @@ func (t *HTTPTransport) HandleCallback(w http.ResponseWriter, r *http.Request) {
}
}
// Handle message
// Check if this is a pending callback response
correlationID := msg.CorrelationID
if correlationID == "" {
correlationID = msg.ID
}
t.pendingMu.RLock()
ch, pending := t.pendingCallbacks[correlationID]
t.pendingMu.RUnlock()
if pending {
// Send response to waiting goroutine
select {
case ch <- &msg:
default:
}
// Send success response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"message": "callback received",
})
return
}
// Handle message via callback handler
if t.callbackHandler != nil {
if err := t.callbackHandler.HandleMessage(&msg); err != nil {
log.Printf("[Transport] Failed to handle message: %v", err)