Skip to content

Commit

Permalink
Merge pull request #1 from mocha-bot/fix/100-percent-cpu
Browse files Browse the repository at this point in the history
fix: high cpu usage when goroutine select default in loop
  • Loading branch information
Aldiwildan77 authored Sep 14, 2024
2 parents a7dd70b + 0255d22 commit 15a134e
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 163 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ test_env:

start:
@echo "Starting the server"
@go run *.go
@go run *.go

build:
@echo "Building the server"
@go mod tidy && go mod download && go build -v -o engine && chmod +x engine

run:
@echo "Injecting environment variables"
@echo "Running the server"
@./engine
12 changes: 12 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type AppConfig struct {
Port int `env:"APP_PORT" envDefault:"8080"`
LogLevel string `env:"LOG_LEVEL" envDefault:"debug"`
TimeZone string `env:"TIME_ZONE" envDefault:"Asia/Jakarta"`
Profile bool `env:"PROFILE_ENABLED" envDefault:"false"`
}

type DatabaseDialect string
Expand Down Expand Up @@ -81,12 +82,23 @@ type GoroutineConfig struct {
Workers int `env:"WORKERS" envDefault:"10"`
}

type PPROF struct {
Host string `env:"PPROF_HOST" envDefault:"localhost"`
Port string `env:"PPROF_PORT" envDefault:"6060"`
IsEnabled bool `env:"PPROF_ENABLED" envDefault:"false"`
}

func (p *PPROF) Address() string {
return fmt.Sprintf("%s:%s", p.Host, p.Port)
}

type Config struct {
AppConfig
DatabaseConfig
RedisConfig
MeilisearchConfig
GoroutineConfig
PPROF
}

func GetConfig() (*Config, error) {
Expand Down
40 changes: 16 additions & 24 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
module github.com/mocha-bot/granola

go 1.21.1
go 1.23.0

require (
github.com/caarlos0/env/v6 v6.10.1
github.com/go-redis/redis v6.15.9+incompatible
github.com/meilisearch/meilisearch-go v0.27.0
github.com/meilisearch/meilisearch-go v0.28.0
github.com/rs/zerolog v1.33.0
golang.org/x/sync v0.7.0
golang.org/x/sync v0.8.0
gorm.io/driver/mysql v1.5.7
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.11
honnef.co/go/tools v0.4.7
gorm.io/gorm v1.25.12
)

require filippo.io/edwards25519 v1.1.0 // indirect

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.15.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.33.1 // indirect
github.com/redis/go-redis/v9 v9.5.4 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.37.1-0.20220607072126-8a320890c08d // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.20.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/redis/go-redis/v9 v9.6.1
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
)
160 changes: 32 additions & 128 deletions go.sum

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
_ "net/http/pprof"

"context"
"fmt"
"net/http"
"os"
"os/signal"
"sync"
Expand All @@ -25,8 +28,19 @@ func main() {

zLog.Info().Msgf("Config: %+v", cfg)

if cfg.PPROF.IsEnabled {
zLog.Info().Msg("Starting pprof...")
go func() {
zLog.Info().Msgf("pprof is now running on %s", cfg.PPROF.Address())
err := http.ListenAndServe(cfg.PPROF.Address(), nil)
if err != nil {
zLog.Fatal().Err(err).Caller().Msg("error starting pprof")
}
}()
}

var redis *redis.Client
var meilisearch *meilisearch.Client
var meilisearch meilisearch.ServiceManager
var db *gorm.DB

eg, mCtx := errgroup.WithContext(ctx)
Expand Down
7 changes: 2 additions & 5 deletions meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import (
"github.com/meilisearch/meilisearch-go"
)

func NewMeilisearchInstance(ctx context.Context, cfg MeilisearchConfig) (*meilisearch.Client, error) {
client := meilisearch.NewClient(meilisearch.ClientConfig{
Host: cfg.Host,
APIKey: cfg.MasterKey,
})
func NewMeilisearchInstance(ctx context.Context, cfg MeilisearchConfig) (meilisearch.ServiceManager, error) {
client := meilisearch.New(cfg.Host, meilisearch.WithAPIKey(cfg.MasterKey))

if !client.IsHealthy() {
return nil, fmt.Errorf("meilisearch is not healthy, please check the configuration")
Expand Down
2 changes: 0 additions & 2 deletions process.redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ func (h *pubSub) ProcessMessage(ctx context.Context) {
}

zLog.Debug().Msgf("Worker %d added room to document: %+v", i, room)
default:
// Add default case to prevent blocking
}
}
}(idx)
Expand Down
4 changes: 2 additions & 2 deletions room.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ type Repository interface {

type repository struct {
db *gorm.DB
search *meilisearch.Client
search meilisearch.ServiceManager
}

func NewRepository(db *gorm.DB, search *meilisearch.Client) Repository {
func NewRepository(db *gorm.DB, search meilisearch.ServiceManager) Repository {
return &repository{
db: db,
search: search,
Expand Down

0 comments on commit 15a134e

Please sign in to comment.