Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skylink race #153

Merged
merged 5 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var (
// APIKeyHeader holds the name of the header we use for API keys. This
// header name matches the established standard used by Swagger and others.
APIKeyHeader = "Skynet-API-Key"
APIKeyHeader = "Skynet-API-Key" // #nosec
// ErrNoAPIKey is an error returned when we expect an API key but we don't
// find one.
ErrNoAPIKey = errors.New("no api key found")
Expand Down
11 changes: 9 additions & 2 deletions database/skylink.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,17 @@ func (db *DB) Skylink(ctx context.Context, skylink string) (*Skylink, error) {
opts := options.Update().SetUpsert(true)
var ur *mongo.UpdateResult
ur, err = db.staticSkylinks.UpdateOne(ctx, filter, upsert, opts)
if err != nil {
return nil, err
}
// The UpsertedID might be nil in case the skylink got added to the DB
// by another server in between the calls.
if err == nil && ur.UpsertedID != nil {
// by another server in between the calls. In that case we'll fetch the
// skylink record from the DB.
if ur.UpsertedID != nil {
ro-tex marked this conversation as resolved.
Show resolved Hide resolved
skylinkRec.ID = ur.UpsertedID.(primitive.ObjectID)
} else {
sr = db.staticSkylinks.FindOne(ctx, filter)
err = sr.Decode(&skylinkRec)
}
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion database/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (db *DB) UploadCreate(ctx context.Context, user User, skylink Skylink) (*Up
return nil, errors.New("invalid user")
}
if skylink.ID.IsZero() {
return nil, errors.New("invalid skylink")
return nil, errors.New("skylink doesn't exist")
}
up := Upload{
UserID: user.ID,
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ const (
envServerDomain = "SERVER_DOMAIN"
// envStripeAPIKey hold the name of the environment variable for Stripe's
// API key. It's only required when integrating with Stripe.
envStripeAPIKey = "STRIPE_API_KEY"
envStripeAPIKey = "STRIPE_API_KEY" // #nosec
// envMaxNumAPIKeysPerUser hold the name of the environment variable which
// sets the limit for number of API keys a single user can create. If a user
// reaches that limit they can always delete some API keys in order to make
// space for new ones.
envMaxNumAPIKeysPerUser = "ACCOUNTS_MAX_NUM_API_KEYS_PER_USER"
envMaxNumAPIKeysPerUser = "ACCOUNTS_MAX_NUM_API_KEYS_PER_USER" // #nosec
)

type (
Expand Down