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

core/scheduler: trigger duty at slot offset #516

Merged
merged 3 commits into from
May 13, 2022
Merged

Conversation

corverroos
Copy link
Contributor

Trigger duties at an offset within each slot.

category: feature
ticket: #515

deadlines map[core.Duty]time.Time
}

func (d *delayer) Get() map[core.Duty]time.Time {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this exported method used or aimed to used outside the package?

Copy link
Contributor Author

@corverroos corverroos May 11, 2022

Choose a reason for hiding this comment

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

As discussed before, the type delayer isn't exported, so nothing is leaked. But the function Get is used by other things in this package. So then the method is capitalised. I prefer using private methods/fields for internal use within in a type only.

Copy link
Contributor Author

@corverroos corverroos May 11, 2022

Choose a reason for hiding this comment

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

Also since this is a test, it is impossible to use it from outside the package.

Copy link
Contributor

@leolara leolara May 11, 2022

Choose a reason for hiding this comment

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

  • test of private methods can be done in _internal_test.go files, no problem for that.
  • If the method is public some developer will think that it is for public consumtion and have a method that returns the struct at some point.

hence, making it public does not give us an adventage, making it private does not requires more complex code but it keeps encapsulation.

Copy link
Contributor

Choose a reason for hiding this comment

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

When you make a method public you are documenting that the method is to be comsumed by other packages

Copy link
Contributor

Choose a reason for hiding this comment

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

You general approach of making everything private, both types and fields/methods is a subjective opinion which I have already mentioned that I do not agree with. I feel making types private is sufficient to ensure encapsulation. Public fields/method then indicate that they are used by other things in the same package as opposed to only for internal use in the type itself.

public methods in private structs does not provide encapsulation in Go as shown in the code https://github.com/leolara/privateExperiment

Copy link
Contributor

Choose a reason for hiding this comment

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

We are in a loop.

These are facts, not subjective at all:

  • private methods can be tested in Go, _internal_test.go allow that
  • public methods of private structs are accessible outside the package in Go, because in Go it is possible to have a public function that returns a private struct, and that makes the public methods of the struct accessible. However, private methods are never accessible.

This is a common rule in Software Engineering:

  • lower visibility by default, higher visibility needs a justification. Not the other way around

Copy link
Contributor

@leolara leolara May 11, 2022

Choose a reason for hiding this comment

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

here there is an example of code: https://willhaley.com/blog/private-and-public-visibility-with-go-packages/

a private struct with public method and private method, they both have a place in Go code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've documented my approach here: https://github.com/corverroos/go-visibilty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok made it unexported.


// Delay implements scheduler.delayFunc and records the deadline and returns it immediately.
func (d *delayer) Delay(duty core.Duty, deadline time.Time) <-chan time.Time {
d.mu.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this method used or aimed to be used outside the package?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

see comment above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok made it unexported.

@codecov
Copy link

codecov bot commented May 11, 2022

Codecov Report

Merging #516 (c101664) into main (22dc97a) will decrease coverage by 1.99%.
The diff coverage is 71.11%.

@@            Coverage Diff             @@
##             main     #516      +/-   ##
==========================================
- Coverage   57.42%   55.42%   -2.00%     
==========================================
  Files          87       91       +4     
  Lines        8032     8383     +351     
==========================================
+ Hits         4612     4646      +34     
- Misses       2819     3128     +309     
- Partials      601      609       +8     
Impacted Files Coverage Δ
core/scheduler/scheduler.go 74.10% <68.29%> (-0.20%) ⬇️
core/scheduler/offset.go 100.00% <100.00%> (ø)
core/scheduler/metrics.go 42.85% <0.00%> (-57.15%) ⬇️
core/proto.go 72.00% <0.00%> (-28.00%) ⬇️
core/consensus/transport.go 0.00% <0.00%> (ø)
core/consensus/msg.go 27.17% <0.00%> (ø)
core/consensus/component.go 0.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 22dc97a...c101664. Read the comment docs.

Copy link
Contributor

@leolara leolara left a comment

Choose a reason for hiding this comment

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

Please, make methods that are not for consumption outside the package private. If there is a reason why it is necessary to make delay and get public, let me know

@leolara
Copy link
Contributor

leolara commented May 12, 2022

Just because delayer is in a _test package and symbols there are never actually importable, the public methods do not have encapsulation implications. However, I would make them private to keep the discipline of keep private things that are private, so all the code base is more consistent in style.

However, if this was outside the _test package it would have serious encapsulation implications as explained.

@corverroos corverroos added the merge when ready Indicates bulldozer bot may merge when all checks pass label May 13, 2022
@obol-bulldozer obol-bulldozer bot merged commit 876402b into main May 13, 2022
@obol-bulldozer obol-bulldozer bot deleted the corver/schedoffset branch May 13, 2022 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge when ready Indicates bulldozer bot may merge when all checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants