-
Notifications
You must be signed in to change notification settings - Fork 27
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
Try Generics #19
Draft
fortuna
wants to merge
2
commits into
main
Choose a base branch
from
fortuna-generics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Try Generics #19
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -20,7 +20,7 @@ import ( | |||||||||
"net" | ||||||||||
) | ||||||||||
|
||||||||||
// StreamConn is a net.Conn that allows for closing only the reader or writer end of | ||||||||||
// StreamConn is a [net.Conn] that allows for closing only the reader or writer end of | ||||||||||
// it, supporting half-open state. | ||||||||||
type StreamConn interface { | ||||||||||
net.Conn | ||||||||||
|
@@ -71,10 +71,7 @@ func WrapConn(c StreamConn, r io.Reader, w io.Writer) StreamConn { | |||||||||
} | ||||||||||
|
||||||||||
// StreamEndpoint represents an endpoint that can be used to established stream connections (like TCP) to a fixed destination. | ||||||||||
type StreamEndpoint interface { | ||||||||||
// Connect establishes a connection with the endpoint, returning the connection. | ||||||||||
Connect(ctx context.Context) (StreamConn, error) | ||||||||||
} | ||||||||||
type StreamEndpoint = Endpoint[StreamConn] | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this is better:
Suggested change
|
||||||||||
|
||||||||||
// TCPEndpoint is a [StreamEndpoint] that connects to the given address using the given [StreamDialer]. | ||||||||||
type TCPEndpoint struct { | ||||||||||
|
@@ -96,35 +93,19 @@ func (e *TCPEndpoint) Connect(ctx context.Context) (StreamConn, error) { | |||||||||
return conn.(*net.TCPConn), nil | ||||||||||
} | ||||||||||
|
||||||||||
// StreamDialerEndpoint is a [StreamEndpoint] that connects to the given address using the given [StreamDialer]. | ||||||||||
type StreamDialerEndpoint struct { | ||||||||||
Dialer StreamDialer | ||||||||||
Address string | ||||||||||
} | ||||||||||
|
||||||||||
var _ StreamEndpoint = (*StreamDialerEndpoint)(nil) | ||||||||||
|
||||||||||
// Connect implements [StreamEndpoint].Connect. | ||||||||||
func (e *StreamDialerEndpoint) Connect(ctx context.Context) (StreamConn, error) { | ||||||||||
return e.Dialer.Dial(ctx, e.Address) | ||||||||||
} | ||||||||||
|
||||||||||
// StreamDialer provides a way to dial a destination and establish stream connections. | ||||||||||
type StreamDialer interface { | ||||||||||
// Dial connects to `raddr`. | ||||||||||
// `raddr` has the form `host:port`, where `host` can be a domain name or IP address. | ||||||||||
Dial(ctx context.Context, raddr string) (StreamConn, error) | ||||||||||
} | ||||||||||
type StreamDialer = Dialer[StreamConn] | ||||||||||
|
||||||||||
// TCPStreamDialer is a [StreamDialer] that uses the standard [net.Dialer] to dial. | ||||||||||
// TCPDialer is a [StreamDialer] that uses the standard [net.Dialer] to dial. | ||||||||||
// It provides a convenient way to use a [net.Dialer] when you need a [StreamDialer]. | ||||||||||
type TCPStreamDialer struct { | ||||||||||
type TCPDialer struct { | ||||||||||
Dialer net.Dialer | ||||||||||
} | ||||||||||
|
||||||||||
var _ StreamDialer = (*TCPStreamDialer)(nil) | ||||||||||
var _ StreamDialer = (*TCPDialer)(nil) | ||||||||||
|
||||||||||
func (d *TCPStreamDialer) Dial(ctx context.Context, addr string) (StreamConn, error) { | ||||||||||
// Dial implements [Dialer].Dial. | ||||||||||
func (d *TCPDialer) Dial(ctx context.Context, addr string) (StreamConn, error) { | ||||||||||
conn, err := d.Dialer.DialContext(ctx, "tcp", addr) | ||||||||||
if err != nil { | ||||||||||
return nil, err | ||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2019 Jigsaw Operations LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package transport | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// Endpoint represents an endpoint that can be used to established connections to a fixed destination. | ||
type Endpoint[Conn any] interface { | ||
// Connect creates a connection bound to an endpoint, returning the connection. | ||
Connect(ctx context.Context) (Conn, error) | ||
} | ||
|
||
// DialerEndpoint is an [Endpoint] that connects to the given address using the given [Dialer]. | ||
// Useful if you talk repeatedly to an endpoint, such as a proxy or DNS resolver, or | ||
// if you find yourself passing a (dialer, address) pair around. | ||
func NewDialerEndpoint[C any](dialer Dialer[C], address string) Endpoint[C] { | ||
return &dialerEndpoint[C]{dialer, address} | ||
} | ||
|
||
type dialerEndpoint[Conn any] struct { | ||
Dialer Dialer[Conn] | ||
Address string | ||
} | ||
|
||
var _ Endpoint[any] = (*dialerEndpoint[any])(nil) | ||
|
||
// Connect implements [Endpoint].Connect. | ||
func (e *dialerEndpoint[Conn]) Connect(ctx context.Context) (Conn, error) { | ||
return e.Dialer.Dial(ctx, e.Address) | ||
} | ||
|
||
// Dialer provides a way to dial a destination and establish connections. | ||
type Dialer[Conn any] interface { | ||
// Dial connects to `raddr`. | ||
// `raddr` has the form `host:port`, where `host` can be a domain name or IP address. | ||
Dial(ctx context.Context, raddr string) (Conn, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if I should define
That would be a parallel to
StreamConn
and allow us to expand in the future, if needed.It would, however, be confusing with
net.PacketConn
. Perhaps it makes sense to call itDatagramConn
orDgramConn
, to mirror posix sockets.