(f TimeoutFunction)
| 23 | type WithContext func(context.Context, string) (string, error) |
| 24 | |
| 25 | func Timeout(f TimeoutFunction) WithContext { |
| 26 | return func(ctx context.Context, arg string) (string, error) { |
| 27 | ch := make(chan struct { |
| 28 | result string |
| 29 | err error |
| 30 | }, 1) |
| 31 | |
| 32 | go func() { |
| 33 | res, err := f(arg) |
| 34 | ch <- struct { |
| 35 | result string |
| 36 | err error |
| 37 | }{res, err} |
| 38 | }() |
| 39 | |
| 40 | select { |
| 41 | case res := <-ch: |
| 42 | return res.result, res.err |
| 43 | case <-ctx.Done(): |
| 44 | return "", ctx.Err() |
| 45 | } |
| 46 | } |
| 47 | } |