timed send/recv for chan?

74 views
Skip to first unread message

yigong liu

unread,
Nov 23, 2009, 3:27:35 AM11/23/09
to golang-nuts
Hello,

I had the basic code for my little channel "scatter"/dispatcher up and running (it is attached at end of email, in case you are interested to have a look and  give me some suggestions:). we can plugin diff dispatching algorithms to the scatter, such as broadcast, roundrobin or random. Now i want to add some  flow control (to avoid one slow receiver block everybody).

Do Go's channels support timed send/recv? I didnot see it in the spec. If i need to set it up from existing features, i am thinking about using a timer to generate timeout event to a channel (chan_timeout), then use a "select" to wrap up chan_timeout and the target chan send/recv operation, such as:
select {
    case v := <- chan_target:
         do_something_with_v;
    case <- chan_timeout:
         handle_timeout;
}
Is this correct way?

How should i create a timer in Go? search the "pkg" page, did not see it.

Thanks
Yigong

scatter.tar.gz

Davies Liu

unread,
Nov 23, 2009, 4:49:49 AM11/23/09
to golang-nuts
Hi,

use Ticker as Timer:

ticker := time.NewTicker(ns);
 select {
    case v := <- chan_target:
         do_something_with_v;
    case <- ticker.C:
         handle_timeout;
 }

http://golang.org/pkg/time/#Ticker

 - Davies

ygl

unread,
Nov 23, 2009, 2:34:35 PM11/23/09
to golang-nuts

On Nov 23, 1:49 am, Davies Liu <davies....@gmail.com> wrote:

> use Ticker as Timer:
> ......
>
> http://golang.org/pkg/time/#Ticker
>

Thanks for the link.

However Ticker looks like just a stream of continuous system ticks. By
"Timer", i mean the facility i can register a function to be called
later when the specified timeout expires. Internally it has a
timing_list/queue/wheel to maintain the timeouts. By combing Ticker,
goroutine and channel, we could implement such a Timer. Just don't
want to reinvent wheels, if i can reuse one from somewhere:)

Regards
Yigong

ygl

unread,
Nov 23, 2009, 3:57:31 PM11/23/09
to golang-nuts


On Nov 23, 11:34 am, ygl <yglg...@gmail.com> wrote:
> On Nov 23, 1:49 am, Davies Liu <davies....@gmail.com> wrote:
>
> > use Ticker as Timer:
> > ......
>
> >http://golang.org/pkg/time/#Ticker
>
> Thanks for the link.
>
> However Ticker looks like just a stream of continuous system ticks. By

In fact i got it wrong! By further reading the reference for "time"
pkg, we can set the interval when creating the ticker, so it'll return
time values at the interval specified, not system raw ticks. This will
satisfy most of my usage.

Thanks!
Yigong

Rob 'Commander' Pike

unread,
Nov 23, 2009, 4:34:24 PM11/23/09
to ygl, golang-nuts
how about

go func() { time.Sleep(ns); function(args) }

-rob

ygl

unread,
Nov 23, 2009, 8:00:44 PM11/23/09
to golang-nuts

On Nov 23, 1:34 pm, "Rob 'Commander' Pike" <r...@google.com> wrote:

> > On Nov 23, 11:34 am, ygl <yglg...@gmail.com> wrote:
> >> On Nov 23, 1:49 am, Davies Liu <davies....@gmail.com> wrote:
>
> >>> use Ticker as Timer:
> >>> ......
>
> how about
>
> go func() { time.Sleep(ns); function(args) }
>
> -rob

This looks really simple and nice!
Timers in other env (such as Java) normally runs a background thread
to dispatch its scheduled tasks. Here we let goroutine runtime/
scheduler do all the job. I still need a little more milage with Go to
gain understanding and confidence using goroutine this way:).

thanks
yigong
Reply all
Reply to author
Forward
0 new messages