| 1 |
package calls |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
|
| 9 |
"github.com/mesos/mesos-go/api/v1/lib" |
| 10 |
"github.com/mesos/mesos-go/api/v1/lib/encoding" |
| 11 |
|
| 12 |
"github.com/mesos/mesos-go/api/v1/lib/agent" |
| 13 |
) |
| 14 |
|
| 15 |
type ( |
| 16 |
|
| 17 |
|
| 18 |
Request interface { |
| 19 |
Call() *agent.Call |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
RequestFunc func() *agent.Call |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
RequestStreaming interface { |
| 29 |
Request |
| 30 |
IsStreaming() |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
RequestStreamingFunc func() *agent.Call |
| 35 |
|
| 36 |
|
| 37 |
Sender interface { |
| 38 |
Send(context.Context, Request) (mesos.Response, error) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
SenderFunc func(context.Context, Request) (mesos.Response, error) |
| 43 |
) |
| 44 |
|
| 45 |
func (f RequestFunc) Call() *agent.Call { return f() } |
| 46 |
|
| 47 |
func (f RequestFunc) Marshaler() encoding.Marshaler { |
| 48 |
|
| 49 |
if call := f(); call != nil { |
| 50 |
return call |
| 51 |
} |
| 52 |
return nil |
| 53 |
} |
| 54 |
|
| 55 |
func (f RequestStreamingFunc) Push(c ...*agent.Call) RequestStreamingFunc { return Push(f, c...) } |
| 56 |
|
| 57 |
func (f RequestStreamingFunc) Marshaler() encoding.Marshaler { |
| 58 |
|
| 59 |
if call := f(); call != nil { |
| 60 |
return call |
| 61 |
} |
| 62 |
return nil |
| 63 |
} |
| 64 |
|
| 65 |
func (f RequestStreamingFunc) IsStreaming() {} |
| 66 |
|
| 67 |
func (f RequestStreamingFunc) Call() *agent.Call { return f() } |
| 68 |
|
| 69 |
|
| 70 |
func Push(r RequestStreaming, c ...*agent.Call) RequestStreamingFunc { |
| 71 |
return func() *agent.Call { |
| 72 |
if len(c) == 0 { |
| 73 |
return r.Call() |
| 74 |
} |
| 75 |
head := c[0] |
| 76 |
c = c[1:] |
| 77 |
return head |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
func Empty() RequestStreamingFunc { return func() *agent.Call { return nil } } |
| 83 |
|
| 84 |
var ( |
| 85 |
_ = Request(RequestFunc(nil)) |
| 86 |
_ = RequestStreaming(RequestStreamingFunc(nil)) |
| 87 |
_ = Sender(SenderFunc(nil)) |
| 88 |
) |
| 89 |
|
| 90 |
|
| 91 |
func NonStreaming(c *agent.Call) RequestFunc { return func() *agent.Call { return c } } |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
func FromChan(ch <-chan *agent.Call) RequestStreamingFunc { |
| 96 |
if ch == nil { |
| 97 |
|
| 98 |
return func() *agent.Call { return nil } |
| 99 |
} |
| 100 |
return func() *agent.Call { |
| 101 |
if m, ok := <-ch; ok { |
| 102 |
return m |
| 103 |
} |
| 104 |
return nil |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func (f SenderFunc) Send(ctx context.Context, r Request) (mesos.Response, error) { |
| 110 |
return f(ctx, r) |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
func IgnoreResponse(s Sender) SenderFunc { |
| 115 |
return func(ctx context.Context, r Request) (mesos.Response, error) { |
| 116 |
resp, err := s.Send(ctx, r) |
| 117 |
if resp != nil { |
| 118 |
resp.Close() |
| 119 |
} |
| 120 |
return nil, err |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
func SendNoData(ctx context.Context, sender Sender, r Request) (err error) { |
| 127 |
_, err = IgnoreResponse(sender).Send(ctx, r) |
| 128 |
return |
| 129 |
} |