Skip to content
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

MF-1556 - Move the most used functions in main.go to internal package #1601

Merged
merged 170 commits into from
Feb 3, 2023

Conversation

arvindh123
Copy link
Contributor

@arvindh123 arvindh123 commented May 3, 2022

What does this do?

This pull request for moving the HTTP Server and GRPC server from cmd/service/main.go to internal/mfserver package

Which issue(s) does this PR fix/relate to?

Resolve partially #1556 because need to check other functions feasibility to move to internal package

List any changes that modify/break current functionality

N/A

Have you included tests for your changes?

N/A

Did you document any new/modified functionality?

N/A

Notes

N/A

@codecov-commenter
Copy link

codecov-commenter commented May 3, 2022

Codecov Report

Merging #1601 (72c26ca) into master (ada5813) will increase coverage by 0.06%.
The diff coverage is 100.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##           master    #1601      +/-   ##
==========================================
+ Coverage   70.32%   70.38%   +0.06%     
==========================================
  Files         148      146       -2     
  Lines       11501    11394     -107     
==========================================
- Hits         8088     8020      -68     
+ Misses       2745     2724      -21     
+ Partials      668      650      -18     
Impacted Files Coverage Δ
certs/postgres/certs.go 0.00% <ø> (ø)
readers/postgres/init.go 89.47% <ø> (ø)
readers/postgres/messages.go 85.29% <ø> (ø)
readers/timescale/init.go 89.47% <ø> (ø)
auth/postgres/init.go 100.00% <100.00%> (+5.00%) ⬆️
bootstrap/postgres/init.go 100.00% <100.00%> (+5.97%) ⬆️
certs/postgres/init.go 100.00% <100.00%> (+22.58%) ⬆️
certs/service.go 83.13% <100.00%> (-0.21%) ⬇️
consumers/notifiers/postgres/init.go 100.00% <100.00%> (+14.28%) ⬆️
consumers/writers/postgres/init.go 100.00% <100.00%> (+10.52%) ⬆️
... and 3 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@dborovcanin dborovcanin changed the title MF-1556 - Moving the HTTP Server and GRPC server to internal MF-1556 - Move the most used functions in main.go to internal package May 3, 2022
cmd/auth/main.go Outdated Show resolved Hide resolved
cmd/auth/main.go Outdated Show resolved Hide resolved
cmd/auth/main.go Outdated Show resolved Hide resolved
@@ -0,0 +1,41 @@
// Copyright (c) Mainflux
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API util does not look like a good place for this. Something like internal/db/connect.go. Also, the most commonly used DB connection (Postgres) is missing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So with Postgres, there is a slight issue. For instance when the auth service connects to Postgres, it will do a migration which is specific to the auth service and hence the bootstrap can't use it.
If we implement a common connect to Postgres this would mean we will import the different modules. Is that okay?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dborovcanin, Moving of postgres connect to internal package could be done in this PR or could we do it in seperate PR?

stdprometheus "github.com/prometheus/client_golang/prometheus"
)

func MakeMetrics(svcName string) (*kitprometheus.Counter, *kitprometheus.Summary) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this complex logic. You can simply pass namespace and subsystem and use:

counter := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
		Namespace: namespace,
		Subsystem: subsystem,
		Name:      "request_count",
		Help:      "Number of requests received.",
	}, []string{"method"})
	latency := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
		Namespace: namespace,
		Subsystem: subsystem,
		Name:      "request_latency_microseconds",
		Help:      "Total duration of requests in microseconds.",
	}, []string{"method"})

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to differentiate the help message between a reader and a writer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dborovcanin, changed, Please reveiew this part and give your comments,

@@ -0,0 +1,39 @@
// Copyright (c) Mainflux
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming apiutil to something closer to what this package is for - maybe initutil or just init.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dborovcanin, The init is misleading so we decided to use apiutil. And the internal folder is restrucutred.
Please review the latest code and give your comments

type GRPCServer struct {
mfserver.BaseServer
server *grpc.Server
authSrvSvr mainflux.AuthServiceServer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auth service sounds implementation-specific. What with other gRPC services, such as ThingsServiceServer?

}

