Skip to content

Fix HTTP transport message handling #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions transport/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type HTTPTransport struct {
closeHandler func()
mu sync.RWMutex
addr string
responseMap map[int64]chan *transport.BaseJsonRpcMessage
}

// NewHTTPTransport creates a new HTTP transport that listens on the specified endpoint
Expand All @@ -29,7 +28,6 @@ func NewHTTPTransport(endpoint string) *HTTPTransport {
baseTransport: newBaseTransport(),
endpoint: endpoint,
addr: ":8080", // Default port
responseMap: make(map[int64]chan *transport.BaseJsonRpcMessage),
}
}

Expand All @@ -55,14 +53,27 @@ func (t *HTTPTransport) Start(ctx context.Context) error {
// Send implements Transport.Send
func (t *HTTPTransport) Send(ctx context.Context, message *transport.BaseJsonRpcMessage) error {
key := message.JsonRpcResponse.Id
responseChannel := t.responseMap[int64(key)]
fmt.Printf("[Send] Attempting to send response with key: %d\n", key)

responseChannel := t.baseTransport.responseMap[int64(key)]
if responseChannel == nil {
fmt.Printf("[Send] Response map keys: %v\n", t.getResponseMapKeys())

return fmt.Errorf("no response channel found for key: %d", key)
}
responseChannel <- message
return nil
}

// Helper method to get keys
func (t *HTTPTransport) getResponseMapKeys() []int64 {
keys := make([]int64, 0, len(t.baseTransport.responseMap))
for k := range t.baseTransport.responseMap {
keys = append(keys, k)
}
return keys
}

// Close implements Transport.Close
func (t *HTTPTransport) Close() error {
if t.server != nil {
Expand Down Expand Up @@ -94,6 +105,7 @@ func (t *HTTPTransport) SetErrorHandler(handler func(error)) {
func (t *HTTPTransport) SetMessageHandler(handler func(ctx context.Context, message *transport.BaseJsonRpcMessage)) {
t.mu.Lock()
defer t.mu.Unlock()
t.baseTransport.messageHandler = handler
t.messageHandler = handler
}

Expand Down