@@ -22,6 +22,7 @@ import (
2222 "errors"
2323 "fmt"
2424 "strings"
25+ "sync"
2526
2627 "github.com/moov-io/achgateway/internal/incoming"
2728 "github.com/moov-io/achgateway/internal/incoming/stream"
@@ -41,6 +42,7 @@ import (
4142type FileReceiver struct {
4243 logger log.Logger
4344 cfg * service.Config
45+ mu sync.RWMutex
4446
4547 defaultShardName string
4648
@@ -79,6 +81,9 @@ func newFileReceiver(
7981}
8082
8183func (fr * FileReceiver ) reconnect () error {
84+ fr .mu .Lock ()
85+ defer fr .mu .Unlock ()
86+
8287 // Close any existing subscription
8388 if fr .streamFiles != nil {
8489 fr .streamFiles .Shutdown (context .Background ())
@@ -94,6 +99,9 @@ func (fr *FileReceiver) reconnect() error {
9499}
95100
96101func (fr * FileReceiver ) ReplaceStreamFiles (sub stream.Subscription ) {
102+ fr .mu .Lock ()
103+ defer fr .mu .Unlock ()
104+
97105 // Close an existing stream subscription
98106 if fr .streamFiles != nil {
99107 fr .streamFiles .Shutdown (context .Background ())
@@ -121,7 +129,7 @@ func (fr *FileReceiver) Start(ctx context.Context) {
121129 fr .logger .LogErrorf ("error handling stream file: %v" , err )
122130
123131 // Attempt to reconnect under some conditions
124- if contains (err , "write: broken pipe" ) {
132+ if isNetworkError (err ) {
125133 fr .logger .Info ().Log ("attempt to reconnect to stream subscription" )
126134 if err := fr .reconnect (); err != nil {
127135 fr .logger .LogErrorf ("unable to reconnect stream subscription: %v" , err )
@@ -172,6 +180,9 @@ func (fr *FileReceiver) RegisterAdminRoutes(r *admin.Server) {
172180// handleMessage will listen for an incoming.ACHFile to pass off to an aggregator for the shard
173181// responsible. It does so with a database lookup and the fixed set of Shards from the file config.
174182func (fr * FileReceiver ) handleMessage (ctx context.Context , sub stream.Subscription ) chan error {
183+ fr .mu .RLock ()
184+ defer fr .mu .RUnlock ()
185+
175186 out := make (chan error , 1 )
176187 if sub == nil {
177188 return out
@@ -191,8 +202,9 @@ func (fr *FileReceiver) handleMessage(ctx context.Context, sub stream.Subscripti
191202 return
192203 }
193204 // Bubble up some errors to alerting
194- if contains (err , "connect: " , "write:" , "broken pipe" , "pubsub" , "EOF" ) {
205+ if isNetworkError (err ) {
195206 out <- err
207+ return
196208 }
197209 fr .logger .LogErrorf ("ERROR receiving message: %v" , err )
198210 }
@@ -217,6 +229,10 @@ func (fr *FileReceiver) handleMessage(ctx context.Context, sub stream.Subscripti
217229 return out
218230}
219231
232+ func isNetworkError (err error ) bool {
233+ return contains (err , "connect: " , "write:" , "broken pipe" , "pubsub" , "EOF" )
234+ }
235+
220236func contains (err error , options ... string ) bool {
221237 es := err .Error ()
222238 for i := range options {
0 commit comments