Skip to content

Commit 2c6d229

Browse files
committed
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.
1 parent 4835fdf commit 2c6d229

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

fn/req.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,27 @@ type Req[Input any, Output any] struct {
1111
// processing.
1212
Request Input
1313

14-
// Response is the channel on which we will receive the result of the
14+
// response is the channel on which we will receive the result of the
1515
// remote computation.
16-
Response chan<- Output
16+
response chan<- Output
17+
}
18+
19+
// Dispatch is a convenience method that lifts a function that transforms the
20+
// Input to the Output type into a full request handling cycle.
21+
func (r *Req[Input, Output]) Dispatch(handler func(Input) Output) {
22+
r.Resolve(handler(r.Request))
23+
}
24+
25+
// Resolve is a function that is used to send a value of the Output type back
26+
// to the requesting thread.
27+
func (r *Req[Input, Output]) Resolve(output Output) {
28+
select {
29+
case r.response <- output:
30+
default:
31+
// We do nothing here because the only situation in which this
32+
// case will fire is if the request handler attempts to resolve
33+
// a request more than once which is explicitly forbidden.
34+
}
1735
}
1836

1937
// 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) (
2947

3048
return Req[Input, Output]{
3149
Request: input,
32-
Response: responseChan,
50+
response: responseChan,
3351
}, responseChan
3452
}

0 commit comments

Comments
 (0)