diff --git a/proxy/local/instances.go b/proxy/local/instances.go index cf8530b..c0339e6 100644 --- a/proxy/local/instances.go +++ b/proxy/local/instances.go @@ -83,21 +83,40 @@ func (i *instances) sync() error { } i.addInstance(ie) + case corroclient.EventTypeChange: + change := e.(*corroclient.Change) + ie, err := scanInstance(change.Row) + if err != nil { + slog.Error("error scanning instance", "err", err) + continue + } + switch change.ChangeType { + case corroclient.ChangeTypeInsert, corroclient.ChangeTypeUpdate: + i.addInstance(ie) + case corroclient.ChangeTypeDelete: + i.removeInstance(ie.GatewayId, ie.InstanceId) + } + } } - return nil } func (i *instances) addInstance(ie Instance) { i.mutex.Lock() - i.instances[ie.GatewayId] = newBackend(ie) + i.instances[ie.GatewayId+ie.InstanceId] = newBackend(ie) + i.mutex.Unlock() +} + +func (i *instances) removeInstance(gw string, instanceId string) { + i.mutex.Lock() + delete(i.instances, gw+instanceId) i.mutex.Unlock() } -func (i *instances) getInstance(gw string) (*Backend, bool) { +func (i *instances) getInstance(gw string, instanceId string) (*Backend, bool) { i.mutex.RLock() - b, ok := i.instances[gw] + b, ok := i.instances[gw+instanceId] i.mutex.RUnlock() if !ok { return nil, false diff --git a/proxy/local/local.go b/proxy/local/local.go index 6f6ed49..ae4c33d 100644 --- a/proxy/local/local.go +++ b/proxy/local/local.go @@ -36,7 +36,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { slog.Debug("proxy request", "instance_id", instanceId, "gateway_id", gatewayId) - instance, ok := p.instances.getInstance(gatewayId) + instance, ok := p.instances.getInstance(gatewayId, instanceId) if !ok { slog.Debug("instance not found", "instance_id", instanceId, "gateway_id", gatewayId) w.WriteHeader(http.StatusBadGateway)