Skip to content

Commit

Permalink
fix: Use QueuedChannel for serve errors
Browse files Browse the repository at this point in the history
The queued channel means we can push errors without blocking execution.
  • Loading branch information
jameshoulahan committed Sep 9, 2022
1 parent 2947849 commit e523ea6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/ProtonMail/gluon/internal"
"github.com/ProtonMail/gluon/internal/backend"
"github.com/ProtonMail/gluon/internal/queue"
"github.com/ProtonMail/gluon/internal/session"
"github.com/ProtonMail/gluon/profiling"
"github.com/ProtonMail/gluon/reporter"
Expand Down Expand Up @@ -58,7 +59,7 @@ func (builder *serverBuilder) build() (*Server, error) {
dir: builder.dir,
backend: backend,
sessions: make(map[int]*session.Session),
serveErrCh: make(chan error),
serveErrCh: queue.NewQueuedChannel[error](1, 1),
serveDoneCh: make(chan struct{}),
inLogger: builder.inLogger,
outLogger: builder.outLogger,
Expand Down
9 changes: 5 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/ProtonMail/gluon/events"
"github.com/ProtonMail/gluon/internal"
"github.com/ProtonMail/gluon/internal/backend"
"github.com/ProtonMail/gluon/internal/queue"
"github.com/ProtonMail/gluon/internal/session"
"github.com/ProtonMail/gluon/profiling"
"github.com/ProtonMail/gluon/reporter"
Expand All @@ -40,7 +41,7 @@ type Server struct {
sessionsLock sync.RWMutex

// serveErrCh collects errors encountered while serving.
serveErrCh chan error
serveErrCh *queue.QueuedChannel[error]

// serveDoneCh is used to stop the server.
serveDoneCh chan struct{}
Expand Down Expand Up @@ -199,7 +200,7 @@ func (s *Server) serve(ctx context.Context, connCh <-chan net.Conn) {
pprof.Do(ctx, labels, func(ctx context.Context) {
if err := session.Serve(ctx); err != nil {
if !errors.Is(err, net.ErrClosed) {
s.serveErrCh <- err
s.serveErrCh.Enqueue(err)
}
}
})
Expand All @@ -210,7 +211,7 @@ func (s *Server) serve(ctx context.Context, connCh <-chan net.Conn) {

// GetErrorCh returns the error channel.
func (s *Server) GetErrorCh() <-chan error {
return s.serveErrCh
return s.serveErrCh.GetChannel()
}

func (s *Server) GetVersionInfo() internal.VersionInfo {
Expand Down Expand Up @@ -246,7 +247,7 @@ func (s *Server) Close(ctx context.Context) error {
}

// Close the server error channel.
close(s.serveErrCh)
s.serveErrCh.Close()

// Close any watchers.
for _, watcher := range s.watchers {
Expand Down

0 comments on commit e523ea6

Please sign in to comment.