-
Notifications
You must be signed in to change notification settings - Fork 249
/
server.go
141 lines (131 loc) · 4.61 KB
/
server.go
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
package testserver
import (
"context"
"time"
"github.com/authzed/spicedb/internal/middleware/servicespecific"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"github.com/authzed/spicedb/internal/datastore/memdb"
"github.com/authzed/spicedb/internal/dispatch/graph"
"github.com/authzed/spicedb/internal/middleware/consistency"
datastoremw "github.com/authzed/spicedb/internal/middleware/datastore"
"github.com/authzed/spicedb/pkg/cmd/server"
"github.com/authzed/spicedb/pkg/cmd/util"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/middleware/logging"
)
// ServerConfig is configuration for the test server.
type ServerConfig struct {
MaxUpdatesPerWrite uint16
MaxPreconditionsCount uint16
MaxRelationshipContextSize int
StreamingAPITimeout time.Duration
}
// NewTestServer creates a new test server, using defaults for the config.
func NewTestServer(require *require.Assertions,
revisionQuantization time.Duration,
gcWindow time.Duration,
schemaPrefixRequired bool,
dsInitFunc func(datastore.Datastore, *require.Assertions) (datastore.Datastore, datastore.Revision),
) (*grpc.ClientConn, func(), datastore.Datastore, datastore.Revision) {
return NewTestServerWithConfig(require, revisionQuantization, gcWindow, schemaPrefixRequired,
ServerConfig{
MaxUpdatesPerWrite: 1000,
MaxPreconditionsCount: 1000,
StreamingAPITimeout: 30 * time.Second,
MaxRelationshipContextSize: 25000,
},
dsInitFunc)
}
// NewTestServerWithConfig creates as new test server with the specified config.
func NewTestServerWithConfig(require *require.Assertions,
revisionQuantization time.Duration,
gcWindow time.Duration,
schemaPrefixRequired bool,
config ServerConfig,
dsInitFunc func(datastore.Datastore, *require.Assertions) (datastore.Datastore, datastore.Revision),
) (*grpc.ClientConn, func(), datastore.Datastore, datastore.Revision) {
emptyDS, err := memdb.NewMemdbDatastore(0, revisionQuantization, gcWindow)
require.NoError(err)
ds, revision := dsInitFunc(emptyDS, require)
ctx, cancel := context.WithCancel(context.Background())
srv, err := server.NewConfigWithOptions(
server.WithDatastore(ds),
server.WithDispatcher(graph.NewLocalOnlyDispatcher(10)),
server.WithDispatchMaxDepth(50),
server.WithMaximumPreconditionCount(config.MaxPreconditionsCount),
server.WithMaximumUpdatesPerWrite(config.MaxUpdatesPerWrite),
server.WithStreamingAPITimeout(config.StreamingAPITimeout),
server.WithMaxCaveatContextSize(4096),
server.WithMaxRelationshipContextSize(config.MaxRelationshipContextSize),
server.WithGRPCServer(util.GRPCServerConfig{
Network: util.BufferedNetwork,
Enabled: true,
}),
server.WithSchemaPrefixesRequired(schemaPrefixRequired),
server.WithGRPCAuthFunc(func(ctx context.Context) (context.Context, error) {
return ctx, nil
}),
server.WithHTTPGateway(util.HTTPServerConfig{HTTPEnabled: false}),
server.WithMetricsAPI(util.HTTPServerConfig{HTTPEnabled: false}),
server.WithDispatchServer(util.GRPCServerConfig{Enabled: false}),
server.SetUnaryMiddlewareModification([]server.MiddlewareModification[grpc.UnaryServerInterceptor]{
{
Operation: server.OperationReplaceAllUnsafe,
Middlewares: []server.ReferenceableMiddleware[grpc.UnaryServerInterceptor]{
{
Name: "logging",
Middleware: logging.UnaryServerInterceptor(),
},
{
Name: "datastore",
Middleware: datastoremw.UnaryServerInterceptor(ds),
},
{
Name: "consistency",
Middleware: consistency.UnaryServerInterceptor(),
},
{
Name: "servicespecific",
Middleware: servicespecific.UnaryServerInterceptor,
},
},
},
}),
server.SetStreamingMiddlewareModification([]server.MiddlewareModification[grpc.StreamServerInterceptor]{
{
Operation: server.OperationReplaceAllUnsafe,
Middlewares: []server.ReferenceableMiddleware[grpc.StreamServerInterceptor]{
{
Name: "logging",
Middleware: logging.StreamServerInterceptor(),
},
{
Name: "datastore",
Middleware: datastoremw.StreamServerInterceptor(ds),
},
{
Name: "consistency",
Middleware: consistency.StreamServerInterceptor(),
},
{
Name: "servicespecific",
Middleware: servicespecific.StreamServerInterceptor,
},
},
},
}),
).Complete(ctx)
require.NoError(err)
go func() {
require.NoError(srv.Run(ctx))
}()
conn, err := srv.GRPCDialContext(ctx, grpc.WithBlock())
require.NoError(err)
return conn, func() {
if conn != nil {
require.NoError(conn.Close())
}
cancel()
}, ds, revision
}