-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
108 lines (93 loc) · 2.83 KB
/
client.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
// Copyright (C) 2019 rameshvk. All rights reserved.
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file.
package dot
import (
"log"
"math/rand"
"os"
"time"
dotlog "github.com/dotchain/dot/log"
"github.com/dotchain/dot/ops"
"github.com/dotchain/dot/ops/nw"
"github.com/dotchain/dot/ops/sync"
"github.com/dotchain/dot/streams"
)
// Session represents a client session
type Session struct {
Version int
Pending, Merge []ops.Op
OpCache map[int]ops.Op
MergeCache map[int][]ops.Op
}
// NewSession creates an empty session
func NewSession() *Session {
return &Session{
Version: -1,
OpCache: map[int]ops.Op{},
MergeCache: map[int][]ops.Op{},
}
}
// Stream returns the stream of changes for this session
//
// The returned store can be used to *close* the stream when needed
//
// Actual syncing of messages happens when Push and Pull are called on the stream
func (s *Session) Stream(url string, logger dotlog.Log) (streams.Stream, ops.Store) {
if logger == nil {
logger = log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)
}
store := &nw.Client{
URL: url,
Log: logger,
ContentType: "application/x-sjson",
}
stream := sync.Stream(
store,
sync.WithNotify(s.UpdateVersion),
sync.WithSession(s.Version, s.Pending, s.Merge),
sync.WithLog(logger),
sync.WithBackoff(rand.Float64, time.Second, time.Minute),
sync.WithAutoTransform(s),
)
return stream, store
}
// NonBlockingStream returns the stream of changes for this session
//
// The returned store can be used to *close* the stream when needed
//
// Actual syncing of messages happens when Push and Pull are called on
// the stream. Pull() does the server-fetch asynchronously, returning
// immediately if there is no server data available.
func (s *Session) NonBlockingStream(url string, logger dotlog.Log) (streams.Stream, ops.Store) {
if logger == nil {
logger = log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)
}
store := &nw.Client{
URL: url,
Log: logger,
ContentType: "application/x-sjson",
}
stream := sync.Stream(
store,
sync.WithNotify(s.UpdateVersion),
sync.WithSession(s.Version, s.Pending, s.Merge),
sync.WithLog(logger),
sync.WithBackoff(rand.Float64, time.Second, time.Minute),
sync.WithAutoTransform(s),
sync.WithNonBlocking(true),
)
return stream, store
}
// Load implements the ops.Cache load interface
func (s *Session) Load(ver int) (ops.Op, []ops.Op) {
return s.OpCache[ver], s.MergeCache[ver]
}
// Store implements the ops.Cache store interface
func (s *Session) Store(ver int, op ops.Op, merge []ops.Op) {
s.OpCache[ver], s.MergeCache[ver] = op, merge
}
// UpdateVersion updates the version/pending info
func (s *Session) UpdateVersion(version int, pending, merge []ops.Op) {
s.Version, s.Pending, s.Merge = version, pending, merge
}