• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hprose / hprose-golang / 4155962766

12 Feb 2023 10:53AM UTC coverage: 85.449% (-0.05%) from 85.495%
4155962766

push

github

小马哥
custom tags take precedence over default tags

2 of 2 new or added lines in 1 file covered. (100.0%)

9889 of 11573 relevant lines covered (85.45%)

22922.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.11
/rpc/plugins/timeout/timeout.go
1
/*--------------------------------------------------------*\
2
|                                                          |
3
|                          hprose                          |
4
|                                                          |
5
| Official WebSite: https://hprose.com                     |
6
|                                                          |
7
| rpc/plugins/timeout/timeout.go                           |
8
|                                                          |
9
| LastModified: Feb 21, 2021                               |
10
| Author: Ma Bingyao <andot@hprose.com>                    |
11
|                                                          |
12
\*________________________________________________________*/
13

14
package timeout
15

16
import (
17
        "context"
18
        "time"
19

20
        "github.com/hprose/hprose-golang/v3/rpc/core"
21
)
22

23
// ExecuteTimeout plugin for hprose.
24
type ExecuteTimeout struct {
25
        Timeout time.Duration
26
}
27

28
type returnValue struct {
29
        result []interface{}
30
        err    error
31
}
32

33
// Handler for ExecuteTimeout.
34
func (et *ExecuteTimeout) Handler(ctx context.Context, name string, args []interface{}, next core.NextInvokeHandler) (result []interface{}, err error) {
12✔
35
        timeout := et.Timeout
12✔
36
        serviceContext := core.GetServiceContext(ctx)
12✔
37
        if t, ok := serviceContext.Method.Options().Get("timeout"); ok {
12✔
38
                switch t := t.(type) {
×
39
                case time.Duration:
×
40
                        timeout = t
×
41
                case int:
×
42
                        timeout = time.Duration(t)
×
43
                case uint:
×
44
                        timeout = time.Duration(t)
×
45
                case int64:
×
46
                        timeout = time.Duration(t)
×
47
                case uint64:
×
48
                        timeout = time.Duration(t)
×
49
                }
50
        }
51
        if timeout <= 0 {
12✔
52
                return next(ctx, name, args)
×
53
        }
×
54
        var cancel context.CancelFunc
12✔
55
        ctx, cancel = context.WithTimeout(ctx, timeout)
12✔
56
        defer cancel()
12✔
57
        c := make(chan returnValue, 1)
12✔
58
        go func() {
24✔
59
                result, err := next(ctx, name, args)
12✔
60
                c <- returnValue{result, err}
12✔
61
        }()
12✔
62
        select {
12✔
63
        case <-ctx.Done():
6✔
64
                return nil, core.ErrTimeout
6✔
65
        case r := <-c:
6✔
66
                return r.result, r.err
6✔
67
        }
68
}
69

70
// New returns an ExecuteTimeout instance.
71
func New(timeout ...time.Duration) *ExecuteTimeout {
6✔
72
        if len(timeout) > 0 {
12✔
73
                return &ExecuteTimeout{timeout[0]}
6✔
74
        }
6✔
75
        return &ExecuteTimeout{time.Second * 30}
×
76
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc