-
Notifications
You must be signed in to change notification settings - Fork 14
/
context.go
185 lines (142 loc) · 3.16 KB
/
context.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package wire
import (
"io"
"sync"
"sync/atomic"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/horizon/pkg/pb"
"github.com/pkg/errors"
)
type Context interface {
Account() *pb.Account
ReadMarshal(v Unmarshaller) (byte, error)
WriteMarshal(tag byte, v Marshaller) error
// Forwards any data between the 2 contexts
BridgeTo(other Context) error
// Returns a writer that will send traffic as framed messages
Writer() io.WriteCloser
// Returns a reader that recieves traffic as framed messages
Reader() io.Reader
// Returns the total number of messages and bytes, respectively, that the
// context has transmitted.
Accounting() (int64, int64)
// Close the context and cleanup any resources. Does not close
// any IOs though.
Close() error
}
type ctx struct {
accountId *pb.Account
fr *FramingReader
fw *FramingWriter
// accounting
messages *int64
bytes *int64
closers []func() error
}
func NewContext(accountId *pb.Account, fr *FramingReader, fw *FramingWriter) Context {
return &ctx{
accountId: accountId,
fr: fr,
fw: fw,
messages: new(int64),
bytes: new(int64),
}
}
func (c *ctx) Close() error {
var err error
for _, f := range c.closers {
serr := f()
if serr != nil {
err = multierror.Append(err, serr)
}
}
return err
}
type closeCtx struct {
Context
closers []func() error
}
func WithCloser(c Context, closers ...func() error) Context {
if priv, ok := c.(*ctx); ok {
priv.closers = append(priv.closers, closers...)
return priv
}
return &closeCtx{Context: c, closers: closers}
}
func (cc *closeCtx) Close() error {
var err error
for _, f := range cc.closers {
serr := f()
if serr != nil {
err = multierror.Append(err, serr)
}
}
parent := cc.Context.Close()
if parent != nil {
err = multierror.Append(err, parent)
}
return err
}
func (c *ctx) Account() *pb.Account {
return c.accountId
}
func (c *ctx) Accounting() (int64, int64) {
return atomic.LoadInt64(c.messages), atomic.LoadInt64(c.bytes)
}
func (c *ctx) ReadMarshal(v Unmarshaller) (byte, error) {
tag, _, err := c.fr.ReadMarshal(v)
if err != nil {
return 0, err
}
return tag, nil
}
func (c *ctx) WriteMarshal(tag byte, v Marshaller) error {
sz, err := c.fw.WriteMarshal(tag, v)
if err != nil {
return err
}
atomic.AddInt64(c.messages, 1)
atomic.AddInt64(c.bytes, int64(sz))
return nil
}
func (c *ctx) Writer() io.WriteCloser {
return c.fw.WriteAdapter()
}
func (c *ctx) Reader() io.Reader {
return c.fr.ReadAdapter()
}
var ErrInvalidContext = errors.New("invalid context type")
func (c *ctx) copyTo(octx *ctx) error {
buf := make([]byte, 32*1024)
for {
tag, sz, err := c.fr.Next()
if err != nil {
return err
}
err = octx.fw.WriteFrame(tag, sz)
if err != nil {
return err
}
_, err = io.CopyBuffer(octx.fw, c.fr, buf)
if err != nil {
return err
}
}
}
func (c *ctx) BridgeTo(other Context) error {
octx, ok := other.(*ctx)
if !ok {
return ErrInvalidContext
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
c.copyTo(octx)
}()
octx.copyTo(c)
wg.Wait()
return nil
}