Skip to content

Commit

Permalink
Handle max user counts more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvolp12 committed Feb 13, 2024
1 parent d5a1d4f commit 079f5e7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ cborgen: ## Run codegen tool for CBOR serialization
.env:
if [ ! -f ".env" ]; then cp example.dev.env .env; fi

.PHONY: run-postgres
run-postgres: .env ## Runs a local postgres instance
docker compose -f cmd/bigsky/docker-compose.yml up -d

.PHONY: run-dev-bgs
run-dev-bgs: .env ## Runs 'bigsky' BGS for local dev
GOLOG_LOG_LEVEL=info go run ./cmd/bigsky --admin-key localdev
Expand Down
25 changes: 24 additions & 1 deletion bgs/bgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (bgs *BGS) StartWithListener(listen net.Listener) error {
e.HideBanner = true

e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"http://localhost:*", "https://bgs.bsky-sandbox.dev"},
AllowOrigins: []string{"*"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, echo.HeaderAuthorization},
}))

Expand Down Expand Up @@ -1052,6 +1052,27 @@ func (s *BGS) createExternalUser(ctx context.Context, did string) (*models.Actor
return nil, fmt.Errorf("refusing to create user on PDS at max repo limit")
}

// Increment the repo count for the PDS
res := s.db.Model(&models.PDS{}).Where("id = ? AND repo_count < repo_limit", peering.ID).Update("repo_count", gorm.Expr("repo_count + 1"))
if res.Error != nil {
return nil, fmt.Errorf("failed to increment repo count for pds: %w", res.Error)
}

if res.RowsAffected == 0 {
return nil, fmt.Errorf("refusing to create user on PDS at max repo limit")
}

successfullyCreated := false

// Release the count if we fail to create the user
defer func() {
if !successfullyCreated {
if err := s.db.Model(&models.PDS{}).Where("id = ?", peering.ID).Update("repo_count", gorm.Expr("repo_count - 1")).Error; err != nil {
log.Errorf("failed to decrement repo count for pds: %s", err)
}
}
}()

ban, err := s.domainIsBanned(ctx, durl.Host)
if err != nil {
return nil, fmt.Errorf("failed to check pds ban status: %w", err)
Expand Down Expand Up @@ -1216,6 +1237,8 @@ func (s *BGS) createExternalUser(ctx context.Context, did string) (*models.Actor
return nil, err
}

successfullyCreated = true

return subj, nil
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/bigsky/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ services:
target: /var/lib/postgresql/data
restart: always
environment:
POSTGRES_USER: bgs
POSTGRES_PASSWORD: 33pAstcHDMszLedQah2EVYNgnxbCP
POSTGRES_DB: bgs
4 changes: 3 additions & 1 deletion ts/bgs-dash/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const BGS_HOST = `${window.location.protocol}//${window.location.hostname}:${
window.location.hostname === "localhost" ? "2470" : window.location.port
window.location.hostname === "localhost" || "jaz1"
? "2470"
: window.location.port
}`;

export { BGS_HOST };

0 comments on commit 079f5e7

Please sign in to comment.