github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/session/pingpong/job.go (about)

     1  /*
     2   * Copyright (C) 2021 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package pingpong
    19  
    20  import (
    21  	"sync"
    22  	"time"
    23  )
    24  
    25  type job struct {
    26  	f       func(stop <-chan struct{})
    27  	jobStop chan struct{}
    28  	timeout time.Duration
    29  	once    sync.Once
    30  
    31  	lock sync.Mutex
    32  }
    33  
    34  func newJob(toRun func(stop <-chan struct{}), timeout time.Duration) *job {
    35  	j := &job{
    36  		f:       toRun,
    37  		timeout: timeout,
    38  	}
    39  
    40  	return j.init()
    41  }
    42  
    43  func (j *job) init() *job {
    44  	j.once = sync.Once{}
    45  	j.jobStop = make(chan struct{})
    46  	return j
    47  }
    48  
    49  func (j *job) Stop() *job {
    50  	j.lock.Lock()
    51  	defer j.lock.Unlock()
    52  
    53  	return j.stop()
    54  }
    55  
    56  func (j *job) stop() *job {
    57  	j.once.Do(func() {
    58  		close(j.jobStop)
    59  	})
    60  	return j
    61  }
    62  
    63  func (j *job) Restart() *job {
    64  	j.lock.Lock()
    65  	defer j.lock.Unlock()
    66  	j.stop()
    67  	new := newJob(j.f, j.timeout)
    68  	new = new.start()
    69  	return new
    70  }
    71  
    72  func (j *job) Start() *job {
    73  	j.lock.Lock()
    74  	defer j.lock.Unlock()
    75  	return j.start()
    76  }
    77  
    78  func (j *job) Done() <-chan struct{} {
    79  	return j.jobStop
    80  }
    81  
    82  func (j *job) start() *job {
    83  	go func() {
    84  		select {
    85  		case <-time.After(j.timeout):
    86  			j.Stop()
    87  			return
    88  		case <-j.jobStop:
    89  			return
    90  		}
    91  	}()
    92  	go j.f(j.jobStop)
    93  	return j
    94  }