This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
forked from streamingfast/bstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplexedsource.go
142 lines (118 loc) · 3.3 KB
/
multiplexedsource.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
// Copyright 2019 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bstream
import (
"sync"
"time"
"github.com/streamingfast/shutter"
"go.uber.org/zap"
)
var sourceReconnectDelay = time.Second * 5 // override me on tests
type MultiplexedSourceOption = func(s *MultiplexedSource)
func MultiplexedSourceWithLogger(logger *zap.Logger) MultiplexedSourceOption {
return func(s *MultiplexedSource) {
s.logger = logger
}
}
// MultiplexedSource contains a gator based on realtime
type MultiplexedSource struct {
*shutter.Shutter
handler Handler
sourceFactories []SourceFactory
sources []Source
sourcesLock sync.Mutex
handlerLock sync.Mutex
logger *zap.Logger
}
func NewMultiplexedSource(sourceFactories []SourceFactory, h Handler, opts ...MultiplexedSourceOption) *MultiplexedSource {
m := &MultiplexedSource{
handler: h,
sourceFactories: sourceFactories,
sources: make([]Source, len(sourceFactories)),
logger: zlog,
}
for _, opt := range opts {
opt(m)
}
m.Shutter = shutter.New()
m.Shutter.OnTerminating(func(_ error) {
m.sourcesLock.Lock()
defer m.sourcesLock.Unlock()
for _, s := range m.sources {
if s != nil {
s.Shutdown(nil)
}
}
})
return m
}
func (s *MultiplexedSource) Run() {
for {
if s.IsTerminating() {
return
}
s.connectSources()
time.Sleep(sourceReconnectDelay)
}
}
func (s *MultiplexedSource) SetLogger(logger *zap.Logger) {
s.logger = logger
}
func (s *MultiplexedSource) connectSources() {
s.sourcesLock.Lock()
defer s.sourcesLock.Unlock()
if s.IsTerminating() {
return
}
failingSources := 0
for idx, factory := range s.sourceFactories {
src := s.sources[idx]
if src == nil || src.IsTerminating() {
shuttingSrcHandler := HandlerFunc(func(blk *Block, obj interface{}) error {
s.handlerLock.Lock()
err := s.handler.ProcessBlock(blk, obj)
s.handlerLock.Unlock()
if err != nil {
s.logger.Error("unable to process block, shutting down source")
s.Shutdown(err)
}
return err
})
newSrc := factory(shuttingSrcHandler)
s.logger.Info("new source factory created")
err := s.LockedInit(func() error {
s.logger.Debug("safe running source")
s.sources[idx] = newSrc
go newSrc.Run()
return nil
})
if err != nil {
s.logger.Error("safe run", zap.Error(err))
s.Shutdown(err)
}
}
}
if failingSources >= len(s.sourceFactories) {
s.logger.Warn("warning, all sources are failing",
zap.Int("source_count", len(s.sources)),
zap.Int("source_factory_count", len(s.sourceFactories)),
)
} else if failingSources > 0 {
s.logger.Info("some sources were down",
zap.Int("failed_source_count", failingSources),
zap.Int("source_count", len(s.sources)),
zap.Int("source_factory_count", len(s.sourceFactories)),
)
}
}