-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathengine.go
More file actions
391 lines (349 loc) · 12.4 KB
/
engine.go
File metadata and controls
391 lines (349 loc) · 12.4 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright The Pit Project Owners. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Please see https://github.com/openpitkit and the OWNERS file for details.
// Package pit exposes the Go binding for the OpenPit engine.
//
// Threading:
// The SDK never spawns OS threads: each public method call runs on the OS
// thread that invoked it. Concurrent invocation of public methods on the same
// engine handle is undefined behavior and must be prevented by the caller.
// Sequential calls on the same handle from different OS threads are supported.
// Goroutine migration between OS threads during one SDK call is supported.
// Callbacks invoked by the SDK back into Go may run on a different OS thread
// than the goroutine that initiated the call, so callback code must not rely
// on thread-local OS state.
package openpit
import (
"errors"
"fmt"
"runtime"
"go.openpit.dev/openpit/accountadjustment"
"go.openpit.dev/openpit/internal/custompolicy"
"go.openpit.dev/openpit/internal/loader"
"go.openpit.dev/openpit/internal/native"
"go.openpit.dev/openpit/model"
"go.openpit.dev/openpit/param"
"go.openpit.dev/openpit/pkg/optional"
"go.openpit.dev/openpit/pretrade"
"go.openpit.dev/openpit/reject"
)
//------------------------------------------------------------------------------
// Engine
type Engine struct{ handle native.Engine }
func newEngineFromHandle(handle native.Engine) *Engine {
return &Engine{handle: handle}
}
// Stop signals the engine to halt internal evaluation, releases policies
// registered on the engine, and frees the underlying native resources.
//
// After Stop returns, the engine handle is no longer valid for any operation.
// The engine must no longer be passed to any other
// method (StartPreTrade, ExecutePreTrade, ApplyExecutionReport,
// ApplyAccountAdjustment); doing so is undefined behavior.
//
// Idempotency: safe to call more than once; subsequent calls are no-ops.
//
// Outstanding objects previously produced by this engine
// (pretrade.Request, pretrade.Reservation) remain owned by the caller and
// must be released independently.
func (e *Engine) Stop() {
native.DestroyEngine(e.handle)
e.handle = nil
}
// StartPreTrade runs the start stage of the pre-trade pipeline.
//
// Return contract:
// - on accept, returns a non-nil *pretrade.Request; the caller takes
// ownership and must release it with Request.Close when done (Execute
// does not close the request — see Request.Execute);
// - on reject, returns a non-nil []reject.Reject; no Request is produced;
// - on transport error, returns a Go error; no Request is produced.
func (e *Engine) StartPreTrade(order model.Order) (*pretrade.Request, []reject.Reject, error) {
request, startReject, err := native.EngineStartPreTrade(e.handle, order.Handle())
runtime.KeepAlive(order)
if err != nil {
return nil, nil, err
}
if startReject != nil {
rejectResult, err := reject.NewListFromHandle(startReject)
native.DestroyRejectList(startReject)
if err != nil {
return nil,
nil,
fmt.Errorf("failed to create reject list for rejected pre-trade start: %w", err)
}
return nil, rejectResult, nil
}
return pretrade.NewRequestFromHandle(request), nil, nil
}
// ExecutePreTrade runs the full pre-trade pipeline and, on accept, returns
// a reservation representing the reserved but not yet finalized state.
//
// Return contract:
// - on accept, returns a non-nil *pretrade.Reservation; the caller takes
// ownership and must resolve it exactly once via CommitAndClose,
// RollbackAndClose, or Close (which rolls back any pending mutations
// implicitly);
// - on reject, returns a non-nil []reject.Reject; no Reservation is produced;
// - on transport error, returns a Go error; no Reservation is produced.
func (e *Engine) ExecutePreTrade(order model.Order) (*pretrade.Reservation, []reject.Reject, error) {
reservation, execRejects, err := native.EngineExecutePreTrade(e.handle, order.Handle())
runtime.KeepAlive(order)
if err != nil {
return nil, nil, err
}
if execRejects != nil {
rejectResult, err := reject.NewListFromHandle(execRejects)
native.DestroyRejectList(execRejects)
if err != nil {
return nil,
nil,
fmt.Errorf("failed to create reject list for rejected order: %w", err)
}
return nil, rejectResult, nil
}
return pretrade.NewReservationFromHandle(reservation), nil, nil
}
type PostTradeResult struct {
KillSwitchTriggered bool
}
func (e *Engine) ApplyExecutionReport(report model.ExecutionReport) (PostTradeResult, error) {
result, err := native.EngineApplyExecutionReport(e.handle, report.Handle())
runtime.KeepAlive(report)
if err != nil {
return PostTradeResult{}, err
}
return PostTradeResult{
KillSwitchTriggered: result.KillSwitchTriggered,
}, nil
}
func (e *Engine) ApplyAccountAdjustment(
accountID param.AccountID,
adjustments []model.AccountAdjustment,
) (optional.Option[reject.AccountAdjustmentBatchError], error) {
nativeAdjustments := make([]native.AccountAdjustment, len(adjustments))
for i, adjustment := range adjustments {
nativeAdjustments[i] = adjustment.Handle()
}
adjustmentReject, err := native.EngineApplyAccountAdjustment(
e.handle,
accountID.Handle(),
nativeAdjustments,
)
runtime.KeepAlive(adjustments)
if err != nil {
return optional.None[reject.AccountAdjustmentBatchError](), err
}
if adjustmentReject != nil {
rejectResult, err := reject.NewAccountAdjustmentBatchErrorFromHandle(adjustmentReject)
native.DestroyAccountAdjustmentBatchError(adjustmentReject)
if err != nil {
return optional.None[reject.AccountAdjustmentBatchError](),
fmt.Errorf("failed to create reject list for rejected account adjustment: %w", err)
}
return optional.Some(rejectResult), nil
}
return optional.None[reject.AccountAdjustmentBatchError](), nil
}
//------------------------------------------------------------------------------
// EngineBuilder
type EngineBuilder struct {
handle native.EngineBuilder
err error
// Policies that were accepted by the builder but never handed off to the
// engine. The builder must close them on Close to release their resources.
unfinished []interface{ Close() }
}
// NewEngineBuilder returns a new engine builder.
// The returned builder must be released by calling either Close or Build
// after use.
func NewEngineBuilder() (*EngineBuilder, error) {
if err := loader.EnsureRuntimeLoaded(); err != nil {
return nil, err
}
return &EngineBuilder{handle: native.CreateEngineBuilder()}, nil
}
// Close releases the builder and any policies that were handed to it but
// never transferred to the engine. Safe to call more than once and safe to
// call after Build; subsequent calls are no-ops.
func (b *EngineBuilder) Close() {
{
for _, entity := range b.unfinished {
entity.Close()
}
b.unfinished = nil
}
if b.handle != nil {
native.DestroyEngineBuilder(b.handle)
b.handle = nil
}
}
// Build constructs the engine and releases the builder. The builder is
// closed on both success and failure, so an explicit Close afterwards is a
// no-op. On failure, any policies that were accepted by the builder but not
// transferred to the engine are closed by the builder. On success, ownership
// of the returned engine passes to the caller, who must release it by
// calling Stop. Behavior is undefined if Build is called more than once on
// the same builder.
func (b *EngineBuilder) Build() (*Engine, error) {
defer b.Close()
if b.err != nil {
return nil, b.err
}
handle, err := native.EngineBuilderBuild(b.handle)
if err != nil {
return nil, err
}
return newEngineFromHandle(handle), nil
}
func (b *EngineBuilder) CheckPreTradeStartPolicy(
policy ...pretrade.CheckStartPolicy,
) *EngineBuilder {
for _, p := range policy {
// Every policy must go through addPolicy even after a previous failure
// so that the builder takes responsibility for releasing it.
b.addCheckPreTradeStartPolicy(p)
}
return b
}
func (b *EngineBuilder) BuiltinCheckPreTradeStartPolicy(
policy ...pretrade.BuiltinPolicy,
) *EngineBuilder {
for _, p := range policy {
b.addBuiltinCheckPreTradeStartPolicy(p)
}
return b
}
func (b *EngineBuilder) PreTradePolicy(policy ...pretrade.Policy) *EngineBuilder {
for _, p := range policy {
// Every policy must go through addPolicy even after a previous failure
// so that the builder takes responsibility for releasing it.
b.addPreTradePolicy(p)
}
return b
}
func (b *EngineBuilder) AccountAdjustmentPolicy(policy ...accountadjustment.Policy) *EngineBuilder {
for _, p := range policy {
// Every policy must go through addPolicy even after a previous failure
// so that the builder takes responsibility for releasing it.
b.addAccountAdjustmentPolicy(p)
}
return b
}
func (b *EngineBuilder) addCheckPreTradeStartPolicy(policy pretrade.CheckStartPolicy) {
addPolicy(
b,
policy,
custompolicy.StartCheckPreTradeStart,
native.DestroyPretradeCheckPreTradeStartPolicy,
native.EngineBuilderAddCheckPreTradeStartPolicy,
)
}
func (b *EngineBuilder) addBuiltinCheckPreTradeStartPolicy(policy pretrade.BuiltinPolicy) {
addPolicy(
b,
policy,
nil,
native.DestroyPretradeCheckPreTradeStartPolicy,
native.EngineBuilderAddCheckPreTradeStartPolicy,
)
}
func (b *EngineBuilder) addPreTradePolicy(policy pretrade.Policy) {
addPolicy(
b,
policy,
custompolicy.StartPreTrade,
native.DestroyPretradePreTradePolicy,
native.EngineBuilderAddPreTradePolicy,
)
}
func (b *EngineBuilder) addAccountAdjustmentPolicy(policy accountadjustment.Policy) {
addPolicy(
b,
policy,
custompolicy.StartAccountAdjustment,
native.DestroyAccountAdjustmentPolicy,
native.EngineBuilderAddAccountAdjustmentPolicy,
)
}
func addPolicy[
Policy interface {
Name() string
Close()
},
Handle any,
](
builder *EngineBuilder,
policy Policy,
startCustomPolicy func(Policy) (Handle, error),
destroyPolicyHandle func(Handle),
add func(native.EngineBuilder, Handle) error,
) {
if builder.err != nil {
builder.scheduleClose(policy)
return
}
var handle Handle
if builtinPolicy, isBuiltin := any(policy).(builtinPolicyWithNative[Handle]); isBuiltin {
// Ownership of the native handle is transferred out of the built-in
// wrapper; after this point a later Close on the wrapper is a no-op.
handle = builtinPolicy.TakeHandle()
} else {
if startCustomPolicy == nil {
builder.err = newEngineBuilderPolicyAddError(
errors.New("policy is not a recognized built-in"),
policy.Name(),
)
builder.scheduleClose(policy)
return
}
var err error
if handle, err = startCustomPolicy(policy); err != nil {
builder.err = newEngineBuilderPolicyAddError(err, policy.Name())
builder.scheduleClose(policy)
return
}
}
// The caller-owned reference must always be released. On success, the
// engine keeps its own reference and will drive the eventual destruction
// on Stop. On failure, dropping this last reference destroys the policy
// immediately and, for custom policies, triggers free_user_data, which in
// turn closes the user-provided implementation.
defer destroyPolicyHandle(handle)
if err := add(builder.handle, handle); err != nil {
// No scheduleClose is needed here: the deferred release above drops
// the last reference to the policy and the native Drop path takes
// care of closing the user implementation via free_user_data.
builder.err = newEngineBuilderPolicyAddError(err, policy.Name())
}
}
func (b *EngineBuilder) scheduleClose(entity interface{ Close() }) {
b.unfinished = append(b.unfinished, entity)
}
type engineBuilderPolicyAddError struct {
err error
policyName string
}
func newEngineBuilderPolicyAddError(err error, policyName string) engineBuilderPolicyAddError {
return engineBuilderPolicyAddError{err: err, policyName: policyName}
}
func (e engineBuilderPolicyAddError) Error() string {
return fmt.Sprintf("failed to add policy %q: %v", e.policyName, e.err)
}
type builtinPolicyWithNative[Handle any] interface {
TakeHandle() Handle
}
//------------------------------------------------------------------------------