Skip to content

Proposal: Introduce Close() error in Transport interface #537

@IAmSurajBobade

Description

@IAmSurajBobade

Transport interface currently only supports Connect method.
This is sufficient for most cases, however it does not give granular control over transports lifecycle. This specifically impacts incase I want to implement my custom transport, like server example.

Describe the solution you'd like
introduce Close() error in Transport interface

type Transport interface {
	Connect(ctx context.Context) (Connection, error)

	// Close closes the transport, releasing any resources.
	//
	Close() error
}

This will give idiomatic way to close a transport object. Following resource clearance logic can be implemented.

// StreamableServerTransport explicitly close connections
func (t *StreamableServerTransport) Close() error {
	if t.connection != nil {
		t.connection.Close()
		t.connection = nil
	}
	return nil
}

// SSEServerTransport mark transport closed
func (t *SSEServerTransport) Close() error {
	t.mu.Lock()
	defer t.mu.Unlock()
	if !t.closed {
		t.closed = true
		close(t.done)
	}
	return nil
}

// InMemoryTransport explicitly close io.ReadWriteCloser
func (t *InMemoryTransport) Close() error { return t.rwc.Close() }

// LoggingTransport close internal Transport instance
func (t *LoggingTransport) Close() error { return t.Transport.Close() }

// StdioTransport empty implementation
func (*StdioTransport) Close() error { return nil }

// CommandTransport empty implementation
func (t *CommandTransport) Close() error { return nil }

This change can also help write clean implementation for #440

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalA proposal for an a new API or behavior. See CONTRIBUTING.md.proposal-declinedProposals that have been declined.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions