1

I need to organize the communication between two goland services but have some problems

func main() {

    ctxWithCancel, cancel := context.WithCancel(context.Background())
    sig := make(chan os.Signal)
    signal.Notify(sig, os.Interrupt)
    defer signal.Stop(sig)

    wg := &sync.WaitGroup{}

    for i := range conf.Services {
        if conf.Services[i].Type == "http" {
            // call returns HttpService service
            http.NewHttpService(ctxWithCancel, wg, &conf.Services[i]) 
        } else if conf.Services[i].Type == "tarantool" {
            // call returns Tarantool service
            tarantool.NewTarantoolService(ctxWithCancel, wg, &conf.Services[i])
        } else {
            log.Fatalf("Unknown service name: %v\n", conf.Services[i].Type)
        }
    }

    select {
    case <-sig:
        cancel()
        wg.Wait()
    }
}

HttpService and TarantoolService are services with infinite loop and the request handlers(runs as go subroutine). NewHttpService is http web server and on the request it should take some data from NewTarantoolService service synchronously.

The question is how to communicate HttpService with TarantoolService. Of course I could pass TarantoolService to the http.NewHttpService as parameter - but I want to use disconnectedness services. Please explain how I could achieve this?

Roman Kazmin
  • 931
  • 6
  • 18

1 Answers1

1

You can use https://gobyexample.com/channels. If your NewTarantoolService has an infinite cycle you could use the following construction:

for {
   select {
   case msg :=<-fromHttpChannel:
      //Do something
      toHttpChannel<- msg
   case <-ctx.Done():
      return
  }
}
 

Volodymyr
  • 89
  • 9