Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: refine reconnect signal #29

Merged
merged 5 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions watch/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (w *Watcher) loopHeartbeat() error {
for {
select {
case <-w.vas.Ctx.Done():
logs.V(1).Infof("stream heartbeat stoped because of %s", w.vas.Ctx.Err().Error())
logs.Infof("stream heartbeat stoped because of %s", w.vas.Ctx.Err().Error())
return

case <-tick.C:
Expand All @@ -77,7 +77,6 @@ func (w *Watcher) loopHeartbeat() error {
logs.Warnf("stream heartbeat failed, notify reconnect upstream, err: %v, rid: %s", err, w.vas.Rid)

w.NotifyReconnect(types.ReconnectSignal{Reason: "stream heartbeat failed"})
return
}
logs.V(1).Infof("stream heartbeat successfully, rid: %s", w.vas.Rid)
}
Expand Down
14 changes: 4 additions & 10 deletions watch/reconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@ func (w *Watcher) NotifyReconnect(signal types.ReconnectSignal) {
}

func (w *Watcher) waitForReconnectSignal() {
for { // nolint
for {
select {
case <-w.vas.Ctx.Done():
return
case signal := <-w.reconnectChan:
logs.Infof("received reconnect signal, reason: %s, rid: %s", signal.String(), w.vas.Rid)

if w.reconnecting.Load() {
logs.Warnf("received reconnect signal, but stream is already reconnecting, ignore this signal.")
return
}

// stop the previous watch stream before close conn.
w.StopWatch()
w.tryReconnect(w.vas.Rid)
Expand All @@ -50,14 +47,11 @@ func (w *Watcher) waitForReconnectSignal() {
}
}

// tryReconnect, Use NotifyReconnect method instead of direct call
func (w *Watcher) tryReconnect(rid string) {
st := time.Now()
logs.Infof("start to reconnect the upstream server, rid: %s", rid)

w.reconnecting.Store(true)
// set reconnecting to false.
defer w.reconnecting.Store(false)

retry := tools.NewRetryPolicy(5, [2]uint{500, 15000})
for {
subRid := rid + strconv.FormatUint(uint64(retry.RetryCount()), 10)
Expand Down
11 changes: 5 additions & 6 deletions watch/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
pbfs "bscp.io/pkg/protocol/feed-server"
"bscp.io/pkg/runtime/jsoni"
sfs "bscp.io/pkg/sf-share"
"go.uber.org/atomic"
"google.golang.org/grpc"

"github.com/TencentBlueKing/bscp-go/cache"
Expand All @@ -47,7 +46,6 @@ type Watcher struct {
opts option.WatchOptions
metaHeaderValue string
reconnectChan chan types.ReconnectSignal
reconnecting *atomic.Bool
Conn *grpc.ClientConn
upstream upstream.Upstream
}
Expand All @@ -68,9 +66,11 @@ func (w *Watcher) buildVas() (*kit.Vas, context.CancelFunc) {
// New return a Watcher
func New(u upstream.Upstream, opts option.WatchOptions) (*Watcher, error) {
w := &Watcher{
opts: opts,
upstream: u,
opts: opts,
upstream: u,
reconnectChan: make(chan types.ReconnectSignal),
}

mh := sfs.SidecarMetaHeader{
BizID: w.opts.BizID,
Fingerprint: w.opts.Fingerprint,
Expand All @@ -86,8 +86,7 @@ func New(u upstream.Upstream, opts option.WatchOptions) (*Watcher, error) {
// StartWatch start watch stream
func (w *Watcher) StartWatch() error {
w.vas, w.cancel = w.buildVas()
w.reconnectChan = make(chan types.ReconnectSignal, 5)
w.reconnecting = atomic.NewBool(false)

var err error
apps := []sfs.SideAppMeta{}
for _, subscriber := range w.subscribers {
Expand Down