Skip to content

Commit d333b0e

Browse files
committed
Improved godocs
1 parent eff7400 commit d333b0e

File tree

9 files changed

+69
-21
lines changed

9 files changed

+69
-21
lines changed

buffer.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,18 @@ type streamer struct {
1919
pipeW *io.PipeWriter
2020
}
2121

22-
// Streamer returns a reader/writer/closer that can be used to stream service responses. It does not necessarily
23-
// perform internal buffering, so users should take care not to depend on such behaviour.
22+
// Streamer returns a reader/writer/closer that can be used to stream responses. A simple use of this is:
23+
// func streamingService(req typhon.Request) typhon.Response {
24+
// body := typhon.Streamer()
25+
// go func() {
26+
// defer body.Close()
27+
// // do something to asynchronously produce output into body
28+
// }()
29+
// return req.Response(body)
30+
// }
31+
//
32+
// Note that a Streamer may not perform any internal buffering, so callers should take care not to depend on writes
33+
// being non-blocking. If buffering is needed, Streamer can be wrapped in a bufio.Writer.
2434
func Streamer() io.ReadWriteCloser {
2535
pipeR, pipeW := io.Pipe()
2636
return &streamer{

client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ func BareClient(req Request) Response {
7171
return HttpService(RoundTripper)(req)
7272
}
7373

74-
// SendVia sends the given request via the given service, returning a future representing the operation
74+
// SendVia round-trips the request via the passed Service. It does not block, instead returning a ResponseFuture
75+
// representing the asynchronous operation to produce the response.
7576
func SendVia(req Request, svc Service) *ResponseFuture {
7677
done := make(chan struct{}, 0)
7778
f := &ResponseFuture{
@@ -83,7 +84,10 @@ func SendVia(req Request, svc Service) *ResponseFuture {
8384
return f
8485
}
8586

86-
// Send is equivalent to SendVia(req, Client)
87+
// Send round-trips the request via the default Client. It does not block, instead returning a ResponseFuture
88+
// representing the asynchronous operation to produce the response. It is equivalent to:
89+
//
90+
// SendVia(req, Client)
8791
func Send(req Request) *ResponseFuture {
8892
return SendVia(req, Client)
8993
}

doc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*
2+
Package typhon wraps net/http to provide a simple yet powerful programming model for RPC servers and clients. At Monzo,
3+
it is used to implement the majority of our Go microservices.
4+
*/
5+
package typhon

terrors.go renamed to errors.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ func init() {
3333
}
3434
}
3535

36-
// ErrorStatusCode returns an HTTP status code for the error
36+
// ErrorStatusCode returns a HTTP status code for the given error.
37+
//
38+
// If the error is not a terror, this will always be 500 (Internal Server Error).
3739
func ErrorStatusCode(err error) int {
3840
code := terrors.Wrap(err, nil).(*terrors.Error).Code
3941
if c, ok := mapTerr2Status[strings.SplitN(code, ".", 2)[0]]; ok {
@@ -50,7 +52,8 @@ func status2TerrCode(code int) string {
5052
return terrors.ErrInternalService
5153
}
5254

53-
// ErrorFilter serialises and de-serialises response errors
55+
// ErrorFilter serialises and deserialises response errors. Without this filter, errors may not be passed across
56+
// the network properly so it is recommended to use this in most/all cases.
5457
func ErrorFilter(req Request, svc Service) Response {
5558
// If the request contains an error, short-circuit and return that directly
5659
var rsp Response

filter.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package typhon
22

3-
// Filter functions compose with Services to modify their observed behaviour. They might change a service's input or
4-
// output, or elect not to call the underlying service at all.
3+
// Filter functions compose with Services to modify their behaviour. They might change a service's input or output, or
4+
// elect not to call the underlying service at all.
5+
//
6+
// These are typically useful to encapsulate common logic that is shared among multiple Services. Authentication,
7+
// authorisation, rate limiting, and tracing are good examples.
58
type Filter func(Request, Service) Response

http.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ func copyErrSeverity(err error) slog.Severity {
7777
}
7878
}
7979

80-
// HttpHandler transforms the given Service into a http.Handler, suitable for use directly with net/http
80+
// HttpHandler transforms the given Service into a standard library HTTP handler. It is one of the main "bridges"
81+
// between Typhon and net/http.
8182
func HttpHandler(svc Service) http.Handler {
8283
return http.HandlerFunc(func(rw http.ResponseWriter, httpReq *http.Request) {
8384
if httpReq.Body != nil {

request.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func (r Request) Decode(v interface{}) error {
7272
return terrors.WrapWithCode(err, nil, terrors.ErrBadRequest)
7373
}
7474

75+
// Write writes the passed bytes to the request's body.
7576
func (r *Request) Write(b []byte) (int, error) {
7677
switch rc := r.Body.(type) {
7778
// In the "normal" case, the response body will be a buffer, to which we can write
@@ -94,8 +95,10 @@ func (r *Request) Write(b []byte) (int, error) {
9495
}
9596
}
9697

97-
// BodyBytes fully reads the request body and returns the bytes read. If consume is false, the body is copied into a
98-
// new buffer such that it may be read again.
98+
// BodyBytes fully reads the request body and returns the bytes read.
99+
//
100+
// If consume is true, this is equivalent to ioutil.ReadAll; if false, the caller will observe the body to be in
101+
// the same state that it was before (ie. any remaining unread body can be read again).
99102
func (r *Request) BodyBytes(consume bool) ([]byte, error) {
100103
if consume {
101104
defer r.Body.Close()
@@ -115,10 +118,16 @@ func (r *Request) BodyBytes(consume bool) ([]byte, error) {
115118
}
116119
}
117120

121+
// Send round-trips the request via the default Client. It does not block, instead returning a ResponseFuture
122+
// representing the asynchronous operation to produce the response. It is equivalent to:
123+
//
124+
// r.SendVia(Client)
118125
func (r Request) Send() *ResponseFuture {
119126
return Send(r)
120127
}
121128

129+
// SendVia round-trips the request via the passed Service. It does not block, instead returning a ResponseFuture
130+
// representing the asynchronous operation to produce the response.
122131
func (r Request) SendVia(svc Service) *ResponseFuture {
123132
return SendVia(r, svc)
124133
}

response.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func (r *Response) Decode(v interface{}) error {
7171
return err
7272
}
7373

74+
// Write writes the passed bytes to the response's body.
7475
func (r *Response) Write(b []byte) (int, error) {
7576
if r.Response == nil {
7677
r.Response = newHTTPResponse(Request{})
@@ -118,7 +119,10 @@ func (r *Response) BodyBytes(consume bool) ([]byte, error) {
118119
}
119120
}
120121

121-
// Writer returns a ResponseWriter proxy.
122+
// Writer returns a ResponseWriter which can be used to populate the response.
123+
//
124+
// This is useful when you want to use another HTTP library that is used to wrapping net/http directly. For example,
125+
// it allows a Typhon Service to use a http.Handler internally.
122126
func (r *Response) Writer() ResponseWriter {
123127
if r.Request != nil && r.Request.hijacker != nil {
124128
return hijackerRw{

router.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,47 +127,56 @@ func (r Router) Params(req Request) map[string]string {
127127

128128
// Sugar
129129

130-
// GET is shorthand for Register("GET", pattern, svc).
130+
// GET is shorthand for:
131+
// r.Register("GET", pattern, svc)
131132
//
132133
// Pattern syntax is as described in echo's documentation: https://echo.labstack.com/guide/routing
133134
func (r *Router) GET(pattern string, svc Service) { r.Register("GET", pattern, svc) }
134135

135-
// CONNECT is shorthand for Register("CONNECT", pattern, svc).
136+
// CONNECT is shorthand for:
137+
// r.Register("CONNECT", pattern, svc)
136138
//
137139
// Pattern syntax is as described in echo's documentation: https://echo.labstack.com/guide/routing
138140
func (r *Router) CONNECT(pattern string, svc Service) { r.Register("CONNECT", pattern, svc) }
139141

140-
// DELETE is shorthand for Register("DELETE", pattern, svc).
142+
// DELETE is shorthand for:
143+
// r.Register("DELETE", pattern, svc)
141144
//
142145
// Pattern syntax is as described in echo's documentation: https://echo.labstack.com/guide/routing
143146
func (r *Router) DELETE(pattern string, svc Service) { r.Register("DELETE", pattern, svc) }
144147

145-
// HEAD is shorthand for Register("HEAD", pattern, svc).
148+
// HEAD is shorthand for:
149+
// r.Register("HEAD", pattern, svc)
146150
//
147151
// Pattern syntax is as described in echo's documentation: https://echo.labstack.com/guide/routing
148152
func (r *Router) HEAD(pattern string, svc Service) { r.Register("HEAD", pattern, svc) }
149153

150-
// OPTIONS is shorthand for Register("OPTIONS", pattern, svc).
154+
// OPTIONS is shorthand for:
155+
// r.Register("OPTIONS", pattern, svc)
151156
//
152157
// Pattern syntax is as described in echo's documentation: https://echo.labstack.com/guide/routing
153158
func (r *Router) OPTIONS(pattern string, svc Service) { r.Register("OPTIONS", pattern, svc) }
154159

155-
// PATCH is shorthand for Register("PATCH", pattern, svc).
160+
// PATCH is shorthand for:
161+
// r.Register("PATCH", pattern, svc)
156162
//
157163
// Pattern syntax is as described in echo's documentation: https://echo.labstack.com/guide/routing
158164
func (r *Router) PATCH(pattern string, svc Service) { r.Register("PATCH", pattern, svc) }
159165

160-
// POST is shorthand for Register("POST", pattern, svc).
166+
// POST is shorthand for:
167+
// r.Register("POST", pattern, svc)
161168
//
162169
// Pattern syntax is as described in echo's documentation: https://echo.labstack.com/guide/routing
163170
func (r *Router) POST(pattern string, svc Service) { r.Register("POST", pattern, svc) }
164171

165-
// PUT is shorthand for Register("PUT", pattern, svc).
172+
// PUT is shorthand for:
173+
// r.Register("PUT", pattern, svc)
166174
//
167175
// Pattern syntax is as described in echo's documentation: https://echo.labstack.com/guide/routing
168176
func (r *Router) PUT(pattern string, svc Service) { r.Register("PUT", pattern, svc) }
169177

170-
// TRACE is shorthand for Register("TRACE", pattern, svc).
178+
// TRACE is shorthand for:
179+
// r.Register("TRACE", pattern, svc)
171180
//
172181
// Pattern syntax is as described in echo's documentation: https://echo.labstack.com/guide/routing
173182
func (r *Router) TRACE(pattern string, svc Service) { r.Register("TRACE", pattern, svc) }

0 commit comments

Comments
 (0)