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 all 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
27 changes: 7 additions & 20 deletions database/skylink.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"gitlab.com/SkynetLabs/skyd/skymodules"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

Expand Down Expand Up @@ -51,26 +50,14 @@ func (db *DB) Skylink(ctx context.Context, skylink string) (*Skylink, error) {
}
// Try to find the skylink in the database.
filter := bson.D{{"skylink", skylinkHash}}
sr := db.staticSkylinks.FindOne(ctx, filter)
err = sr.Decode(&skylinkRec)
if errors.Contains(err, mongo.ErrNoDocuments) {
// It's not there, upsert it. We use upsert instead of insert in order
// to avoid races. And we use an update object instead of just passing
// the skylink record to UpdateOne because we want to omit the _id in
// case it has a zero value. The struct tags instruct the compiler to
// omit it when it's empty but that doesn't cover the case where it's
// zero because in that case it's a valid array of ints which happen to
// be zeros.
upsert := bson.M{"$set": bson.M{"skylink": skylinkHash}}
opts := options.Update().SetUpsert(true)
var ur *mongo.UpdateResult
ur, err = db.staticSkylinks.UpdateOne(ctx, filter, upsert, opts)
// 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 {
skylinkRec.ID = ur.UpsertedID.(primitive.ObjectID)
}
upsert := bson.M{"$setOnInsert": bson.M{"skylink": skylinkHash}}
after := options.After
opts := &options.FindOneAndUpdateOptions{
ReturnDocument: &after,
Upsert: &True,
}
sr := db.staticSkylinks.FindOneAndUpdate(ctx, filter, upsert, opts)
err = sr.Decode(&skylinkRec)
if err != nil {
return nil, err
}
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