Skip to content

Commit

Permalink
Merge pull request #7 from Vector-Hector/dev
Browse files Browse the repository at this point in the history
Interface improvements
  • Loading branch information
Vector-Hector committed Dec 15, 2023
2 parents fd4cef3 + 5a49706 commit e5aea0e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion astar.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (b *Bifrost) RouteOnlyTimeIndependent(rounds *Rounds, origins []SourceKey,

_, ok := rounds.EarliestArrivals[destKey]
if !ok {
panic("destination unreachable")
panic(NoRouteError(true))
}

journey := b.ReconstructJourney(destKey, 1, rounds)
Expand Down
9 changes: 9 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bifrost

import "fmt"

type NoRouteError bool

func (e NoRouteError) Error() string {
return fmt.Sprintf("no route found")
}
2 changes: 1 addition & 1 deletion raptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (b *Bifrost) RouteTransit(rounds *Rounds, origins []SourceKey, destKey uint

_, ok = rounds.EarliestArrivals[destKey]
if !ok {
return nil, fmt.Errorf("destination unreachable")
return nil, NoRouteError(true)
}

journey := b.ReconstructJourney(destKey, lastRound, rounds)
Expand Down
46 changes: 30 additions & 16 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ func (s *StringSlice) Set(value string) error {
return nil
}

func SemaphoreMiddleware(maxConcurrentRequests int) gin.HandlerFunc {
semaphore := make(chan bool, maxConcurrentRequests)

return func(c *gin.Context) {
semaphore <- true
defer func() {
<-semaphore
}()

c.Next()
}
}

func main() {
var osmPath StringSlice
var gtfsPath StringSlice
Expand Down Expand Up @@ -84,23 +97,13 @@ func main() {
*numHandlerThreads = roundChanSize
}

threadChan := make(chan bool, *numHandlerThreads)

for i := 0; i < *numHandlerThreads; i++ {
threadChan <- true
}

fmt.Println("Startup took", time.Since(start))

engine := gin.Default()

engine.POST("/bifrost", func(c *gin.Context) {
<-threadChan

defer func() {
threadChan <- true
}()
engine.Use(SemaphoreMiddleware(*numHandlerThreads))

engine.POST("/bifrost", func(c *gin.Context) {
handle(c, b)
})

Expand All @@ -112,13 +115,24 @@ func main() {

func handle(c *gin.Context, b *bifrost.Bifrost) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
r := recover()

debug.PrintStack()
if r == nil {
return
}

c.String(500, "Internal server error: %v", r)
if _, ok := r.(bifrost.NoRouteError); ok {
c.JSON(404, gin.H{
"error": "no route found",
})
return
}

fmt.Println("Recovered in f", r)

debug.PrintStack()

c.String(500, "Internal server error: %v", r)
}()

req := &JourneyRequest{}
Expand Down

0 comments on commit e5aea0e

Please sign in to comment.