mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-20 21:30:06 +02:00
fn: harden and refine the Req[I,O] API.
In this commit we opt to make the internal response channel fully private and instead expose methods for doing resolution. This prevents internal implementation details from leaking a little bit better than the previous iteration.
This commit is contained in:
24
fn/req.go
24
fn/req.go
@@ -11,9 +11,27 @@ type Req[Input any, Output any] struct {
|
|||||||
// processing.
|
// processing.
|
||||||
Request Input
|
Request Input
|
||||||
|
|
||||||
// Response is the channel on which we will receive the result of the
|
// response is the channel on which we will receive the result of the
|
||||||
// remote computation.
|
// remote computation.
|
||||||
Response chan<- Output
|
response chan<- Output
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dispatch is a convenience method that lifts a function that transforms the
|
||||||
|
// Input to the Output type into a full request handling cycle.
|
||||||
|
func (r *Req[Input, Output]) Dispatch(handler func(Input) Output) {
|
||||||
|
r.Resolve(handler(r.Request))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve is a function that is used to send a value of the Output type back
|
||||||
|
// to the requesting thread.
|
||||||
|
func (r *Req[Input, Output]) Resolve(output Output) {
|
||||||
|
select {
|
||||||
|
case r.response <- output:
|
||||||
|
default:
|
||||||
|
// We do nothing here because the only situation in which this
|
||||||
|
// case will fire is if the request handler attempts to resolve
|
||||||
|
// a request more than once which is explicitly forbidden.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewReq is the base constructor of the Req type. It returns both the packaged
|
// NewReq is the base constructor of the Req type. It returns both the packaged
|
||||||
@@ -29,6 +47,6 @@ func NewReq[Input, Output any](input Input) (
|
|||||||
|
|
||||||
return Req[Input, Output]{
|
return Req[Input, Output]{
|
||||||
Request: input,
|
Request: input,
|
||||||
Response: responseChan,
|
response: responseChan,
|
||||||
}, responseChan
|
}, responseChan
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user