Skip to content

Storing the number of running GoFr servers #1474

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

Merged
merged 21 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion examples/grpc/grpc-server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestGRPCServer(t *testing.T) {
host := configs.GRPCHost

go main()
time.Sleep(100 * time.Millisecond)
time.Sleep(1000 * time.Millisecond)

client, conn := createGRPCClient(t, host)
defer conn.Close()
Expand Down
38 changes: 38 additions & 0 deletions pkg/gofr/gofr.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ const (
gofrTraceExporter = "gofr"
gofrTracerURL = "https://tracer.gofr.dev"
checkPortTimeout = 2 * time.Second
gofrHost = "https://gofr.dev"
startServerPing = "/api/ping/up"
shutServerPing = "/api/ping/down"
pingTimeout = 5 * time.Second
TelemetryTrue = "true"
)

// App is the main application in the GoFr framework.
Expand Down Expand Up @@ -173,9 +178,17 @@ func (a *App) Run() {
shutdownCtx, done := context.WithTimeout(context.WithoutCancel(ctx), shutDownTimeout)
defer done()

if a.Config.GetOrDefault("GOFR_TELEMETRY", "true") == TelemetryTrue {
a.pingGoFr(http.DefaultClient, "shutdown")
}

_ = a.Shutdown(shutdownCtx)
}()

if a.Config.GetOrDefault("GOFR_TELEMETRY", "true") == TelemetryTrue {
a.pingGoFr(http.DefaultClient, "start")
}

wg := sync.WaitGroup{}

// Start Metrics Server
Expand Down Expand Up @@ -224,6 +237,31 @@ func (a *App) Run() {
wg.Wait()
}

func (a *App) pingGoFr(client *http.Client, s string) {
url := fmt.Sprint(gofrHost, shutServerPing)

if s == "start" {
url = fmt.Sprint(gofrHost, startServerPing)

a.container.Info("GoFr tracks server count via telemetry. Use GOFR_TELEMETRY to disable it.")
}

ctx, cancel := context.WithTimeout(context.Background(), pingTimeout)
defer cancel()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return
}

resp, err := client.Do(req)
if err != nil {
return
}

resp.Body.Close()
}

// Shutdown stops the service(s) and close the application.
// It shuts down the HTTP, gRPC, Metrics servers and closes the container's active connections to datasources.
func (a *App) Shutdown(ctx context.Context) error {
Expand Down
52 changes: 51 additions & 1 deletion pkg/gofr/gofr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestGoFr_isPortAvailable(t *testing.T) {
g := New()

go g.Run()
time.Sleep(100 * time.Millisecond)
time.Sleep(1000 * time.Millisecond)
}

isAvailable := isPortAvailable(configs.HTTPPort)
Expand All @@ -71,6 +71,56 @@ func TestGoFr_isPortAvailable(t *testing.T) {
}
}

// mockRoundTripper is a mock implementation of http.RoundTripper.
type mockRoundTripper struct {
lastRequest *http.Request // Store the last request for assertions
mockResponse *http.Response
mockError error
}

// RoundTrip mocks the HTTP request and stores the request for verification.
func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
m.lastRequest = req // Store the request for assertions
return m.mockResponse, m.mockError
}

func TestPingGoFr(t *testing.T) {
tests := []struct {
name string
input string
expectedURL string
expectedMethod string
mockStatus int
}{
{"Ping Start Server", "start", gofrHost + startServerPing, http.MethodGet, http.StatusOK},
{"Ping Shut Server", "stop", gofrHost + shutServerPing, http.MethodGet, http.StatusOK},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockTransport := &mockRoundTripper{
mockResponse: &http.Response{
StatusCode: tt.mockStatus,
Body: http.NoBody,
},
mockError: nil,
}

mockClient := &http.Client{Transport: mockTransport}

_ = testutil.NewServerConfigs(t)

a := New()

a.pingGoFr(mockClient, tt.input)

assert.NotNil(t, mockTransport.lastRequest, "Request should not be nil")
assert.Equal(t, tt.expectedURL, mockTransport.lastRequest.URL.String(), "Unexpected request URL")
assert.Equal(t, tt.expectedMethod, mockTransport.lastRequest.Method, "Unexpected HTTP method")
})
}
}

func TestGofr_ServerRoutes(t *testing.T) {
_ = testutil.NewServerConfigs(t)

Expand Down
Loading