-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcall.go
More file actions
108 lines (93 loc) · 2.47 KB
/
call.go
File metadata and controls
108 lines (93 loc) · 2.47 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package gomeassistant
import (
"context"
"sync"
"saml.dev/gome-assistant/internal/services"
"saml.dev/gome-assistant/websocket"
)
// CallAndForget implements [services.API.CallAndForget].
func (app *App) CallAndForget(req services.BaseServiceRequest) error {
reqMsg := services.CallServiceMessage{
BaseMessage: websocket.BaseMessage{
Type: "call_service",
},
BaseServiceRequest: req,
}
return app.conn.Send(
func(lc websocket.LockedConn) error {
reqMsg.ID = lc.NextMessageID()
return lc.SendMessage(reqMsg)
},
)
}
// Call implements [services.API.Call].
func (app *App) Call(
ctx context.Context, req services.BaseServiceRequest, result any,
) error {
// Call works as follows:
// 1. Generate a message ID.
// 2. Subscribe to that ID.
// 3. Send a `CallServiceMessage` containing `req` over the websocket.
// 4. Wait for a single "result" message.
// 5. Unsubscribe from ID.
// 6. Unmarshal the "result" part of the response into `result`.
reqMsg := services.CallServiceMessage{
BaseMessage: websocket.BaseMessage{
Type: "call_service",
},
BaseServiceRequest: req,
}
// once ensures that exactly one of the following occurs:
// * a single response is handled and then the handler
// unsubscribes itself; or
// * (if `ctx` expires) the handler is unsubscribed if and only
// if no response has been handled.
var once sync.Once
// responseErr is set either to the error in the response message,
// or to `ctx.Err()`.
var responseErr error
// done is closed once a response has been processed.
done := make(chan struct{})
var subscription websocket.Subscription
unsubscribe := func() {
_ = app.conn.Send(func(lc websocket.LockedConn) error {
lc.Unsubscribe(subscription)
return nil
})
}
handleResponse := func(msg websocket.Message) {
once.Do(
func() {
responseErr = msg.GetResult(result)
unsubscribe()
close(done)
},
)
}
err := app.conn.Send(
func(lc websocket.LockedConn) error {
subscription = lc.Subscribe(handleResponse)
reqMsg.ID = subscription.MessageID()
return lc.SendMessage(reqMsg)
},
)
if err != nil {
return err
}
select {
case <-done:
// `handleResponse` has processed a response and set
// `responseErr`.
case <-ctx.Done():
// The context has expired. Unsubscribe and return
// `ctx.Err()`, but only if `handleResponse` hasn't just
// racily processed a response.
once.Do(
func() {
unsubscribe()
responseErr = ctx.Err()
},
)
}
return responseErr
}