func stopAllServer(servers ...Server) error {
var err error
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this error for?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be able to aggregate all the errors. For instance, we have two errors out of 5 servers, we will be able to return all the errors rather than the error it checked first

@dborovcanin dborovcanin marked this pull request as ready for review May 24, 2022 07:38
@dborovcanin dborovcanin requested a review from a team as a code owner May 24, 2022 07:38
auth/api/grpc/requests.go Outdated Show resolved Hide resolved
@@ -11,7 +11,7 @@ import (
"github.com/golang/protobuf/ptypes/empty"
mainflux "github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/auth"
"github.com/mainflux/mainflux/internal/apiutil"
apiutil "github.com/mainflux/mainflux/internal/init"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here and for other imports

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming to initutil

auth/api/grpc/requests.go Outdated Show resolved Hide resolved
cmd/http/main.go Outdated
"github.com/mainflux/mainflux"
adapter "github.com/mainflux/mainflux/http"
"github.com/mainflux/mainflux/http/api"
initutil "github.com/mainflux/mainflux/internal/init"
"github.com/mainflux/mainflux/internal/init/mfserver"
"github.com/mainflux/mainflux/internal/init/mfserver/httpserver"
Copy link
Contributor

@manuio manuio May 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite redundant. I'd use "github.com/mainflux/mainflux/internal/servers/http"
I'd prpose the next organisation:

internal/auth/auth.go
internal/auth/keto.go
internal/auth/things.go
internal/db/redis.go
internal/db/mongo.go
internal/server/http.go
internal/server/coap.go
internal/server/grpc.go
internal/apiutil/transport.go
internal/apiutil/responses.go
internal/apiutil/tokens.go
internal/apiutil/errors.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manuio, changed as per you proposal

cmd/auth/main.go Outdated
@@ -19,8 +19,8 @@ import (
"github.com/mainflux/mainflux/auth/tracing"
"github.com/mainflux/mainflux/internal"
"github.com/mainflux/mainflux/internal/server"
"github.com/mainflux/mainflux/internal/server/grpcserver"
"github.com/mainflux/mainflux/internal/server/httpserver"
mfgrpcserver "github.com/mainflux/mainflux/internal/server/grpc"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove mf prefix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manuio, removed mf prefix

@@ -253,6 +253,6 @@ func encodeError(err error) error {
case errors.Contains(err, errors.ErrAuthorization):
return status.Error(codes.PermissionDenied, err.Error())
default:
return status.Error(codes.Internal, "internal server error")
return status.Error(codes.Internal, "apiutil server error")
Copy link
Contributor

@manuio manuio May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What this means? Can we find something more meaningfull?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manuio ,changed to internal server error

cmd/bootstrap/main.go Outdated Show resolved Hide resolved
internal/starter/loadConfig.go Outdated Show resolved Hide resolved
internal/starter/loadConfig.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@dborovcanin dborovcanin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the lib @drasko suggested works for us - I'd go for it. It looks well written, tested, broadly used, and lightweight enough for us. I like having our solution, but we'd need to invest in testing and maintenance with no obvious benefits.

Comment on lines +14 to +15
Start() error
Stop() error
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Serve and Shutdown are more similar to Go HTTP server terminology.

Ctx context.Context
Cancel context.CancelFunc
Name string
Address string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need Address field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dborovcanin, Yes required. Now it is used properly.

internal/server/http/http.go Show resolved Hide resolved
internal/server/grpc/grpc.go Show resolved Hide resolved
arvindh123 and others added 12 commits December 9, 2022 20:34
* Add : errgroup to cmd/auth

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Add : Handle graceful stop for auth service
Remove : errgroups from auth service

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Add : Wait till server shutdown

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Change : instead of waitgroup changed to errgroups

Signed-off-by: Arvindh <arvindh91@gmail.com>

* change : KillSignalHandler return type to error

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Empty Commit

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Add : Context to http server shutdown
Rename : varaible from proto to protocol

Signed-off-by: Arvindh <arvindh91@gmail.com>

* change : to default log level

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Add : Sign-off

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Add: graceful stop of http and grpc server

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Fix: typos and caps

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Add: Signed-off

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Rename: Func KillSignalHandler to SignalHandler
Add: SIGABRT

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Fix: auth service

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Add: timeout for grpc gracefulstop
Fix: typos

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Add: .vscode folder to git ignore

Signed-off-by: Arvindh <arvindh91@gmail.com>

* change: variable name to stopWaitTime

Signed-off-by: Arvindh <arvindh91@gmail.com>

* remove: .vscode folder

Signed-off-by: Arvindh <arvindh91@gmail.com>

* remove: .vscode from .gitignore

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Add : logger to handlers

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Add : New line at end of .gitignore file

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Fix : variable naming
Add : graceful stop for timescale

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Remove : unsued NATS library from import

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Move: "https" and "https" to moved to const var

Signed-off-by: Arvindh <arvindh91@gmail.com>

* Move: "http" and "https" to moved to const var

Signed-off-by: Arvindh <arvindh91@gmail.com>

* update:  branch with master

Signed-off-by: Arvindh <arvindh91@gmail.com>

Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
Co-authored-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
* Initial commit

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Update subscriber interface

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Add tests

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Add tests

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* check subscription map

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Check topic id after topic

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* reword description

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Setup empty queue

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Change mqtt implementation

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Switch statements

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Simplify

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Change mqtt subscriber

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Protect subscription map

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Fix subscription

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Set client id

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Format

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Change delete method

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
* Add max and min limit size

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Format

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

* Format

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>

Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: zhangchuanfeng <654300242@qq.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
@arvindh123
Copy link
Contributor Author

Rename internal/client to internal/clients.

Done

auth/postgres/init.go Outdated Show resolved Hide resolved
certs/postgres/init.go Outdated Show resolved Hide resolved
things/postgres/init.go Outdated Show resolved Hide resolved
arvindh123 and others added 2 commits January 25, 2023 22:18
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
dborovcanin
dborovcanin previously approved these changes Jan 25, 2023
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
dborovcanin
dborovcanin previously approved these changes Jan 26, 2023
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
dborovcanin
dborovcanin previously approved these changes Jan 26, 2023
Copy link
Contributor

@drasko drasko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all good, just please remove full-stops from one-sentence comments and I will approve.


func migrateDB(db *sqlx.DB) error {
migrations := &migrate.MemoryMigrationSource{
// Migration of Auth service.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for a full-stop at the end of the comment. Only if the comment spans more than one sentences, then putting full-stops makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed end dot in single line comments

Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
Signed-off-by: Arvindh <arvindh91@gmail.com>
drasko
drasko previously approved these changes Feb 2, 2023
Copy link
Contributor

@drasko drasko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Signed-off-by: Arvindh <arvindh91@gmail.com>
Copy link
Contributor

@drasko drasko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants