I'm developing backend for my mobile app by using gin-gonic and gorm ORM (mysql). But I'm not sure api and db handle huge amount requests if clients increased. for example I'm using my db struct as parameter, is this a problem for concurrency ?
Here is my implementation:
main.go
:
var i Impl
func main() {
// #GIN# //
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())
// #GIN# //
// #DB# //
i.InitDB()
// #DB# //
// #TWITTER# //
t := Twitter{}
t.InitTwitter()
// #TWITTER# //
r.GET("/someEndpoint", func(c *gin.Context) {
i.InsertUser(&user)
go t.Ping(&i)
c.JSON(200, gin.H{"status": true})
})
r.Run(":5000")
}
twitter.go
:
// InitAPI TO-DO
func (t *Twitter) InitAPI(token string, tokenSecret string) {
t.API = anaconda.NewTwitterApi(token, tokenSecret)
t.UserID = strings.Split(token, "-")[0]
}
// Ping TO-DO
func (t *Twitter) Ping(i *Impl) {
t.GetHomeTimeLine(i)
t.GetUsersFriends(i)
}
// GetHomeTimeLine TO-DO
func (t *Twitter) GetHomeTimeLine(i *Impl) {
v := url.Values{}
v.Set("count", "200")
v.Set("exclude_replies", "true")
results, err := t.API.GetHomeTimeline(v)
if err != nil {
log.Println(err.Error())
return
}
tweets := convertTweets(results, t.UserID)
go i.CreateOrUpdateTimeline(tweets)
}
// GetUsersFriends TO-DO
func (t *Twitter) GetUsersFriends(i *Impl) {
v := url.Values{}
v.Set("count", "200")
v.Set("skip_status", "true")
friendsChannel := t.API.GetFriendsListAll(v)
for {
_friends, more := <-friendsChannel
if _friends.Error != nil {
log.Println(_friends.Error.Error())
break
}
friends := convertFriends(_friends.Friends, t.UserID)
go i.CreateOrUpdateFriends(friends)
if !more {
break
}
}
}
db.go
:
// Impl TO-DO
type Impl struct {
DB gorm.DB
}
// InitDB TO-DO
func (i *Impl) InitDB() {
var err error
i.DB, err = gorm.Open("mysql", "connection string")
if err != nil {
log.Fatalf("Got error when connect database, the error is '%v'", err)
}
i.DB.LogMode(false)
i.DB.DB().SetMaxIdleConns(10)
i.DB.DB().SetMaxOpenConns(100)
}
// InsertUser TO-DO
func (i *Impl) InsertUser(user *User) bool {
if err := i.DB.Create(&user).Error; err != nil {
log.Fatalf("InsertError: %s", err.Error())
return false
}
return true
}