-
Notifications
You must be signed in to change notification settings - Fork 68
/
calc.go
65 lines (54 loc) · 1.72 KB
/
calc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package calcapi
import (
"context"
"log"
"time"
calc "goa.design/examples/error/gen/calc"
)
// calc service example implementation.
// The example methods log the requests and return zero values.
type calcsrvc struct {
logger *log.Logger
}
// NewCalc returns the calc service implementation.
func NewCalc(logger *log.Logger) calc.Service {
return &calcsrvc{logger}
}
// Divide implements divide. It uses a convoluted implementation to illustrate returning timeout errors.
func (s *calcsrvc) Divide(ctx context.Context, p *calc.DividePayload) (res *calc.DivideResult, err error) {
// First make sure divisor is not 0, return DivByZero error if it is.
if p.Divisor == 0 {
return nil, &calc.DivByZero{Message: "divide by zero"}
}
// Set a timeout in the context.
ctx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
defer cancel()
// Use a channel to wait for either timeout or completion of division
c := make(chan *calc.DivideResult, 1)
// Call division operation in separate Go routine
go func() { c <- divide(p.Dividend, p.Divisor) }()
// Wait for results or timeout
select {
case <-ctx.Done():
// Timeout triggered, return timeout error
return nil, calc.MakeTimeout(ctx.Err())
case res := <-c:
// Division completed successfully
return res, nil
}
}
// Add implements add.
func (s *calcsrvc) Add(ctx context.Context, p *calc.AddPayload) (res int, err error) {
return p.A + p.B, nil
}
// divide implements the divide operation, it sleeps for 100 milliseconds if
// dividend is 42 to emulate timeouts.
func divide(dividend, divisor int) *calc.DivideResult {
if dividend == 42 {
time.Sleep(100 * time.Millisecond)
}
return &calc.DivideResult{
Quotient: dividend / divisor,
Reminder: dividend % divisor,
}
}