This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #393 from TheThingsNetwork/develop
v2.0.3
- Loading branch information
Showing
17 changed files
with
576 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright © 2016 The Things Network | ||
// Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
|
||
package monitor | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/TheThingsNetwork/ttn/api/broker" | ||
"github.com/TheThingsNetwork/ttn/utils/backoff" | ||
"github.com/TheThingsNetwork/ttn/utils/errors" | ||
"github.com/golang/protobuf/ptypes/empty" | ||
) | ||
|
||
func (cl *brokerClient) initDownlink() { | ||
cl.downlink.ch = make(chan *broker.DownlinkMessage, BufferSize) | ||
go cl.monitorDownlink() | ||
} | ||
|
||
func (cl *brokerClient) monitorDownlink() { | ||
var retries int | ||
newStream: | ||
for { | ||
ctx, cancel := context.WithCancel(cl.Context()) | ||
cl.downlink.Lock() | ||
cl.downlink.cancel = cancel | ||
cl.downlink.Unlock() | ||
|
||
stream, err := cl.client.client.BrokerDownlink(ctx) | ||
if err != nil { | ||
cl.Ctx.WithError(errors.FromGRPCError(err)).Warn("Failed to open new monitor downlink stream") | ||
|
||
retries++ | ||
time.Sleep(backoff.Backoff(retries)) | ||
|
||
continue | ||
} | ||
retries = 0 | ||
cl.Ctx.Debug("Opened new monitor downlink stream") | ||
|
||
// The actual stream | ||
go func() { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case downlink, ok := <-cl.downlink.ch: | ||
if ok { | ||
stream.Send(downlink) | ||
cl.Ctx.Debug("Sent downlink to monitor") | ||
} | ||
} | ||
} | ||
}() | ||
|
||
msg := new(empty.Empty) | ||
for { | ||
if err := stream.RecvMsg(&msg); err != nil { | ||
cl.Ctx.WithError(errors.FromGRPCError(err)).Warn("Received error on monitor downlink stream, closing...") | ||
stream.CloseSend() | ||
cl.Ctx.Debug("Closed monitor downlink stream") | ||
|
||
cl.downlink.Lock() | ||
cl.downlink.cancel() | ||
cl.downlink.cancel = nil | ||
cl.downlink.Unlock() | ||
|
||
retries++ | ||
time.Sleep(backoff.Backoff(retries)) | ||
|
||
continue newStream | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (cl *brokerClient) closeDownlink() { | ||
cl.downlink.Lock() | ||
defer cl.downlink.Unlock() | ||
if cl.downlink.cancel != nil { | ||
cl.downlink.cancel() | ||
} | ||
} | ||
|
||
// SendDownlink sends downlink to the monitor | ||
func (cl *brokerClient) SendDownlink(downlink *broker.DownlinkMessage) (err error) { | ||
cl.downlink.init.Do(cl.initDownlink) | ||
|
||
select { | ||
case cl.downlink.ch <- downlink: | ||
default: | ||
cl.Ctx.Warn("Not sending downlink to monitor, buffer full") | ||
return errors.New("Not sending downlink to monitor, buffer full") | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright © 2016 The Things Network | ||
// Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
|
||
package monitor | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/TheThingsNetwork/ttn/api/broker" | ||
"github.com/TheThingsNetwork/ttn/utils/backoff" | ||
"github.com/TheThingsNetwork/ttn/utils/errors" | ||
"github.com/golang/protobuf/ptypes/empty" | ||
) | ||
|
||
func (cl *brokerClient) initUplink() { | ||
cl.uplink.ch = make(chan *broker.DeduplicatedUplinkMessage, BufferSize) | ||
go cl.monitorUplink() | ||
} | ||
|
||
func (cl *brokerClient) monitorUplink() { | ||
var retries int | ||
newStream: | ||
for { | ||
ctx, cancel := context.WithCancel(cl.Context()) | ||
cl.uplink.Lock() | ||
cl.uplink.cancel = cancel | ||
cl.uplink.Unlock() | ||
|
||
stream, err := cl.client.client.BrokerUplink(ctx) | ||
if err != nil { | ||
cl.Ctx.WithError(errors.FromGRPCError(err)).Warn("Failed to open new monitor uplink stream") | ||
|
||
retries++ | ||
time.Sleep(backoff.Backoff(retries)) | ||
|
||
continue | ||
} | ||
retries = 0 | ||
cl.Ctx.Debug("Opened new monitor uplink stream") | ||
|
||
// The actual stream | ||
go func() { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case uplink, ok := <-cl.uplink.ch: | ||
if ok { | ||
stream.Send(uplink) | ||
cl.Ctx.Debug("Sent uplink to monitor") | ||
} | ||
} | ||
} | ||
}() | ||
|
||
msg := new(empty.Empty) | ||
for { | ||
if err := stream.RecvMsg(&msg); err != nil { | ||
cl.Ctx.WithError(errors.FromGRPCError(err)).Warn("Received error on monitor uplink stream, closing...") | ||
stream.CloseSend() | ||
cl.Ctx.Debug("Closed monitor uplink stream") | ||
|
||
cl.uplink.Lock() | ||
cl.uplink.cancel() | ||
cl.uplink.cancel = nil | ||
cl.uplink.Unlock() | ||
|
||
retries++ | ||
time.Sleep(backoff.Backoff(retries)) | ||
|
||
continue newStream | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (cl *brokerClient) closeUplink() { | ||
cl.uplink.Lock() | ||
defer cl.uplink.Unlock() | ||
if cl.uplink.cancel != nil { | ||
cl.uplink.cancel() | ||
} | ||
} | ||
|
||
// SendUplink sends uplink to the monitor | ||
func (cl *brokerClient) SendUplink(uplink *broker.DeduplicatedUplinkMessage) (err error) { | ||
cl.uplink.init.Do(cl.initUplink) | ||
|
||
select { | ||
case cl.uplink.ch <- uplink: | ||
default: | ||
cl.Ctx.Warn("Not sending uplink to monitor, buffer full") | ||
return errors.New("Not sending uplink to monitor, buffer full") | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.