Skip to content

Commit

Permalink
fix: local proxy store
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-jacques committed Dec 3, 2024
1 parent 3381b77 commit d709c78
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 23 additions & 4 deletions proxy/local/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion proxy/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d709c78

Please sign in to comment.