-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c88d28f
commit 643895b
Showing
5 changed files
with
173 additions
and
21 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package rhp | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
|
||
rhp4 "go.sia.tech/core/rhp/v4" | ||
"go.sia.tech/core/types" | ||
rhp "go.sia.tech/coreutils/rhp/v4" | ||
) | ||
|
||
var ( | ||
// errDialTransport is returned when the worker could not dial the host. | ||
ErrDialTransport = errors.New("could not dial transport") | ||
) | ||
|
||
type ( | ||
Dialer interface { | ||
Dial(ctx context.Context, hk types.PublicKey, address string) (net.Conn, error) | ||
} | ||
) | ||
|
||
type Client struct { | ||
tpool *transportPool | ||
} | ||
|
||
func New(dialer Dialer) *Client { | ||
return &Client{ | ||
tpool: newTransportPool(dialer), | ||
} | ||
} | ||
|
||
func (c *Client) Settings(ctx context.Context, hk types.PublicKey, addr string) (hs rhp4.HostSettings, _ error) { | ||
err := c.tpool.withTransport(hk, addr, func(t *transport) error { | ||
client, err := t.Dial(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
hs, err = rhp.RPCSettings(ctx, client) | ||
return err | ||
}) | ||
return hs, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package rhp | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"go.sia.tech/core/types" | ||
rhp "go.sia.tech/coreutils/rhp/v4" | ||
) | ||
|
||
type transportPool struct { | ||
dialer Dialer | ||
|
||
mu sync.Mutex | ||
pool map[string]*transport | ||
} | ||
|
||
func newTransportPool(dialer Dialer) *transportPool { | ||
return &transportPool{ | ||
dialer: dialer, | ||
pool: make(map[string]*transport), | ||
} | ||
} | ||
|
||
func (p *transportPool) withTransport(hk types.PublicKey, addr string, fn func(*transport) error) (err error) { | ||
// fetch or create transport | ||
p.mu.Lock() | ||
t, found := p.pool[addr] | ||
if !found { | ||
t = &transport{ | ||
dialer: p.dialer, | ||
hk: hk, | ||
addr: addr, | ||
} | ||
p.pool[addr] = t | ||
} | ||
t.refCount++ | ||
p.mu.Unlock() | ||
|
||
// execute function | ||
err = func() (err error) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("panic (withTransport): %v", r) | ||
} | ||
}() | ||
return fn(t) | ||
}() | ||
|
||
// Decrement refcounter again and clean up pool. | ||
p.mu.Lock() | ||
t.refCount-- | ||
if t.refCount == 0 { | ||
// Cleanup | ||
if t.t != nil { | ||
_ = t.t.Close() | ||
t.t = nil | ||
} | ||
delete(p.pool, addr) | ||
} | ||
p.mu.Unlock() | ||
return err | ||
} | ||
|
||
// transportPool is a pool of rhpv4.TransportClients which allows for reusing | ||
// them. | ||
func dialTransport(ctx context.Context, dialer Dialer, addr string, hk types.PublicKey) (rhp.TransportClient, error) { | ||
// Dial host. | ||
conn, err := dialer.Dial(ctx, hk, addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Upgrade to rhpv4.Transport. | ||
return rhp.UpgradeConn(ctx, conn, hk) | ||
} | ||
|
||
type transport struct { | ||
dialer Dialer | ||
refCount uint64 // locked by pool | ||
|
||
mu sync.Mutex | ||
hk types.PublicKey | ||
addr string | ||
t rhp.TransportClient | ||
} | ||
|
||
// DialStream dials a new stream on the transport. | ||
func (t *transport) Dial(ctx context.Context) (rhp.TransportClient, error) { | ||
t.mu.Lock() | ||
defer t.mu.Unlock() | ||
if t.t == nil { | ||
start := time.Now() | ||
|
||
// dial host | ||
conn, err := t.dialer.Dial(ctx, t.hk, t.addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// upgrade conn | ||
newTransport, err := rhp.UpgradeConn(ctx, conn, t.hk) | ||
if err != nil { | ||
return nil, fmt.Errorf("UpgradeConn: %w: %w (%v)", ErrDialTransport, err, time.Since(start)) | ||
} | ||
t.t = newTransport | ||
} | ||
return t.t, nil | ||
} |
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