syslog icon indicating copy to clipboard operation
syslog copied to clipboard

Handler.Queue() example

Open chrissnell opened this issue 11 years ago • 2 comments

Hi Michał,

I'm really new to Go and I'm working with your syslog package. I was reading through the source and noticed Handler.Queue() but I'm still struggling to understand channels, particularly how they might be used with your package. I'm writing a syslog proxy and I would like to be able to read in new messages and send them on their way without blocking. I was wondering if you might provide an example of how to do this.

Thanks,

Chris

chrissnell avatar Jan 22 '14 22:01 chrissnell

You always can write your own handler that implements syslog.Handler interface.

If you want to use syslog.BaseHandler instead write your own one, you can use its Get method to read message from BaseHandler internal queue. But Get blocks and waits for message if queue is empty. If you do not want to block your whole program you have two ways:

  1. Create separate gorutine for reading from handler using Get method.
  2. Use BaseHandler.Queue:

q := h.Queue() select { case msg := <-q: // do something with msg case something := <-your_other_channel: // do something with your other data default: // this is need if you don't want block // there is no any data. }

ziutek avatar Jan 23 '14 11:01 ziutek

Thanks! That helps a lot.

Chris

On Thu, Jan 23, 2014 at 3:22 AM, Michał Derkacz [email protected]:

You always can write your own handler that implements syslog.Handler interface.

If you want to use syslog.BaseHandler instead write your own one, you can use its Get method to read message from BaseHandler internal queue. But Get blocks and waits for message if queue is empty. If you do not want to block your whole program you have two ways:

Create separate gorutine for reading from handler using Get method. 2.

Use BaseHandler.Queue:

q := h.Queue() select { case msg := <-q:

default:
    // None free, so allocate a new one.
    b = new(Buffer)
}

}

— Reply to this email directly or view it on GitHubhttps://github.com/ziutek/syslog/issues/1#issuecomment-33115883 .

chrissnell avatar Jan 23 '14 19:01 chrissnell