开发者

async reply in registry pattern

I'm learning go, and I would like to explore some patterns.

I would like to build a Registry component which maintains a map of some stuff, and I want to provide a serialized access to it:

Currently I ended up with something like this:

type JobRegistry struct {
  submission chan JobRegistrySubmitRequest
  listing chan JobRegistryListRequest
}

type JobRegistrySubmitRequest struct {
  request JobSubmissionRequest
  response chan Job
}

type JobRegistryListRequest struct {
  response chan []Job
}

func NewJobRegistry() (this *JobRegistry) {
  this = &JobRegistry{make(chan JobRegistrySubmitRequest, 10), make(chan JobRegistryListRequest, 10)}

  go func() {
    jobMap := make(map[string] Job)

    for {
        select {
        case sub := <- this.submission:
            job := MakeJob(sub.request) // ....

            jobMap[job.Id] = job
            sub.response <- job.Id

        case list := <- this.listing:

            res := make([]Job, 0, 100)
            for _, v := range jobMap {
                res = append(res, v)
            }
            list.response <- res

        }

        /// case somechannel....
     }
   }()

   return
}

Basically, I encapsulate each operation inside a struct, which carries the parameters and a response channel.

Then I created helper methods for end users:

func (this *JobRegistry) List() ([]Job, os.Error) {
    res := make(chan []Job, 1)
    req := JobRegistryListRequest{res}
    this.listing <- req
    return <-res, nil // todo: handle errors like timeouts
}

I decided to use a channel for each type of request in order to be type safe.


The problem I see with this approach are:

  • A lot of boilerplate code and a 开发者_C百科lot of places to modify when some param/return type changes

  • Have to do weird things like create yet another wrapper struct in order to return errors from within the handler goroutine. (If I understood correctly there are no tuples, and no way to send multiple values in a channel, like multi-valued returns)

So, I'm wondering whether all this makes sense, or rather just get back to good old locks.

I'm sure that somebody will find some clever way out using channels.


I'm not entirely sure I understand you, but I'll try answering never the less.

You want a generic service that executes jobs sent to it. You also might want the jobs to be serializable.

What we need is an interface that would define a generic job.

type Job interface {
    Run()
    Serialize(io.Writer)
}

func ReadJob(r io.Reader) {...}

type JobManager struct {
    jobs map[int] Job
    jobs_c chan Job      
}

func NewJobManager (mgr *JobManager) {
    mgr := &JobManager{make(map[int]Job),make(chan Job,JOB_QUEUE_SIZE)}
    for {
        j,ok := <- jobs_c
        if !ok {break}
        go j.Run()
    }
}

type IntJob struct{...}
func (job *IntJob) GetOutChan() chan int {...}
func (job *IntJob) Run() {...}
func (job *IntJob) Serialize(o io.Writer) {...}

Much less code, and roughly as useful.

About signaling errors with an axillary stream, you can always use a helper function.

type IntChanWithErr struct {
    c chan int
    errc chan os.Error
}
func (ch *IntChanWithErr) Next() (v int,err os.Error) {
    select {
        case v := <- ch.c // not handling closed channel
        case err := <- ch.errc
    }
    return
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