-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"time" | ||
|
||
"strings" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/topfreegames/pitaya" | ||
"github.com/topfreegames/pitaya/acceptor" | ||
"github.com/topfreegames/pitaya/acceptorwrapper" | ||
"github.com/topfreegames/pitaya/component" | ||
"github.com/topfreegames/pitaya/config" | ||
"github.com/topfreegames/pitaya/examples/demo/rate_limiting/services" | ||
"github.com/topfreegames/pitaya/serialize/json" | ||
) | ||
|
||
func configureFrontend(port int) { | ||
room := services.NewRoom() | ||
pitaya.Register(room, | ||
component.WithName("room"), | ||
component.WithNameFunc(strings.ToLower)) | ||
|
||
// 5 requests in 1 minute. Doesn't make sense, just to test | ||
// rate limiting | ||
vConfig := viper.New() | ||
vConfig.Set("pitaya.conn.ratelimiting.limit", 5) | ||
vConfig.Set("pitaya.conn.ratelimiting.interval", time.Minute) | ||
pConfig := config.NewConfig(vConfig) | ||
|
||
tcp := acceptor.NewTCPAcceptor(fmt.Sprintf(":%d", port)) | ||
wrapped := acceptorwrapper.WithWrappers( | ||
tcp, | ||
acceptorwrapper.NewRateLimitingWrapper(pConfig)) | ||
pitaya.AddAcceptor(wrapped) | ||
} | ||
|
||
func main() { | ||
defer pitaya.Shutdown() | ||
|
||
port := flag.Int("port", 3250, "the port to listen") | ||
svType := "room" | ||
|
||
flag.Parse() | ||
|
||
pitaya.SetSerializer(json.NewSerializer()) | ||
configureFrontend(*port) | ||
|
||
pitaya.Configure(true, svType, pitaya.Cluster, map[string]string{}) | ||
pitaya.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/topfreegames/pitaya/component" | ||
) | ||
|
||
// Room represents a component that contains a bundle of room related handler | ||
type Room struct { | ||
component.Base | ||
} | ||
|
||
// NewRoom returns a new room | ||
func NewRoom() *Room { | ||
return &Room{} | ||
} | ||
|
||
// Ping returns a pong | ||
func (r *Room) Ping(ctx context.Context) ([]byte, error) { | ||
return []byte("pong"), nil | ||
} |