-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprotocol_services.go
More file actions
100 lines (90 loc) · 2.39 KB
/
Copy pathprotocol_services.go
File metadata and controls
100 lines (90 loc) · 2.39 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
package main
import (
"context"
"fmt"
"sync"
)
type protocolServices struct {
credentials credentialCodec
runtimeFields runtimeFieldGenerator
modelCache modelCacheDecryptor
contextFactory protocolContextFactory
closeFn func(context.Context) error
closeOnce sync.Once
closeErr error
}
func newProtocolServices(host protocolHostDeps) (*protocolServices, error) {
services, err := newNativeProtocolServices(host)
if err != nil {
return nil, err
}
if err := validateProtocolServices(services); err != nil {
return nil, err
}
return services, nil
}
func validateProtocolServices(services *protocolServices) error {
var missing string
switch {
case services == nil:
missing = "services"
case services.credentials == nil:
missing = "credentials"
case services.runtimeFields == nil:
missing = "runtime-fields"
case services.modelCache == nil:
missing = "model-cache"
case services.contextFactory == nil:
missing = "context-factory"
default:
return nil
}
return newProtocolError(
protocolBackendFailure,
"protocol services are incomplete",
fmt.Errorf("required protocol capability %s is unavailable", missing),
)
}
func newNativeProtocolServices(host protocolHostDeps) (*protocolServices, error) {
if host.Clock == nil || host.Entropy == nil {
return nil, newProtocolError(
protocolInvalidInput,
"native protocol host dependencies are invalid",
fmt.Errorf("native protocol services require clock and entropy dependencies"),
)
}
runtimeFields := &nativeRuntimeFieldGenerator{host: host}
return &protocolServices{
credentials: nativeCredentialCodec{},
runtimeFields: runtimeFields,
modelCache: nativeModelCacheDecryptor{},
contextFactory: &nativeContextFactory{
host: host,
runtimeFields: runtimeFields,
bodyCodec: nativeBodyCodec{},
prepare: prepareNativeInferRequest,
},
}, nil
}
type protocolServiceCloseError struct {
cause error
}
func (*protocolServiceCloseError) Error() string { return "close protocol service failed" }
func (e *protocolServiceCloseError) Unwrap() error { return e.cause }
func (s *protocolServices) Close(ctx context.Context) error {
if s == nil {
return nil
}
if ctx == nil {
ctx = context.Background()
}
s.closeOnce.Do(func() {
if s.closeFn == nil {
return
}
if err := s.closeFn(ctx); err != nil {
s.closeErr = &protocolServiceCloseError{cause: err}
}
})
return s.closeErr
}