Skip to content

Commit 1ab0337

Browse files
committed
Remove context support in favor of using a read only channel
1 parent 74a384b commit 1ab0337

File tree

6 files changed

+42
-19
lines changed

6 files changed

+42
-19
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,37 @@ Getting Started
2626

2727
See the [getting started](http://kusanagi.io/docs/getting-started) tutorial to begin with the **KUSANAGI**™ framework and the **Go** SDK.
2828

29+
### Cancellation Signal
30+
31+
The Go SDK implements support to signal deadlines or cancellation through a read only channel that is available to **Middleware** and **Service** components.
32+
33+
The channel can be read using the `Api.Done()` method. For example, within a service action:
34+
35+
```go
36+
func handler(action *kusanagi.Action) (*kusanagi.Action, error) {
37+
// Create a context for the current service call
38+
ctx, cancel := context.WithCancel(context.Background())
39+
40+
// Create a channel to get the task result
41+
result := make(chan int)
42+
43+
// Run some async task
44+
go task(ctx, result)
45+
46+
// Cancel the context when the service call times out
47+
select{
48+
case v := <-result:
49+
action.Log(v, 6)
50+
case <-action.Done():
51+
cancel()
52+
}
53+
54+
return action, nil
55+
}
56+
```
57+
58+
It is highly recommended to monitor this channel and stop any ongoing task when the channel is closed.
59+
2960
Documentation
3061
-------------
3162

v2/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ func (a *Action) Call(
548548
// Make the runtime call
549549
callee := []string{service, version, action}
550550
c, err := call(
551-
a.GetContext(),
551+
a.Done(),
552552
schema.GetAddress(),
553553
a.GetActionName(),
554554
callee,

v2/api.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package kusanagi
1010

1111
import (
12-
"context"
1312
"path"
1413

1514
"github.com/kusanagi/kusanagi-sdk-go/v2/lib/cli"
@@ -140,12 +139,7 @@ func (a *Api) Log(value interface{}, level int) (*Api, error) {
140139
return a, nil
141140
}
142141

143-
// GetContext return the context for the current request.
144-
func (a *Api) GetContext() context.Context {
145-
return a.state.context
146-
}
147-
148-
// Done is a dummy method to comply with KUSANAGI SDK specifications.
149-
func (a *Api) Done() bool {
150-
panic("SDK does not support async call to end action: Api.done()")
142+
// Done returns a channel that signals the deadline or cancellation of the call.
143+
func (a *Api) Done() <-chan struct{} {
144+
return a.state.done
151145
}

v2/lib/runtime/runtime.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package runtime
1010

1111
import (
12-
"context"
1312
"fmt"
1413
"time"
1514

@@ -20,7 +19,7 @@ import (
2019
)
2120

2221
// Call makes a runtime call to a service.
23-
func Call(ctx context.Context, address string, message []byte, timeout uint) (*payload.Reply, time.Duration, error) {
22+
func Call(stop <-chan struct{}, address string, message []byte, timeout uint) (*payload.Reply, time.Duration, error) {
2423
var duration time.Duration
2524

2625
// Define a custom ZMQ context
@@ -36,7 +35,7 @@ func Call(ctx context.Context, address string, message []byte, timeout uint) (*p
3635
// When the context is done terminate the ZMQ context to stop the runtime call
3736
go func() {
3837
select {
39-
case <-ctx.Done():
38+
case <-stop:
4039
if err := zctx.Term(); err != nil {
4140
log.Errorf("Failed to terminate runtime call context: %v", err)
4241
}

v2/runtime.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package kusanagi
1010

1111
import (
12-
"context"
1312
"errors"
1413
"fmt"
1514
"time"
@@ -28,7 +27,7 @@ type callResult struct {
2827
}
2928

3029
func call(
31-
ctx context.Context,
30+
stop <-chan struct{},
3231
address string,
3332
action string,
3433
callee []string,
@@ -66,7 +65,7 @@ func call(
6665
// NOTE: Run-time calls are made to the server address where the caller is runnning
6766
// and NOT directly to the service we wish to call. The KUSANAGI framework
6867
// takes care of the call logic for us to keep consistency between all the SDKs.
69-
reply, duration, err := runtime.Call(ctx, protocol.SocketAddress(address, tcp), message, timeout)
68+
reply, duration, err := runtime.Call(stop, protocol.SocketAddress(address, tcp), message, timeout)
7069
if err != nil {
7170
c <- callResult{Duration: duration, Error: err}
7271
} else if err := reply.Error; err != nil {

v2/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type state struct {
3333
reply *payload.Reply
3434
payload []byte
3535
input cli.Input
36-
context context.Context
36+
done chan struct{}
3737
logger log.RequestLogger
3838
request requestMsg
3939
}
@@ -194,9 +194,11 @@ func (s *server) startMessageListener(msgc <-chan requestMsg) <-chan requestOutp
194194
action: action,
195195
schemas: schemas,
196196
input: s.input,
197+
done: make(chan struct{}),
197198
logger: logger,
198199
request: msg,
199200
}
201+
defer close(state.done)
200202

201203
// Prepare defaults for the request output
202204
output := requestOutput{state: &state}
@@ -206,7 +208,6 @@ func (s *server) startMessageListener(msgc <-chan requestMsg) <-chan requestOutp
206208
output.err = fmt.Errorf(`Invalid action for component %s: "%s"`, title, action)
207209
resc <- output
208210
return
209-
210211
}
211212

212213
// Try to read the new schemas when present
@@ -227,7 +228,6 @@ func (s *server) startMessageListener(msgc <-chan requestMsg) <-chan requestOutp
227228
// Create a child context with the process execution timeout as limit
228229
ctx, cancel := context.WithTimeout(ctx, timeout)
229230
defer cancel()
230-
state.context = ctx
231231

232232
// Create a channel to wait for the processor output
233233
outc := make(chan requestOutput)

0 commit comments

Comments
 (0)