// Go routine to keep registered into // Eureka service discovery func sendHeartBeatToEureka(ec fargo.EurekaConnection, i fargo.Instance) { ticker := time.Tick(time.Duration(30*1000) * time.Millisecond) for { select { case <-ticker: ec.HeartBeatInstance(&i) } } }
func withCustomRegisteredInstance(e *fargo.EurekaConnection, application string, hostName string, f func(i *fargo.Instance)) func() { return func() { vipAddress := "app" i := &fargo.Instance{ HostName: hostName, Port: 9090, App: application, IPAddr: "127.0.0.10", VipAddress: vipAddress, DataCenterInfo: fargo.DataCenterInfo{Name: fargo.MyOwn}, SecureVipAddress: vipAddress, Status: fargo.UP, LeaseInfo: fargo.LeaseInfo{ DurationInSecs: 90, }, } So(e.ReregisterInstance(i), ShouldBeNil) var wg sync.WaitGroup stop := make(chan struct{}) wg.Add(1) go func() { defer wg.Done() ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() for { select { case <-stop: return case <-ticker.C: if err := e.HeartBeatInstance(i); err != nil { if code, ok := fargo.HTTPResponseStatusCode(err); ok && code == http.StatusNotFound { e.ReregisterInstance(i) } } } } }() Reset(func() { close(stop) wg.Wait() So(e.DeregisterInstance(i), ShouldBeNil) }) f(i) } }