Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Sep 5, 2023
1 parent 52b74e6 commit e97227e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (mi *ModuleInstance) stream(c goja.ConstructorCall) *goja.Object {
instanceMetrics: mi.metrics,
builtinMetrics: mi.vu.State().BuiltinMetrics,
done: make(chan struct{}),
writing: opened,
writingState: opened,

writeQueueCh: make(chan message),

Expand Down
18 changes: 13 additions & 5 deletions grpc/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ type stream struct {

obj *goja.Object // the object that is given to js to interact with the stream

writing int8
done chan struct{}
writingState int8
done chan struct{}

writeQueueCh chan message

Expand Down Expand Up @@ -300,7 +300,7 @@ func (s *stream) on(event string, listener func(goja.Value) (goja.Value, error))

// write writes a message to the stream
func (s *stream) write(input goja.Value) {
if s.writing != opened {
if s.writingState != opened {
return
}

Expand All @@ -321,13 +321,13 @@ func (s *stream) write(input goja.Value) {

// end closes client the stream
func (s *stream) end() {
if s.writing == closed {
if s.writingState == closed {
return
}

s.logger.Debug("stream is closing")

s.writing = closed
s.writingState = closed
s.writeQueueCh <- message{isClosing: true}
}

Expand All @@ -344,7 +344,15 @@ func (s *stream) close(err error) {
return
}

select {
case <-s.done:
s.logger.Debugf("stream %v is already closed", s.method)
return
default:
}

close(s.done)

s.tq.Queue(func() error {
return s.callEventListeners(eventEnd)
})
Expand Down

0 comments on commit e97227e

Please sign in to comment.