@@ -37,7 +37,14 @@ const (
3737 defaultDatabaseName = "postgres"
3838)
3939
40- type baseCloning struct {
40+ // Config contains a cloning configuration.
41+ type Config struct {
42+ MaxIdleMinutes uint `yaml:"maxIdleMinutes"`
43+ AccessHost string `yaml:"accessHost"`
44+ }
45+
46+ // Base provides cloning service.
47+ type Base struct {
4148 config * Config
4249 cloneMutex sync.RWMutex
4350 clones map [string ]* CloneWrapper
@@ -48,9 +55,9 @@ type baseCloning struct {
4855 observingCh chan string
4956}
5057
51- // NewBaseCloning instances a new base Cloning .
52- func NewBaseCloning (cfg * Config , provision * provision.Provisioner , observingCh chan string ) Cloning {
53- return & baseCloning {
58+ // NewBase instances a new Base service .
59+ func NewBase (cfg * Config , provision * provision.Provisioner , observingCh chan string ) * Base {
60+ return & Base {
5461 config : cfg ,
5562 clones : make (map [string ]* CloneWrapper ),
5663 instanceStatus : & models.InstanceStatus {
@@ -66,14 +73,15 @@ func NewBaseCloning(cfg *Config, provision *provision.Provisioner, observingCh c
6673 }
6774}
6875
69- func (c * baseCloning ) Reload (cfg Config ) {
76+ // Reload reloads base cloning configuration.
77+ func (c * Base ) Reload (cfg Config ) {
7078 * c .config = cfg
7179}
7280
73- // Initialize and run cloning component.
74- func (c * baseCloning ) Run (ctx context.Context ) error {
81+ // Run initializes and runs cloning component.
82+ func (c * Base ) Run (ctx context.Context ) error {
7583 if err := c .provision .Init (); err != nil {
76- return errors .Wrap (err , "failed to run cloning" )
84+ return errors .Wrap (err , "failed to run cloning service " )
7785 }
7886
7987 if _ , err := c .GetSnapshots (); err != nil {
@@ -86,7 +94,7 @@ func (c *baseCloning) Run(ctx context.Context) error {
8694}
8795
8896// CreateClone creates a new clone.
89- func (c * baseCloning ) CreateClone (cloneRequest * types.CloneCreateRequest ) (* models.Clone , error ) {
97+ func (c * Base ) CreateClone (cloneRequest * types.CloneCreateRequest ) (* models.Clone , error ) {
9098 cloneRequest .ID = strings .TrimSpace (cloneRequest .ID )
9199
92100 if _ , ok := c .findWrapper (cloneRequest .ID ); ok {
@@ -204,7 +212,8 @@ func (c *baseCloning) CreateClone(cloneRequest *types.CloneCreateRequest) (*mode
204212 return clone , nil
205213}
206214
207- func (c * baseCloning ) CloneConnection (ctx context.Context , cloneID string ) (pgxtype.Querier , error ) {
215+ // ConnectToClone connects to clone by cloneID.
216+ func (c * Base ) ConnectToClone (ctx context.Context , cloneID string ) (pgxtype.Querier , error ) {
208217 w , ok := c .findWrapper (cloneID )
209218 if ! ok {
210219 return nil , errors .New ("not found" )
@@ -226,7 +235,8 @@ func connectionString(host, port, username, dbname string) string {
226235 host , port , username , dbname )
227236}
228237
229- func (c * baseCloning ) DestroyClone (cloneID string ) error {
238+ // DestroyClone destroys clone.
239+ func (c * Base ) DestroyClone (cloneID string ) error {
230240 w , ok := c .findWrapper (cloneID )
231241 if ! ok {
232242 return models .New (models .ErrCodeNotFound , "clone not found" )
@@ -270,7 +280,8 @@ func (c *baseCloning) DestroyClone(cloneID string) error {
270280 return nil
271281}
272282
273- func (c * baseCloning ) GetClone (id string ) (* models.Clone , error ) {
283+ // GetClone returns clone by ID.
284+ func (c * Base ) GetClone (id string ) (* models.Clone , error ) {
274285 w , ok := c .findWrapper (id )
275286 if ! ok {
276287 return nil , errors .New ("clone not found" )
@@ -295,7 +306,8 @@ func (c *baseCloning) GetClone(id string) (*models.Clone, error) {
295306 return w .clone , nil
296307}
297308
298- func (c * baseCloning ) UpdateClone (id string , patch types.CloneUpdateRequest ) (* models.Clone , error ) {
309+ // UpdateClone updates clone.
310+ func (c * Base ) UpdateClone (id string , patch types.CloneUpdateRequest ) (* models.Clone , error ) {
299311 w , ok := c .findWrapper (id )
300312 if ! ok {
301313 return nil , models .New (models .ErrCodeNotFound , "clone not found" )
@@ -314,7 +326,7 @@ func (c *baseCloning) UpdateClone(id string, patch types.CloneUpdateRequest) (*m
314326}
315327
316328// UpdateCloneStatus updates the clone status.
317- func (c * baseCloning ) UpdateCloneStatus (cloneID string , status models.Status ) error {
329+ func (c * Base ) UpdateCloneStatus (cloneID string , status models.Status ) error {
318330 c .cloneMutex .Lock ()
319331 defer c .cloneMutex .Unlock ()
320332
@@ -328,7 +340,8 @@ func (c *baseCloning) UpdateCloneStatus(cloneID string, status models.Status) er
328340 return nil
329341}
330342
331- func (c * baseCloning ) ResetClone (cloneID string , resetOptions types.ResetCloneRequest ) error {
343+ // ResetClone resets clone to chosen snapshot.
344+ func (c * Base ) ResetClone (cloneID string , resetOptions types.ResetCloneRequest ) error {
332345 w , ok := c .findWrapper (cloneID )
333346 if ! ok {
334347 return models .New (models .ErrCodeNotFound , "the clone not found" )
@@ -391,7 +404,8 @@ func (c *baseCloning) ResetClone(cloneID string, resetOptions types.ResetCloneRe
391404 return nil
392405}
393406
394- func (c * baseCloning ) GetInstanceState () (* models.InstanceStatus , error ) {
407+ // GetInstanceState returns the current state of instance.
408+ func (c * Base ) GetInstanceState () (* models.InstanceStatus , error ) {
395409 disk , err := c .provision .GetDiskState ()
396410 if err != nil {
397411 return nil , errors .Wrap (err , "failed to get a disk state" )
@@ -412,7 +426,8 @@ func (c *baseCloning) GetInstanceState() (*models.InstanceStatus, error) {
412426 return c .instanceStatus , nil
413427}
414428
415- func (c * baseCloning ) GetSnapshots () ([]models.Snapshot , error ) {
429+ // GetSnapshots returns all available snapshots.
430+ func (c * Base ) GetSnapshots () ([]models.Snapshot , error ) {
416431 // TODO(anatoly): Update snapshots dynamically.
417432 if err := c .fetchSnapshots (); err != nil {
418433 return nil , errors .Wrap (err , "failed to fetch snapshots" )
@@ -428,7 +443,7 @@ func (c *baseCloning) GetSnapshots() ([]models.Snapshot, error) {
428443}
429444
430445// GetClones returns the list of clones descend ordered by creation time.
431- func (c * baseCloning ) GetClones () []* models.Clone {
446+ func (c * Base ) GetClones () []* models.Clone {
432447 clones := make ([]* models.Clone , 0 , c .lenClones ())
433448
434449 c .cloneMutex .RLock ()
@@ -445,7 +460,7 @@ func (c *baseCloning) GetClones() []*models.Clone {
445460}
446461
447462// findWrapper retrieves a clone findWrapper by id.
448- func (c * baseCloning ) findWrapper (id string ) (* CloneWrapper , bool ) {
463+ func (c * Base ) findWrapper (id string ) (* CloneWrapper , bool ) {
449464 c .cloneMutex .RLock ()
450465 w , ok := c .clones [id ]
451466 c .cloneMutex .RUnlock ()
@@ -454,29 +469,29 @@ func (c *baseCloning) findWrapper(id string) (*CloneWrapper, bool) {
454469}
455470
456471// setWrapper adds a clone wrapper to the map of clones.
457- func (c * baseCloning ) setWrapper (id string , wrapper * CloneWrapper ) {
472+ func (c * Base ) setWrapper (id string , wrapper * CloneWrapper ) {
458473 c .cloneMutex .Lock ()
459474 c .clones [id ] = wrapper
460475 c .cloneMutex .Unlock ()
461476}
462477
463478// deleteClone removes the clone by ID.
464- func (c * baseCloning ) deleteClone (cloneID string ) {
479+ func (c * Base ) deleteClone (cloneID string ) {
465480 c .cloneMutex .Lock ()
466481 delete (c .clones , cloneID )
467482 c .cloneMutex .Unlock ()
468483}
469484
470485// lenClones returns the number of clones.
471- func (c * baseCloning ) lenClones () int {
486+ func (c * Base ) lenClones () int {
472487 c .cloneMutex .RLock ()
473488 lenClones := len (c .clones )
474489 c .cloneMutex .RUnlock ()
475490
476491 return lenClones
477492}
478493
479- func (c * baseCloning ) getExpectedCloningTime () float64 {
494+ func (c * Base ) getExpectedCloningTime () float64 {
480495 lenClones := c .lenClones ()
481496
482497 if lenClones == 0 {
@@ -494,7 +509,7 @@ func (c *baseCloning) getExpectedCloningTime() float64 {
494509 return sum / float64 (lenClones )
495510}
496511
497- func (c * baseCloning ) fetchSnapshots () error {
512+ func (c * Base ) fetchSnapshots () error {
498513 entries , err := c .provision .GetSnapshots ()
499514 if err != nil {
500515 return errors .Wrap (err , "failed to get snapshots" )
@@ -520,7 +535,7 @@ func (c *baseCloning) fetchSnapshots() error {
520535}
521536
522537// getLatestSnapshot returns the latest snapshot.
523- func (c * baseCloning ) getLatestSnapshot () (models.Snapshot , error ) {
538+ func (c * Base ) getLatestSnapshot () (models.Snapshot , error ) {
524539 c .snapshotMutex .RLock ()
525540 defer c .snapshotMutex .RUnlock ()
526541
@@ -534,7 +549,7 @@ func (c *baseCloning) getLatestSnapshot() (models.Snapshot, error) {
534549}
535550
536551// getSnapshotByID returns the snapshot by ID.
537- func (c * baseCloning ) getSnapshotByID (snapshotID string ) (models.Snapshot , error ) {
552+ func (c * Base ) getSnapshotByID (snapshotID string ) (models.Snapshot , error ) {
538553 c .snapshotMutex .RLock ()
539554 defer c .snapshotMutex .RUnlock ()
540555
@@ -547,7 +562,7 @@ func (c *baseCloning) getSnapshotByID(snapshotID string) (models.Snapshot, error
547562 return models.Snapshot {}, errors .New ("no snapshot found" )
548563}
549564
550- func (c * baseCloning ) runIdleCheck (ctx context.Context ) {
565+ func (c * Base ) runIdleCheck (ctx context.Context ) {
551566 if c .config .MaxIdleMinutes == 0 {
552567 return
553568 }
@@ -567,7 +582,7 @@ func (c *baseCloning) runIdleCheck(ctx context.Context) {
567582 }
568583}
569584
570- func (c * baseCloning ) destroyIdleClones (ctx context.Context ) {
585+ func (c * Base ) destroyIdleClones (ctx context.Context ) {
571586 for _ , cloneWrapper := range c .clones {
572587 select {
573588 case <- ctx .Done ():
@@ -592,7 +607,7 @@ func (c *baseCloning) destroyIdleClones(ctx context.Context) {
592607}
593608
594609// isIdleClone checks if clone is idle.
595- func (c * baseCloning ) isIdleClone (wrapper * CloneWrapper ) (bool , error ) {
610+ func (c * Base ) isIdleClone (wrapper * CloneWrapper ) (bool , error ) {
596611 currentTime := time .Now ()
597612
598613 idleDuration := time .Duration (c .config .MaxIdleMinutes ) * time .Minute
0 commit comments