-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add timeout operation for Mono. (#8)
- Loading branch information
Showing
5 changed files
with
140 additions
and
9 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
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,80 @@ | ||
package mono | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/jjeffcaii/reactor-go" | ||
"github.com/jjeffcaii/reactor-go/hooks" | ||
"github.com/jjeffcaii/reactor-go/internal" | ||
) | ||
|
||
type monoTimeout struct { | ||
source reactor.RawPublisher | ||
timeout time.Duration | ||
} | ||
|
||
type timeoutSubscriber struct { | ||
actual reactor.Subscriber | ||
timeout time.Duration | ||
done chan struct{} | ||
} | ||
|
||
func (t *timeoutSubscriber) OnComplete() { | ||
select { | ||
case <-t.done: | ||
default: | ||
close(t.done) | ||
t.actual.OnComplete() | ||
} | ||
} | ||
|
||
func (t *timeoutSubscriber) OnError(err error) { | ||
select { | ||
case <-t.done: | ||
hooks.Global().OnErrorDrop(err) | ||
default: | ||
close(t.done) | ||
t.actual.OnError(err) | ||
} | ||
} | ||
|
||
func (t *timeoutSubscriber) OnNext(any reactor.Any) { | ||
select { | ||
case <-t.done: | ||
hooks.Global().OnNextDrop(any) | ||
default: | ||
t.actual.OnNext(any) | ||
} | ||
} | ||
|
||
func (t *timeoutSubscriber) OnSubscribe(ctx context.Context, subscription reactor.Subscription) { | ||
timer := time.NewTimer(t.timeout) | ||
go func() { | ||
defer timer.Stop() | ||
select { | ||
case <-timer.C: | ||
t.OnError(reactor.ErrSubscribeCancelled) | ||
case <-t.done: | ||
} | ||
}() | ||
t.actual.OnSubscribe(ctx, subscription) | ||
} | ||
|
||
func (m *monoTimeout) SubscribeWith(ctx context.Context, subscriber reactor.Subscriber) { | ||
subscriber = internal.ExtractRawSubscriber(subscriber) | ||
ts := &timeoutSubscriber{ | ||
actual: subscriber, | ||
timeout: m.timeout, | ||
done: make(chan struct{}), | ||
} | ||
subscriber = internal.NewCoreSubscriber(ts) | ||
m.source.SubscribeWith(ctx, subscriber) | ||
} | ||
|
||
func newMonoTimeout(source reactor.RawPublisher, timeout time.Duration) *monoTimeout { | ||
return &monoTimeout{ | ||
source: source, | ||
timeout: timeout, | ||
} | ||
} |
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