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

fix submission race condition #124

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 23 additions & 1 deletion conf/constants.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package conf

import "github.com/mdg-iitr/Codephile/models/types"

const (
CODECHEF = "codechef"
CODEFORCES = "codeforces"
HACKERRANK = "hackerrank"
SPOJ = "spoj"
)

var (
codechefSubMutex = types.SubmissionMutex{Website: CODECHEF}
codeforcesSubMutex = types.SubmissionMutex{Website: CODECHEF}
hackerrankSubMutex = types.SubmissionMutex{Website: CODECHEF}
spojSubMutex = types.SubmissionMutex{Website: CODECHEF}
)

var ValidSites = []string{HACKERRANK, CODECHEF, CODEFORCES, SPOJ}

func IsSiteValid(s string) bool {
Expand All @@ -17,4 +26,17 @@ func IsSiteValid(s string) bool {
}
return false
}

func GetMutexForWebsiteSubmission(site string) *types.SubmissionMutex {
switch site {
case CODECHEF:
return &codechefSubMutex
case CODEFORCES:
return &codeforcesSubMutex
case HACKERRANK:
return &hackerrankSubMutex
case SPOJ:
return &spojSubMutex
default:
return nil
}
}
5 changes: 5 additions & 0 deletions models/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (
func AddSubmissions(uid bson.ObjectId, site string) error {
sess := db.NewUserCollectionSession()
defer sess.Close()
// Since each website submission can be allowed to fetch
// concurrently, so a different mutex for each website
mutex := GetMutexForWebsiteSubmission(site)
mutex.Lock()
defer mutex.Unlock()
coll := sess.Collection
var user types.User
err := coll.FindId(uid).Select(bson.M{"handle": 1, "lastfetched": 1}).One(&user)
Expand Down
6 changes: 6 additions & 0 deletions models/types/submission.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"sync"
"time"
)

Expand All @@ -22,3 +23,8 @@ type CodeforcesSubmissions struct {
Status string `json:"status"`
Result []map[string]interface{} `json:"result"`
}

type SubmissionMutex struct {
Website string
sync.Mutex
}