Skip to content

Commit

Permalink
Merge branch 'development' of github.com:gofr-dev/gofr into fix/pubsu…
Browse files Browse the repository at this point in the history
…b_retry
  • Loading branch information
Umang01-hash committed Feb 18, 2025
2 parents 18b47a3 + d6c7f5f commit 14a73e9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
44 changes: 44 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
defaultTelemetry = "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.hasTelemetry() {
a.sendTelemetry(http.DefaultClient, false)
}

_ = a.Shutdown(shutdownCtx)
}()

if a.hasTelemetry() {
go a.sendTelemetry(http.DefaultClient, true)
}

wg := sync.WaitGroup{}

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

func (a *App) hasTelemetry() bool {
return a.Config.GetOrDefault("GOFR_TELEMETRY", defaultTelemetry) == "true"
}

func (a *App) sendTelemetry(client *http.Client, isStart bool) {
url := fmt.Sprint(gofrHost, shutServerPing)

if isStart {
url = fmt.Sprint(gofrHost, startServerPing)

a.container.Info("GoFr records the number of active servers. Set GOFR_TELEMETRY=false in configs to disable it.")
}

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

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

req.Header.Set("Connection", "close")

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
48 changes: 48 additions & 0 deletions pkg/gofr/gofr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,54 @@ 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 bool
expectedURL string
}{
{"Ping Start Server", true, gofrHost + startServerPing},
{"Ping Shut Server", false, gofrHost + shutServerPing},
}

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

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

_ = testutil.NewServerConfigs(t)

a := New()

a.sendTelemetry(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, http.MethodPost, mockTransport.lastRequest.Method, "Unexpected HTTP method")
})
}
}

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

Expand Down

0 comments on commit 14a73e9

Please sign in to comment.