-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgodless.go
422 lines (337 loc) · 9.99 KB
/
godless.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Godless is a peer-to-peer database running over IPFS.
//
// Godless uses a Consistent Replicated Data Type called a Namespace to share schemaless data with peers.
//
// Godless is in alpha, and should be considered experimental software.
package godless
import (
"fmt"
"sync"
"time"
gohttp "net/http"
"github.com/pkg/errors"
"github.com/johnny-morrice/godless/api"
"github.com/johnny-morrice/godless/cache"
"github.com/johnny-morrice/godless/crdt"
"github.com/johnny-morrice/godless/crypto"
"github.com/johnny-morrice/godless/datapeer"
"github.com/johnny-morrice/godless/function"
"github.com/johnny-morrice/godless/http"
"github.com/johnny-morrice/godless/internal/service"
"github.com/johnny-morrice/godless/log"
)
// Godless options.
type Options struct {
// IpfsServiceUrl is required, unless specifying your own DataPeer or RemoteStore.
IpfsServiceUrl string
// DataPeer is optional. If specified, none of the IPFS options will be used.
DataPeer api.DataPeer
// RemoteStore is optional. If specified, the DataPeer will not be used, nor any of the IPFS options.
RemoteStore api.RemoteStore
// KeyStore is required. A private Key store.
KeyStore api.KeyStore
// MemoryImage is required.
MemoryImage api.MemoryImage
// WebServiceAddr is optional. If not set, the webservice will be disabled.
WebServiceAddr string
// IndexHash is optional. Set to load an existing index from IPFS.
IndexHash string
// FailEarly will cause the godless process to crash if it cannot contact IPFS on startup.
FailEarly bool
// ReplicateInterval is optional. The duration between peer-to-peer replications.
ReplicateInterval time.Duration
// Pulse is optional. The duration between flushes of the index to IPFS.
Pulse time.Duration
// Topics is optional. Two godless servers which share a topic will replicate indices. An empty topics slice will disable replication.
Topics []string
// IpfsClient is optional. Specify a HTTP client for IPFS.
IpfsClient *gohttp.Client
// IpfsPingTimeout is optional. Specify a lower timeout for "Am I Connected?" checks.
IpfsPingTimeout time.Duration
// Functions is optional.
Functions function.FunctionNamespace
// Cache is optional. Build a 12-factor app by supplying your own remote cache.
// HeadCache, IndexCache and NamespaceCache can be used to specify different caches for different data types.
Cache api.Cache
// PriorityQueue is optional. Build a 12-factor app by supplying your own remote cache.
PriorityQueue api.RequestPriorityQueue
// ApiConcurrency is optional. Tune performance by setting the number of simultaneous queries.
ApiConcurrency int
// PublicServer is optional. If false, the index will only be updated from peers who are in your public key list.
PublicServer bool
// WebService is optional.
WebService api.WebService
// Shutdown mechanism
shutdownLock sync.Mutex
isShutdownInProgress bool
}
// Godless is a peer-to-peer database. It shares structured data between peers, using IPFS as a backing store.
// The core datastructure is a CRDT namespace which resembles a relational scheme in that it has tables, rows, and entries.
type Godless struct {
Options
api api.Service
errch chan error
errwg sync.WaitGroup
stopch chan struct{}
stoppers []api.Closer
remote api.Core
}
// New creates a godless instance, connecting to any services, and providing any services, specified in the options.
func New(options Options) (*Godless, error) {
godless := &Godless{Options: options}
missing := godless.findMissingParameters()
if missing != nil {
return nil, missing
}
setupFuncs := []func() error{
godless.connectDataPeer,
godless.connectRemoteStore,
godless.connectCache,
godless.setupNamespace,
godless.launchAPI,
godless.setupWebService,
godless.serveWeb,
godless.replicate,
}
err := breakOnError(setupFuncs)
if err != nil {
return nil, err
}
godless.report()
return godless, nil
}
func (godless *Godless) Send(request api.Request) (api.Response, error) {
respch, err := godless.api.Call(request)
if err != nil {
return api.RESPONSE_FAIL, err
}
resp := <-respch
if resp.Err != nil {
return resp, resp.Err
}
return resp, nil
}
func (godless *Godless) report() {
if godless.PublicServer {
log.Info("Running public Godless API")
} else {
log.Info("Running private Godless API")
}
privCount := len(godless.KeyStore.GetAllPrivateKeys())
pubCount := len(godless.KeyStore.GetAllPublicKeys())
log.Info("Godless API using %d private and %d public keys", privCount, pubCount)
}
func (godless *Godless) findMissingParameters() error {
var missing error
if godless.KeyStore == nil {
msg := godless.missingParameterText("KeyStore")
missing = addErrorMessage(missing, msg)
}
if godless.MemoryImage == nil {
msg := godless.missingParameterText("MemoryImage")
missing = addErrorMessage(missing, msg)
}
return missing
}
func addErrorMessage(err error, msg string) error {
if err == nil {
return errors.New(msg)
} else {
return errors.Wrap(err, msg)
}
}
func (godless *Godless) missingParameterText(param string) string {
return fmt.Sprintf("Missing required parameter '%v'", param)
}
// Errors provides a stream of errors from godless. Godless will attempt to handle any errors it can. Any errors received here indicate that bad things have happened.
func (godless *Godless) Errors() <-chan error {
return godless.errch
}
// Shutdown stops all godless processes. It waits for all processes to stop.
func (godless *Godless) Shutdown() {
godless.shutdownLock.Lock()
defer godless.shutdownLock.Unlock()
if godless.isShutdownInProgress {
return
}
godless.isShutdownInProgress = true
godless.api.CloseAPI()
if godless.WebService != nil {
godless.WebService.Close()
}
if godless.Cache != nil {
godless.Cache.CloseCache()
}
for _, closer := range godless.stoppers {
closer.Close()
}
}
func (godless *Godless) connectDataPeer() error {
if godless.RemoteStore != nil {
return nil
}
if godless.DataPeer == nil {
if godless.IpfsServiceUrl == "" {
msg := godless.missingParameterText("IpfsServiceUrl")
return errors.New(msg)
}
options := datapeer.IpfsWebServiceOptions{
Url: godless.IpfsServiceUrl,
PingTimeout: godless.IpfsPingTimeout,
Http: godless.IpfsClient,
}
peer := datapeer.MakeIpfsWebService(options)
godless.DataPeer = peer
}
return godless.DataPeer.Connect()
}
func (godless *Godless) connectRemoteStore() error {
if godless.RemoteStore == nil {
ipfs := &service.ContentAddressableRemoteStore{
Shell: godless.DataPeer,
}
if godless.FailEarly {
err := ipfs.Connect()
if err != nil {
return err
}
}
godless.RemoteStore = ipfs
}
return nil
}
func (godless *Godless) connectCache() error {
if godless.Cache == nil {
godless.Cache = cache.MakeResidentMemoryCache(__UNKNOWN_BUFFER_SIZE, __UNKNOWN_BUFFER_SIZE)
}
return nil
}
func (godless *Godless) setupNamespace() error {
if godless.IndexHash != "" {
head := crdt.IPFSPath(godless.IndexHash)
err := godless.Cache.SetHead(head)
if err != nil {
return err
}
}
if godless.Functions == nil {
godless.Functions = function.StandardFunctions()
}
namespaceOptions := service.RemoteNamespaceCoreOptions{
Pulse: godless.Pulse,
Store: godless.RemoteStore,
Cache: godless.Cache,
KeyStore: godless.KeyStore,
IsPublicIndex: godless.PublicServer,
MemoryImage: godless.MemoryImage,
Functions: godless.Functions,
}
godless.remote = service.MakeRemoteNamespaceCore(namespaceOptions)
return nil
}
func (godless *Godless) launchAPI() error {
limit := godless.ApiConcurrency
if limit == 0 {
limit = 1
}
queue := godless.PriorityQueue
if queue == nil {
queue = cache.MakeResidentBufferQueue(__UNKNOWN_BUFFER_SIZE)
}
validator := api.StaticRequestValidator{
FunctionNamespace: godless.Functions,
}
options := service.QueuedApiServiceOptions{
Core: godless.remote,
Queue: queue,
QueryLimit: limit,
Validator: validator,
}
api, errch := service.LaunchQueuedApiService(options)
godless.addErrors(errch)
godless.api = api
return nil
}
func (godless *Godless) setupWebService() error {
if godless.WebService != nil {
return nil
}
options := http.WebServiceOptions{
Api: godless.api,
}
godless.WebService = http.MakeWebService(options)
return nil
}
// Serve serves the Godless webservice.
func (godless *Godless) serveWeb() error {
addr := godless.WebServiceAddr
if addr == "" {
return nil
}
handler := godless.WebService.GetApiRequestHandler()
closer, err := http.Serve(addr, handler)
if err != nil {
return err
}
godless.addCloser(closer)
return nil
}
// Replicate shares data via the IPFS pubsub mechanism.
func (godless *Godless) replicate() error {
topics := godless.Topics
interval := godless.ReplicateInterval
if len(topics) == 0 {
return nil
}
pubsubTopics := make([]api.PubSubTopic, len(topics))
for i, t := range topics {
pubsubTopics[i] = api.PubSubTopic(t)
}
options := service.ReplicateOptions{
API: godless.api,
RemoteStore: godless.RemoteStore,
Interval: interval,
Topics: pubsubTopics,
KeyStore: godless.KeyStore,
}
closer, errch := service.Replicate(options)
godless.addCloser(closer)
godless.addErrors(errch)
return nil
}
func (godless *Godless) addCloser(closer api.Closer) {
if godless.stopch == nil {
godless.stopch = make(chan struct{})
}
godless.stoppers = append(godless.stoppers, closer)
}
func (godless *Godless) addErrors(errch <-chan error) {
godless.errwg.Add(1)
if godless.errch == nil {
godless.errch = make(chan error)
go func() {
godless.errwg.Wait()
close(godless.errch)
}()
}
go func() {
for err := range errch {
godless.errch <- err
}
godless.errwg.Done()
}()
}
func MakeKeyStore() api.KeyStore {
return &crypto.KeyStore{}
}
func breakOnError(pipeline []func() error) error {
for _, f := range pipeline {
err := f()
if err != nil {
return err
}
}
return nil
}
// We don't know the right buffer size here, so let the cache package handle it.
const __UNKNOWN_BUFFER_SIZE = -1